Hello there,
I am a bit lost with the ways to create mesh objects with the new API (2.5x).
I know well the old API for Blender 2.4x, with Mesh or NMesh (Mesh better, NMesh faster).
The new API is seem to be easy to understand, but it is not well documented (with short example like the old API).
But no worry, the new API rock’s.
I found many way to create a simple mesh in 2.5x (2.56a), but I don’t know that they are differences between them (like performance or better initialisation).
I found at least four way to create a mesh :
- by appending a face:
me.faces.append(face) - by direct method
me.from_pydata(vertexCoords,edgesList,faceList) - by loops technique (two ways):
me.faces.foreach_set(“vertices_raw”,unpack_face_list([f[0] for f in faces]))
or
me.faces[i].vertices = vertsIndex
I think to create mesh from scratch, loopings techniques are the right way ?
Perso I use the second loop technique, but it does not appear to work.
Any idea about what is wrong ?
import bpy
nbVerts=8
vertsData=[1,-1,-1, 1,-1,1, -1,-1,1 ,-1,-1,-1 ,1,1,-1 ,1,1,1 ,-1,1,1 ,-1,1,-1]
nbEdges=13
edgesData=[0,1,0 ,1,2,0 ,2,3,0 ,3,7,0 ,4,7,0 ,5,6,0 ,6,7,0 ,0,3,0 ,4,5,0 ,1,5,0 ,2,6,0 ,0,4,0 ,1,4,0]
nbFaces=5
facesData=[(0,1,2,3),(4,7,6,5),(1,5,6,2),(2,6,7,3),(4,0,3,7)]
scn = bpy.data.scenes[0]
mesh = bpy.data.meshes.new('test')
mesh.vertices.add(nbVerts)
for i in range(nbVerts):
mesh.vertices[i].co = (vertsData[i*3],vertsData[i*3+1],vertsData[i*3+2])
mesh.edges.add(nbEdges)
for i in range(nbEdges):
mesh.edges[i].vertices = [edgesData[i*3],edgesData[i*3+1]]
mesh.faces.add(nbFaces)
for i in range(len(facesData)):
mesh.faces[i].vertices = facesData[i]
obj = bpy.data.objects.new("test",mesh)
scn.objects.link(obj)
This way crash Blender.