Changing materials dynamically

Danaugrs

03-11-2007 18:56:13

How can I change the texture in my python program, without having to create lots of materials, each with one texture?
For instance, I have a background plane, with this material:

material Game/Background
{
technique
{
pass
{
scene_blend alpha_blend
depth_writing off
texture_unit
{
texture background12.jpg
}
}
}
}


How can I change, with python, only the texture? (in the example, background12.jpg)
Or do I have to create the whole material by programming?

Regards in advance,
Danaugrs

Game_Ender

04-11-2007 00:28:06

This is not a Python-Ogre specific question. You will get a quicker answer on the normal Ogre forum. I do know that the you can change materials at run time (you don't have to create new ones). Please see the API docs.

EDIT: Fixed glaring logical and grammar mistakes.

Danaugrs

04-11-2007 11:29:53

Sorry, I forgot there were other forums hehe :D

Messing around with overlays, i made a very opportune mistake! instead of looking for overlayManager on the API I looked out for resourceManager and, after getting used to many manager functions I saw at some place in the forums a materialManager. I believe all the managers work the same, so I will try what I had done with the materialManager.
When I'm able to find the answer I'll post it here.

Regards,
Danaugrs

bharling

04-11-2007 13:38:49

Heres some code to change an image at runtime:

oceanMat = ogre.MaterialManager.getSingleton().getByName("OceanCg_RTT")
mPass = oceanMat.getTechnique(0).getPass(0)
tState = mPass.getTextureUnitState(0)
tState.setTextureName("new.jpg")


and heres some to create a material with a specified texture:

mat = ogre.MaterialManager.getSingleton().create("RttMat",ogre.ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME)
t = mat.getTechnique(0).getPass(0).createTextureUnitState('Water01.jpg')

Danaugrs

04-11-2007 18:06:47

Thank you very much!
I arrived at the same solution, :wink: but I forgot to post. hehe :D
But mine has like, one line for pass another for technique...
and lots of useless variables hehe
So I will change that to your way.

I didn't know how to create materials entirely by programming,
That last piece of code might come in handy

Regards,
Danaugrs

bharling

05-11-2007 09:19:36

you don't even need most of the variables / steps I've mentioned, but you would end up with quite a long line of code!