Setting material to a model

I wrote a script that loads a model twice and change the material of each one independently. I changed the first material of the first model to be blue and i changed the material of the second one to be green. But when i run the script both models appear green. Why do this happen? And how to fix it?
This is a part of the script


    self.directionalLight = DirectionalLight('directionalLight')
    self.directionalLight.setColor((1, 1, 1, 1))
    self.directionalLightNP = self.render.attachNewNode(self.directionalLight)

    self.pandaModel.findMaterial("Body").setDiffuse((0, 0, 1, 1))
    self.myModel2 =loader.loadModel("car_red")
    self.myModel2.setPos(0, 0, 5)

    self.directionalLight2 = DirectionalLight('directionalLight2')
    self.directionalLight2.setColor((1, 1, 1, 1))
    self.directionalLightNP2 = self.render.attachNewNode(self.directionalLight2)

    self.myModel2.findMaterial("Body").setDiffuse((0, 1, 0, 1))

Thanks in advance.

I imagine that, for performance reasons, there is only one material object, and that it’s shared between the two models. After all, proliferating identical materials could potentially cause significant performance issues.

You could perhaps check this by printing out the Material object that’s returned by our two calls to “findMaterial”.

As to dealing with this, it looks like you can apply a new material via “setMaterial”.

However, if you’re only re-colouring your materials, there might be a simpler way: instead of altering the materials of your models, you could give the material itself a white diffuse colour, and then apply a colour or colour-scale to the model’s NodePath.

However, I don’t know whether this would affect the non-diffuse components of the model’s appearance; if it does, then it might not be desirable.

1 Like

I printed the material objects and they were the same as you have suggested.
I tried setMaterial but it resulted in the same behavior.
Setting the diffuse to be white and adding a color scale worked =D
Thank you very much.