Trying to build mesh, cannot set vertices.

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
caibbor
Posts: 0
Joined: Thu Sep 20, 2012 11:43 pm

Trying to build mesh, cannot set vertices.

Post by caibbor »

I've been trying to assign vertex indices to the polygons I create in my mesh, but I'm doing something wrong. Also, blender crashes after try to mesh.Update()

Using Blender 2.63

Code: Select all

>>> 
>>> name='newmesh'
>>> verts=[(-1,1,0),(1,1,0),(1,-1,0),(-1,-1,0),(0,1,1),(0,-1,1)] # each unique vertice in the mesh
>>> faces=[(0,1,2,3),(1,2,5,4),(0,3,5,4),(0,1,4),(2,3,5)] # referencing vertices by index
>>> m = bpy.data.meshes.new(name)
>>> o = bpy.data.objects.new(name, m)
>>> bpy.context.scene.objects.link(o)
bpy.data.scenes['Scene']...ObjectBase

>>> m.vertices.add(len(verts)) 
>>> for i in range( len( m.vertices ) ):
... 	m.vertices[i].co = verts[i]
... 
>>> m.polygons.add(len(faces))
>>> m.polygons[0].vertices = (0,1,2)
>>> for i in range( len( m.polygons ) ):
... 	m.polygons[i].vertices = faces[i]
... 
>>> # lets check the valus of all our faces
>>> for i in range( len( m.polygons ) ):
... 	m.polygons[i].vertices
... 
bpy.data.meshes['newmesh'].polygons[0].vertices
bpy.data.meshes['newmesh'].polygons[1].vertices
bpy.data.meshes['newmesh'].polygons[2].vertices
bpy.data.meshes['newmesh'].polygons[3].vertices
bpy.data.meshes['newmesh'].polygons[4].vertices

>>> # wtf? they don't have values?
>>> m.update(calc_edges=True) # CRASH!

caibbor
Posts: 0
Joined: Thu Sep 20, 2012 11:43 pm

Post by caibbor »

I've been continuing to research and hack at the blender API. I've got the mesh building now, and I'm using loops. they are.... weird. I don't get why it's so complicated under the hood. Anyway, here's my current solution.

Code: Select all

name='newmesh'
verts=[(-1,1,0),(1,1,0),(1,-1,0),(-1,-1,0),(0,1,1),(0,-1,1)] # each unique vertice in the mesh
faces=[(0,1,2,3),(1,2,5,4),(0,3,5,4),(0,1,4),(2,3,5)] # referencing vertices by index
m = bpy.data.meshes.new(name)
o = bpy.data.objects.new(name, m)
bpy.context.scene.objects.link(o)
m.vertices.add(len(verts)) 
for i in range( len( m.vertices ) ):
	m.vertices[i].co = verts[i]

# put all the vertices in each face into the MeshLoop
loop=0
for f in range( len(faces) ):
	for v in range( len(faces[f]) ):
		m.loops.add( 1 )
		m.loops[loop].vertex_index=faces[f][v]
		loop += 1

# assign the vertex indices from the loops to the faces
m.polygons.add(len(faces))
start = 0
for i in range( len(faces) ):
	m.polygons[i].loop_start=start
	m.polygons[i].loop_total=len( faces[i] )
	start += m.polygons[i].loop_total

m.update(calc_edges=True,calc_tessface=True)

Post Reply