how to create and vertex a mesh with python?

Hello again!
I need to do vertex coloring of a mesh created with python, but
I am not sure how to do it. I found this thread:
https://blenderartists.org/forum/viewtopic.php?t=19426
But there is some missing, namely the part that creates
creates the mesh. So, can anyone show me how to create
a mesh and add vertex (painting) coloring to it? (I will be creating
500 or so meshes, and so I would like to automate all of it.)
(using 2.36)
I’ve gotten this far:


mesh = NMesh.New()
p = []

d = 16
zCo = 0

# make the vertices
p.append( NMesh.Vert(  d,  d, zCo ) )
p.append( NMesh.Vert( -d,  d, zCo ) )
p.append( NMesh.Vert( -d, -d, zCo ) )
p.append( NMesh.Vert(  d, -d, zCo ) )
  

# add the vertices to the mesh
for i in range( 0, 4 ):
   mesh.verts.append( p[i] )
 # make a face
f = NMesh.Face()
for i in range( 0, 4 ):
   f.v.append( p[i] )

# add face to mesh
mesh.faces.append(f)



Thanks!
–Squirrel

Ok, I’ve gotten most of it figured out, except one
crucial point, how do I set the TexFace button
using python?

I looked in the 2.0 docs, and there is a has_uv
boolean, but I don’t see it in the 2.36 docs. Did
they take it out? or (hopefully) just move it?

Help much appreciated.

Thanks
Squirrel

Ah, well, in the spirit of this conversation with myself,
not a rarity I assure you, I will continue to post my
findings.

I added color to the face with


f = NMesh.Face()
#added verticies

f.col.append( NMesh.Col( 0,200, 0, 255 ) )

added the face to the mesh, and finally
set the vertex colors with


mesh.hasVertexColors(1)

Now, once I add the mesh to the scene, it is
the default grey, but if I hit ‘f’ it becomes the
green I so envy. Which is exactly what I need.
The problem is that I need to hit the ‘f’ key to
make it work. In the buttons window, it unsets
the vertCol button and sets the TexFace button.

I tried every combination of
hasFaceUV(1)
hasVertexUV(1)
hasVertexColors(1)

to no avail. Any ideas?

thanks
– Squirrel

Did you try Window.redraw() ?

LetterRip

Woohoo, thanks for the reply.
I tried the Window.Redraw() out of desperation,
but nothing new.

Here is my code:


# draws a box, using the already created mesh
# at pos, zCo with side len d

from Blender import NMesh, Window

def drawBox( mesh, zCo, d ):
   p = []

   d = d/2.0
   # make the vertices
   p.append( NMesh.Vert(  d,  d, zCo ) )
   p.append( NMesh.Vert( -d,  d, zCo ) )
   p.append( NMesh.Vert( -d, -d, zCo ) )
   p.append( NMesh.Vert(  d, -d, zCo ) )
   
   # add the vertices to the mesh
   for i in range( 0, 4 ):
      mesh.verts.append( p[i] )

   # make a face
   f = NMesh.Face()
   for i in range( 0, 4 ):
      f.v.append( p[i] )
   f.col = []
   f.col.append( NMesh.Col( 0, 200, 0, 255 ) )
  # f.mode |= NMesh.FaceModes['LIGHT']


   # add face to mesh
   mesh.faces.append(f)

   mesh.hasVertexColours(1)
   #mesh.hasFaceUV(1)
 
# end drawBox
    
me = NMesh.New()
drawBox( me, 0, 4)

obj = NMesh.PutRaw( me, "Bob" )

As is, the vertCol button is set (ie says delete, not
make) and the others (edges, texFace, and sticky)
are all unset (ie, says make). The mesh is grey,
but when I hit “f” in the 3d window, the mesh becomes
green, and VertCol is unset and TexFace is set, and
the mesh is green, as it should be.

So theoretically, removing the “#” before the
“mesh.hasFaceUV(1)” should set TexFace and it should
be green. But it turns out white- not green. Why? and is
this normal? How do I work around it?

Thanks
– Squirrel

Ok, just for completeness, I will add to this that
it does work if you set the vertCol button, it just
doesn’t show up unless you hit p from a camera
view. Or something like that. It seems very finiky.

I also noticed that the topic on this it wrong. It must
have been early or something. Wierd, so I will add
the key words: “color vertex mesh python with”

Don’t forget to tie your shoes.
– Squirrel

So far your code is good, but you need to set the mesh’s material to vcolLight if you want it to show up in the green color during normal renders and viewing. Otherwise it will only be green in the game engine or face select mode.