Loading materials from string

19 views
Skip to first unread message

Bernie Roehl

unread,
Apr 29, 2007, 4:30:48 PM4/29/07
to Python Ogre Developers
I'm converting my application from PyOgre to Python-Ogre.

I'm trying to load a material from a string, which I could do in
PyOgre like this:

ogre.MaterialManager.getSingleton().parseScript(someString,
"General")

Is there a way of doing this in Python-Ogre?

I'm thinking if I could create a MemoryStreamDataPtr from a string, I
could just pass that to parseScript, but there doesn't seem to be any
way to do that.

Anyone have any suggestions?

Andy Miller

unread,
Apr 29, 2007, 6:19:42 PM4/29/07
to python-ogre...@googlegroups.com
Do you have a more complete example -- ie what would be in 'someString'
 
Cheers
 
Andy

 

Bernie Roehl

unread,
Apr 29, 2007, 10:22:42 PM4/29/07
to python-ogre...@googlegroups.com
It could be called like this:

ogre.MaterialManager.getSingleton().parseScript(file("myfile.material"),
"General")

... in other words, the string would be the contents of a .material file.  Of course, it wouldn't actually be loaded from a file.  In my case, it would be receiving over the network.



--
   Bernie Roehl
   Mail: bro...@gmail.com
   Voice:  (519) 577-8494

Game_Ender

unread,
Apr 30, 2007, 2:46:21 AM4/30/07
to Python Ogre Developers
Well that looks like a custom wrapper. That method really takes a
DataStreamPtr. So I guess we could create some code the creates a
data stream pointer out of python file like objects. The DataStream
API if very similar.

On Apr 29, 10:22 pm, "Bernie Roehl" <bro...@gmail.com> wrote:
> It could be called like this:
>
> ogre.MaterialManager.getSingleton().parseScript(file("myfile.material"),
> "General")
>
> ... in other words, the string would be the contents of a .material file.
> Of course, it wouldn't actually be loaded from a file. In my case, it would
> be receiving over the network.
>

Andy Miller

unread,
Apr 30, 2007, 3:08:58 AM4/30/07
to python-ogre...@googlegroups.com
Try this:
 
f= file("test.material", 'r')
MatString = f.read()
RawMemBuffer = ctypes.create_string_buffer( MatString ) ## Note it allocates one extra byte
dataptr = ogre.MemoryDataStream ( ctypes.addressof ( RawMemBuffer ), len (MatString) + 1 )
ogre.MaterialManager.getSingleton().parseScript( dataptr, "General" ) 
 
Cheers

Andy

Bernie Roehl

unread,
Apr 30, 2007, 6:56:02 AM4/30/07
to python-ogre...@googlegroups.com
Actually, yes -- a DataStreamPtr that gets built from a Python file-like object would make the most sense, since they're conceptually similar.

Can that be added to the "wish list" for Python-Ogre?

Bernie Roehl

unread,
May 1, 2007, 1:15:57 PM5/1/07
to Python Ogre Developers
This doesn't seem to be working for me. I try loading the material
file, and there are no errors, but the materials aren't loaded.

It appears that the MemoryDataStream is not being created correctly.
As a test, I tried doing the following:


>>> string = str("Hello world")
>>> memBuffer = ctypes.create_string_buffer(string)
>>> print memBuffer
<ctypes.c_char_Array_12 object at 0x008EDD00>
>>> print memBuffer[0]
'H'
>>> # So far, so good
>>> dataptr = ogre.MemoryDataStream (ctypes.addressof(memBuffer), len(string)+1)
>>> print dataptr
<ogre.renderer.OGRE._ogre_.MemoryDataStream object at 0x01421B40>
>>> # Good
>>> print dataptr.getLine()

>>> # Not so good :-(

Every call to dataptr.getLine() returns an empty string, which
explains why the materials aren't being loaded.

Am I missing something?

Andy Miller

unread,
May 4, 2007, 8:57:44 AM5/4/07
to python-ogre...@googlegroups.com
You aren't missing anything -- I gave you an incorrect example....
 
f= file("test.material", 'r')
MatString = f.read()
RawMemBuffer = ctypes.create_string_buffer( MatString ) ## Note it allocates one extra byte
dataptr = ogre.MemoryDataStream ( ogre.CastVoidPtr (ctypes.addressof ( RawMemBuffer )), len (MatString) + 1 )
ogre.MaterialManager.getSingleton().parseScript( dataptr, "General" )
 
Note the specific cast to a void pointer for the ctypes buffer.. 
 
Now all of this is rather ugly because we are putting as thin a layer as possible over the C++ library..  However all is not lost as Roman came up with a really cool way to manage raw buffers that IS implemented for MemoryDataStream's in the current PythonOgre release..
 

f= file(

"test.material", 'r'
)
MatString = f.read ()
dataptr = ogre.MemoryDataStream ( size=len (MatString) )
# create the memory buffer
p = dataptr.getPtr() # retrieve a handle to the raw memory buffer
pos=0
for c in MatString: # copy the string char at a time to the mem buffer
    p[pos]=ord(c) # note we need to write a 'char' so use ord to get it..
    pos+=1
> RawMemBuffer = ctypes.create_string_buffer ( MatString ) ## Note it allocates

Bernie Roehl

unread,
May 4, 2007, 1:49:03 PM5/4/07
to python-ogre...@googlegroups.com
Using CastVoidPtr worked great -- thanks, I really appreciate the help!


> > > ogre.MaterialManager.getSingleton ().parseScript(file("myfile.material"),
Reply all
Reply to author
Forward
0 new messages