Changing texture of Blender model programmatically [resolved]

Hi everybody,

following situation: I’ve got a model (exported from Blender as model.mesh.xml, model.material and texture.jpg) that I want to use more than once in the game but with different textures.

Now if I simply do something like this


Spatial model = assetManager.loadModel(“Models/model/model.mesh.xml”);

Material m = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

m.setTexture(“ColorMap”, assetManager.loadTexture(“Models/model/other-texture.jpg”));



apparently the UV-mapping (and probably shading instructions etc) are lost.

Is there a way to change only the texture used in the material from inside jMonkey? Thanks for your help!

Get the material from the model. Clone it, then change the texture, and set that material to the model.

Sorry, forgot to mention this in the original post:

When I do this and try to set the ColorMap (like in the example above), it says the material has no such parameter.

The way I did this was to have several materials with the different textures and swap the materials on the geometry… I did have multiple on screen at once all potentially changing though.

That’s essentially what wezrule suggested…but it still doesn’t address my issue :wink:

@lunikon said:
apparently the UV-mapping (and probably shading instructions etc) are lost.

No. uv coordinates are stored on the mesh level.

You probably have to flip your texture. load it with a texture key to do it
TextureKey key = new TextureKey("Models/model/other-texture.jpg", true);
m.setTexture("ColorMap", assetManager.loadTexture(key));
The boolean argument in the TextureKey constructor is for flipping.
1 Like

The loader uses Common/MatDefs/Light/Lighting.j3md, not Common/MatDefs/Misc/Unshaded.j3md. ColorMap is only available in Unshaded.

Use the DiffuseMap parameter instead :slight_smile:

See for details

1 Like

Thanks a lot @nehon

That fixed the UV issue. Concerning the other material properties: A short debugging session (shame on me for not trying this earlier) revealed that the parameter simply isn’t called “ColorMap” but “DiffuseMap”, so

[java]m.setTexture(“DiffuseMap”, assetManager.loadTexture(key));[/java]

did the trick.

@rylius was faster :slight_smile:

Yeeh, the texture parameter name is different for unshaded and lighting materials.