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?


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:

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:

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:

# if you make edits then you need to update at the end
  • $\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 '13 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.