me=Nmesh.GetRaw('mesh') and coloring faces

I’m playing with Blender python scripts for the first time. I did the tuts on jmsoler, and searched the knowledge base and google … but I’m wondering how to do the following:
Take a mesh that I have created in Blender and color faces on it. Should be simple, but …

Just to keep it really simple, lets assume that the mesh is a plane with 9 vertices and 4 square faces:

v1 ---- v2 ---- v3
| face 1 | face 2 |
v4 ---- v5 ---- v6
| face 3 | face 4 |
v7 ---- v8 ---- v9

Before the script runs, I want the mesh to be entirely white. At the beginning of the script, I want to set a color variable for each face to red or white. After the script runs the faces are colored according to this variable.

Does anybody know of a tut that explains this? Maybe somebody could do a quick example? I’d really appreciate it.

I’m kinda thinking out loud here so bear with me. I think you would do something like this:

first create a couple of materials

mat1 = Material.New(‘red’)
mat2 = Material.New(‘white’)

then give them color

mat1.setRGBCol(1,0,0)
mat2.setRGBCol(1,1,1)

then add the materials to the mesh’s material list

meshname.addMaterial(mat1)
meshname.addMaterial(mat2)

finally have each face reference it’s material from the list (I am least confident in this part)

face1.materialIndex(0)
face2.materialIndex(1)
face3.materialIndex(0)
face4.materialIndex(1)

Of course, I have never tried this before so I could be way off. Brute force and ignorance have got me to where I am today.

Thanks for the suggestions. But, I tried them and still nothing. Maybe I should just post my code:

import Blender
from Blender import NMesh, Material
me=NMesh.GetRaw()

#Create vertices

v=NMesh.Vert(1.0,0.0,0.0)
me.verts.append(v)
v=NMesh.Vert(1.0,1.0,0.0)
me.verts.append(v)
v=NMesh.Vert(0.0,1.0,0.0)
me.verts.append(v)
v=NMesh.Vert(0.0,0.0,0.0)
me.verts.append(v)

#Create face

f=NMesh.Face()
f.v.append(me.verts[0])
f.v.append(me.verts[1])
f.v.append(me.verts[2])
f.v.append(me.verts[3])
me.faces.append(f)

NMesh.PutRaw(me, “plane”,1)

mat1=Material.New(‘red’)
mat1.setRGBCol(1,0,0)
me.addMaterial(mat1)

Blender.Redraw()

The face1.materialIndex(0) generated an error, so I took it out.

In the end, I decided to only tryi to get ONE colored face, and that does not even work. The material is created, but does not show up on the face.

I’ve looked through the documentation at blender.org, but there aren’t many examples. That is what I need … sigh.

I got the code to work by adding me.update(). As far as the rest goes … I’m still working on it. I will post the complete solution when I figure it out in case someone else is interested.

Thanks lowcash