16
$\begingroup$

Preferable in object mode.

I have a mesh I know is made of quads only. I want my script to iterate through each quad and do something with the coordinates of each of the four vertices.

I have read the API documentation but I don't understand how faces are stored. I think what I want can be accomplished by looking at bpy.types.Mesh.polygons but the type MeshPolygon does not reference vertices directly, it references loops, and loops references a vertex and an edge, and edges, finally, references vertices.

For me this looks like a lot of guesswork to figure out what vertices form the polygon.

How to go from polygon to vertices?

$\endgroup$

1 Answer 1

16
$\begingroup$

The text editor has a useful template that may interest you:
Text Editor > Templates > Python > BMesh Simple

Each polygon stores a reference to the indices of the vertices it's made from.

object mode

import bpy

# obj = bpy.data.objects['Cube']  <-- depending how you want to access it.
obj = bpy.context.active_object

for f in obj.data.polygons:
    for idx in f.vertices:
        print(obj.data.vertices[idx].co)

or manipulate the mesh directly (this will affect all objects that reference this mesh). Meshes and Objects are different things.

import bpy

data = bpy.data.meshes['Cube']

for f in data.polygons:
    for idx in f.vertices:
        print(data.vertices[idx].co)

edit mode

import bpy
import bmesh 

obj = bpy.context.active_object
bm = bmesh.from_edit_mesh(obj.data) 

for f in bm.faces:
    for v in f.verts:
        print(v.co)

# if you make edits then you need to update at the end
bmesh.update_edit_mesh(obj.data)
$\endgroup$
1
  • $\begingroup$ For some reason I missed that one. Thank you. Now I can iterate the faces of the object and make the script behave according to the coordinates of each vertex of each face. $\endgroup$ Sep 9, 2013 at 11:28

You must log in to answer this question.

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