[SOLVED] How to override a texture supplied by a bam file

In order to completely replace an existing texture on a model, you have to have a pointer to the TextureStage that was used to apply the texture to the model, and use that same TextureStage.

Since your example code creates a new TextureStage, it will of course add to the texture stack, rather than replacing the existing texture. Think of each TextureStage as a slot to hold a texture. If you want to replace a particular texture, you have to store it in the same slot. Each time you apply a new TextureStage, you are stacking a new texture slot into the box.

Normally, if your model doesn’t apply any custom-named UV’s or any other fancy texture properties, then the egg loader will apply its textures to the default TextureStage, which is why you can usually override a texture on a simple model with simply model.setTexture(newTex, 1), which also uses the default TextureStage.

Since your model uses custom-named UV’s, it will have a custom TextureStage implicitly created to apply this texture. So you can find that TextureStage in the model and reuse it with something like:

ts = model.findTextureStage('*')
model.setTexture(ts, tex, 1)

Note that the above also works for the case that the model uses the default TextureStage.

David