Vertex color changes in game

I want to see the changes of vertex colors live in the game engine.
The following code changes the vertex colors of an OB named square1 and the print statements reflect the changes when the script is attached to an always sensor with a pulse but the changes do not appear until after I exit the game engine. How do I tell the game engine or the object to reflect these changes in game?


import Blender
square1 = Blender.Object.Get ('square1') 
mesh1 = Blender.Mesh.Get (square1.data.name)
mesh1.vertexColors = 1
for i in range(4):
mesh1.faces[0].col[i].r = mesh1.faces[0].col[0].r + 1
mesh1.faces[0].col[i].g = mesh1.faces[0].col[0].g + 1
mesh1.faces[0].col[i].b = mesh1.faces[0].col[0].b + 1
 
print mesh1.faces[0].col[i].r
print mesh1.faces[0].col[i].g
print mesh1.faces[0].col[i].b

Does Blender.Redraw() work for the GE?

Blender.Redraw() updates the user interface and there are no changes of the vertex colors in the game engine

I was making same tests a long time ago. I think it depended on “Use Blender Materials” settings and material settings (VCol on and if there was a texture). But I dont remember clear how it was - try to make some test.

Anyway, Blender module is only for Blender API, not BGE API, so it will be not functional in your game (my opinion).

Some of the Blender module runs in the game. I know that Blender.Run() works

Thank you for responses. Using the GameLogic module I can now see the vertex colors change in the game engine. After using the code found in post: http://blenderartists.org/forum/showthread.php?t=103531 I came up with the following.


import GameLogic
o = GameLogic.getCurrentController().getOwner()
m = o.getMesh()
for i in range(m.getVertexArrayLength(0)):
 v = m.getVertex(0,i)
 r = v.color[0]
 if r >= 1 :
  r=0
 g = v.color[1]
 if g >= 1 :
  g=0
 b = v.color[2]
 if b >= 1 :
  b=0
 v.color = [r+.1,g+.1,b+.1,1]
 print "rgb",r,", ",g,",",b 


It should run, if you have Blender installed. But if you are making a game for people that dont have Blender installed, it will not work.

It is the same as if you will make a standalone python script, which will display time. In this script you will import time module from Python. So it will crash on all computers, which dont have Python language installed (you will not even open it on these). - These are my experiences, I may be wrong, so wait for other answers. -

Thanks! I got it working using the GameLogic module. I also found the GameLogic API. It makes sense that the Blender module is more for the 3D studio and the GameLogic module is for the game engine.
The modified code:


import GameLogic
o = GameLogic.getCurrentController().getOwner()
m = o.getMesh()
for mc in range(m.getNumMaterials()):
 for i in range(m.getVertexArrayLength(0)):
 v = m.getVertex(mc,i)
 r = v.color[0]
 if r >= 1 :
  r=0
 g = v.color[1]
 if g >= 1 :
  g=0
 b = v.color[2]
 if b >= 1 :
  b=0
 
 v.setRGBA([r+.01, g+.01, b+.01, 1.0])

Hope this helps anyone else trying to set vertex colors dynamically in the game engine

Nice python script. I will save it for later usage. Thanks :slight_smile:

In new apricot builds this script isn’t working. Is there a way to fix this?