Mesh color python - Still need help

Hey,

I´m trying to change the color of a mesh in 2.49b.

import GameLogic as g

get controller

cont = g.getCurrentController()

get object controller is attached to

own = cont.owner

get the mesh

mesh = own.meshes[0]

get the number of materials on the mesh

mats = mesh.numMaterials

cycle through all of the materials

for i in range(0, mats):

get array length of the material

array = mesh.getVertexArrayLength(i - 1)

loop through array

for vertNum in range( 0, array):

get vertex

vert = mesh.getVertex(0, vertNum)

#Change the color
color = [0.5, 0.5, 0.5, 1.0]

change vertex color

vert.setRGBA(color)

Won’t it work because of the older blender version?
I attached the .blend.

Attachments

color change.blend (129 KB)

I don’t know, but in the newer Blender 2.55 you can use object.color.

You could always just place the object in another layer, with its own light, then change the color of the lightto alter the color of the mesh…

Try this:


def main():
    cont = GameLogic.getCurrentController()
    own = cont.owner
    
    mesh = own.meshes[0]
    
    # Iterates through all vertices:
    for mat in range(mesh.numMaterials):
        for v_index in range(mesh.getVertexArrayLength(mat)):
            vertex = mesh.getVertex(mat, v_index)
            # ... eg: colour the vertex red.
            vertex.colour = [1.0, 0.0, 0.0, 1.0]

main()

… as described in the BGE API

@Gamemaster: You are accessing a non-existing material (-1) otherwise it would be fine

Ok, I have another question-> why, when I attach an object to an armature and create just one vertex group(even without assigning vertexes to it), the applied vertex color(with the above script) of the object is disabled/deleted automatically, and when I delete the created vertex group the script works again?

P.S. It actually applies the color. When I trace the “vertex.color” after applying the script, it show, that all the vertexes has the new color, but visually the color of the mesh is not changed.

P.S.P.S. Never mind, I find a workaround! :slight_smile:

Hey thanks a lot all. It works ^^

*edit

Oh wait, it doesn’t work when I parent it to an armature. Any way to fix this?

There is no fix, this is a bug in 2.49.
…but, I found a workaround:

  1. Duplicate the mesh wich color you wnat to change.
  2. Unparent the duplicated object from the armature, but do not delete the vertex groups in it.
  3. Apply the above script to change the color of the duplicate object.
  4. Use instantReplaceMesh() on the original object to replace its mesh with the duplicate mesh,wich color is already changed.

In other words to workaround this bug you need to apply the color first to an unparented(from the armature) mesh, then replace the original mesh with the duplicated mesh.

Oh wow that sucks -.-

It’s pretty complicated to make that in my scene… but thanks anyway. :wink: