change colour using Python?

Hey guys,

how can I change colour using python script? I would like to create cube, that will change colour according to value of other variable. Is it possible?

Thanks!

Hi,

you can change colors of the face’s vertices. You need to add a colorlayer and will need to add a material linked to it and change the material mode to VColPaint. But, unfortunately, I do not know how to “insert key” for that colors (Inserting RGB key for the material does not work), such that if you load different colors for each frame, all the frame have the colors of the last frame. Actually, I got the same problem if I use vertex painting; I do not know how to make keyframes with different vertex colors for each frame.

  mesh.addColorLayer('Col')
  faces = mesh.faces
  for f in faces:
    for i in range(0,len(f.verts)):
      f.col[i].r = int(colorList[f.verts[i].index][0])
      f.col[i].g = int(colorList[f.verts[i].index][1])
      f.col[i].b = int(colorList[f.verts[i].index][2])
  mesh.update()

  mat = Material.New('Material')  # create new material
  mat.rgbCol = [1.0, 1.0, 1.0]
  mat.setAlpha(1.0)
  mat.setRef(0.8)
  mat.setSpec(0.5)
  mat.setHardness(50)
  mat.setMode("VColPaint")

heya,

thank you for the reply. It looks kinda difficult. I have another idea. Would it be possible to create two materials in advance and just apply them to my cube through python script?

Basically I am checking value of my variable all the time - if it is 0 I want my cube to be green and if it is 1 I want it to be red.

Thanks!

If all your vertices have the same color, you can do that by changing the color of the material you are using. the material property is setRGBCol(). You will have to fix the color of each key frame with mat.insertkey(Material.RBG), where mat is your material. Then there will be a transition between the two colors.

Sorry, it is insertIpoKey, not insertkey…

You may want to specify what API you are targeting is this for 2.49 or 2.5?

sorry my bad, it is 2.49b and in Blender Game Engine.

Thank you for responses, I also found this code:


<b>###### get and set vertex color
</b>

<b>
</b>

<b># get controller</b>

controller = GameLogic.getCurrentController()

<b># get object the controller is attached to</b>

obj = controller.owner

<b># get the 1st mesh</b>

mesh = obj.meshes[0]

<b># get the first vertex of the first material</b>

vert  = mesh.getVertex( 0, 0)

<b># set the color of this vertex to red</b>

vert.color = [ 1.0, 0.0, 0.0, 1.0]

and I can get color and set it, however my cube still remains black, even it should be red (get function say it). What I am doing wrong?

Thank you!