For example, if I subdivided a square face so there were now 4 faces, how I do iterate through those 4 faces in a Blender add-on?

  • Could you elaborate on the question. Do you mean : you want to map the newly created faces on the old face, which was subdivided? If yes, I don't think that kind of history would be maintained in any of the built-in objects. If you simply want to iterate through the faces, you can do that through bmesh.faces iterator. Here, you won't get the face that was subdivided but the new 4 faces that were the result of the subdivision. – Blender Dadaist Oct 11 at 14:56
  • @BlenderDadaist I would like the second option; I simply want to iterate through the new faces. – Maria Oct 11 at 15:09
up vote 0 down vote accepted

Here is the code to iterate through the faces of the selected object:

import bmesh
import bpy

obj = bpy.context.object
me = obj.data

if me.is_editmode:
    bm = bmesh.from_edit_mesh(me)
else:
    bm = bmesh.new()
    bm.from_mesh(me)

for face in bm.faces:
    #Your logic here
    print(face.index)

Bmesh approach

Using the bmesh operator bmesh.ops.subdivide_edges(..) with cuts = 1 and use_grid_fill = True will produce the result of subdividing a quad face into four.

Test script, run in edit mode, subdivides face 0, prints the four newly added faces.

import bpy
import bmesh
context = bpy.context
ob = context.edit_object
me = ob.data

bm = bmesh.from_edit_mesh(me)

face = bm.faces[0]
ret = bmesh.ops.subdivide_edges(bm, edges=face.edges[:], cuts=1)
for f in ret['geom']:
    # new geometry
    if isinstance(f, bmesh.types.BMFace):
        print(f)

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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