setColor not working?

I’m currently trying to get something to change color when it’s clicked. This is what I’ve got:

getting the right object and stuff works just fine. but coloring it just doesn’t do it:

	def __init__(self):
		self.model = loader.loadModelCopy('./data/worlds/tile.egg')
	def unhilight(self):
		print 'unhilighting: ' + str(self.x) + ', ' + str(self.y)
	def hilight(self):
		print 'hilighting: ' + str(self.x) + ', ' + str(self.y)

printing the test stuff goes just fine, but the model doesn’t seem to want to be coloured…

What color is the model already? Is it textured? It may be that the texture is making it difficult to see the color change.

Try base.toggleTexture() to disable textures, and see if that helps.

If it’s still not visibly working even without textures, see if you can post your egg file somewhere, so I can try to duplicate the problem.


base.toggleTexture() didn’t seem to help.

afaik, it has no texture, that is, it’s a ‘blank’ tile (blender default color: 0.8 grey)

I uploaded the .egg (and the .blend) file here:

Ah. This egg file has a Material on it, and the material defines an explicit diffuse color. With a Material in effect, and lighting enabled, the Material’s diffuse color overrides the object’s color, so setColor() has no visible effect.

You can either find a handle to the Material and modify that (or create a new Material that has the desired color), or you can edit the egg file to remove the “diffr”, “diffg”, and “diffb” entries from the Material, which are the same as the RGBA of the polygon anyway. With the diffuse color specification removed, the polygon color of the model will show through, and you’ll be able to setColor() on the model as expected.


wow, I would’ve never found that… thanks a lot. I’ll let you know if it worked out.

Edit: worked like a charm ^^ - thanks again!

I have an object that had a material already (from the modeling package). I “overroad” that material by making a Panda Material and setting it on the object.

I then wrote code to manipulate the Material in hopes of making the material get really dark or really light.

def ChngLVmater(self, AddorSub, Val):
        if AddorSub == 0:
            GameLevel.LandMatrl.setAmbient(Vec4(0.5-Val, 0.5-Val, 0.5-Val, 1));
            GameLevel.LandMatrl.setDiffuse(Vec4(0.8-Val, 0.8-Val, 0.8-Val, 1));
            GameLevel.LandMatrl.setEmission(Vec4(0.0-Val, 0.0-Val, 0.0-Val, 1));
            GameLevel.LandMatrl.setSpecular(Vec4(0.0-Val, 0.0-Val, 0.0-Val, 1));
        elif AddorSub == 1:
            GameLevel.LandMatrl.setAmbient(Vec4(0.5+Val, 0.5+Val, 0.5+Val, 1));
            GameLevel.LandMatrl.setDiffuse(Vec4(0.8+Val, 0.8+Val, 0.8+Val, 1));
            GameLevel.LandMatrl.setEmission(Vec4(0.0+Val, 0.0+Val, 0.0+Val, 1));
            GameLevel.LandMatrl.setSpecular(Vec4(0.0+Val, 0.0+Val, 0.0+Val, 1));

To no avail - I guess you can’t make an object get darker or ligher just by manipulating the material settings.

Could it be that I have flattenStrong() called on the object?

I wonder -

If I go back into the modeling package and re-export the model without a Material, and then just call setColor on the object…

Could that be a solution?

Shaders are set to off anyway, so I really wouldn’t need the material (I guess).

Once you have applied a material to an object, it’s too late to change the material properties and expect them to show up reliably. However, you can make a copy of the material, change the properties of the copy, then re-apply the copy to the object. (This is, of course, impossible to do once the object has been flattened away with flattenStrong().)

If an object does not have a material, or if the material it does have does not override the diffuse color, then you may change the object’s color at runtime with setColor() or setColorScale(). (This is also impossible once you have called flattenStrong().)

Note that a material is used for lighting effects; it isn’t necessarily related to shaders.


So I would be better of creating multiple materials at different colors and cycle them (and remove flattenStrong).