Search code examples
pythontexturesgame-engineblender

Getting an instance of KX_PolygonMaterial in Blender


I've got a question concerning using Python in Blender's Game Engine.

Before I start I want to state that I'm trying to change the color of an object in Blender's game engine. To do this, I'm attempting to find a way to update the texture of the object (I basically want two or three states, red, (yellow), green).

What I'm doing right now is:

scene = GameLogic.getCurrentScene();    
pingMeter = scene.objects['Ping Meter'];
mesh = pingMeter.meshes;
materials = mesh[0].materials;
material = materials[0];

However, when I do print(material.__class__.__name__) it outputs KX_BlenderMaterial. Shouldn't I be getting KX_PolygonMaterial if I'm running the Blender Game Engine? Is there anyway to change color or texture with KX_BlenderMaterial because I can't find anything in the documentation. Can I get an instance of KX_PolygonMaterial out of the code above?

...or should I just take a different approach all together?

Thanks!

EDIT: I'm using Blender 2.65 which uses Python 3 in case anyone is wondering.


Solution

  • I determined a way to change the color of an object's material in Python with Blender 2.65. Before my approach above I simply tried something like:

    scene = GameLogic.getCurrentScene();
    pingMeter = scene.objects['Ping Meter'];
    red = mathutils.Vector((1.0, 0.0, 0.0, 1.0));
    pingMeter.color = red;
    

    However, this produced no change to the color of the material (should've turned red, and the object was properly lit as well). I found that there has to be an option checked under the materials menu of the object which is Object Color. After checking this box the ping meter successfully change to red in the game.

    Additionally, another method I attempted was to get the mesh of the object, get the material of the mesh, and change each vertex color of it. This did not work either, however I speculate that if the Vertex Color Paint option is checked in the materials menu that it would. I have not tested this though.