5
$\begingroup$

I've learned that an object may contain multiple meshes. I want to export all vertices from all meshes. How do I iterate through them?

Also, is there a simple way to know in advance the total count? Other than doing 2 passes?

$\endgroup$
4
  • 1
    $\begingroup$ You could try selecting everything in edit mode (with python), then getting the vert count from total_vert_sel. $\endgroup$
    – gandalf3
    Jan 10, 2014 at 1:56
  • 1
    $\begingroup$ I'm not sure about the "multiple meshes" part, but I think that for vert in bpy.data.objects['MyObject'].data.vertices should get everything (unless I'm missing something). $\endgroup$
    – gandalf3
    Jan 10, 2014 at 2:00
  • 1
    $\begingroup$ An object cant contain multiple meshes, ... unless your talking about something totally different to bpy.types.Object - can you explain? $\endgroup$
    – ideasman42
    Jan 10, 2014 at 6:50
  • $\begingroup$ Where can I learn the basic data-structure of Blender? $\endgroup$
    – P i
    Jan 10, 2014 at 13:49

1 Answer 1

8
$\begingroup$

A mesh may consist of loose parts the following script will export all vertices and faces. The length is determined by pythons len() you would need to change the print statements to writing into a file.

Uncomment the dump method # dump(obj.data) to find out more on available properties you're interested in.

If you need also to get the edges of the mesh you could have a look at Python script to determine XYZ of tip of cone.

import bpy

def dump(obj, level=0):
   for attr in dir(obj):
       if hasattr( obj, "attr" ):
           print( "obj.%s = %s" % (attr, getattr(obj, attr)))
       else:
           print( attr )


#obj = bpy.data.objects["Cube"]  # particular object by name
obj = bpy.context.scene.objects.active # active object

# dump(obj.data)
mesh = obj.data
print("# of vertices=%d" % len(mesh.vertices))
for vert in mesh.vertices:
    print( 'v %f %f %f\n' % (vert.co.x, vert.co.y, vert.co.z) )

print("# of faces=%d" % len(mesh.polygons))

for face in mesh.polygons:
    print('face')
    #dump(face)
    for vert in face.vertices:
        print(vert)

Output format:

# of vertices=8
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 0.999999 1.000000
v 0.999999 -1.000001 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
# of faces=6
face
0
1
2
3
face
4
7
6
5
face
0
4
5
1
face
1
5
6
2
face
2
6
7
3
face
4
0
3
7
$\endgroup$
1
  • $\begingroup$ Came here for something different, but found what I was searching. If you want it shorter we can use list comprehension: print(f"vertices = {[[p for p in v.co] for v in mesh.vertices]}") for the vertices (points that make up the faces) and print(f"indices = {[[p for p in poly.vertices] for poly in mo.polygons]}") for the indices that make up a face. $\endgroup$
    – Pascal
    Jan 16 at 10:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .