How to change material ?

I have a car model which has only one material defined for the body part.

<Material> "Car paint 3" {
  <Scalar> diffr {0.0}
  <Scalar> diffg {0.7}
  <Scalar> diffb {0.0}
  <Scalar> specr {0.742455124855}
  <Scalar> specg {0.742455124855}
  <Scalar> specb {0.742455124855}
  <Scalar> shininess {127.75}
}

I change the car of the body by:

        self.npBody = self.carbody_view.find("**/body")
        material = self.npBody.findMaterial("*")
        material.setDiffuse(color)
        self.npBody.setMaterial(material)

But the color of the car become the new color blended with the original material color. How can I correct this ?

When you are using the auto-shader, you are not allowed to directly modify Material objects (because the auto-shader can’t detect this). Instead, create a new Material object:

material = Material(material)
material.setDiffuse(color) 

And use an override when you reapply it, in case you’re not applying it to the actual node that contained the original material object:

self.npBody.setMaterial(material, 1)

David

Really? glGSG::set_state_and_transform asks the shader context to re-issue the material parameters when the MaterialAttrib has changed, right?

Yes, but directly changing the parameters of the Material will not change the MaterialAttrib that references it. The only way to change the MaterialAttrib is to create a new Material object.

David

I did not use auto shader in this case.

I have tried your suggestion, the result is the same. Any idea? Or I need to upload the model for a further check ?

Thank you.

Ok, I find the answer. By default the material ambient color seems follows the diffuse color. I add also material.setAmbient(color) the blending issue is gone.