5
$\begingroup$

I'm quite new to Blender and I'm trying to write my first Blender/Python script. I'm implementing an algorithm that is looking for the lowest (z-value) laying neighboring face for each mesh face. I have been trying to find a way to efficiently find the adjacent faces of a given face, but so far without any luck. Is there a built-in function for that in Blender? Or do I have to loop through all faces and check if they share edges with the current face?

Cheers!

$\endgroup$

1 Answer 1

7
$\begingroup$

The bmesh module

Suggest for working with a mesh use the bmesh module.

Test script. Assumes a face is selected and active in edit mode. For each edge in a face, look at its linked faces, and select if other face.

import bpy
import bmesh

ob = bpy.context.edit_object
me = ob.data
bm = bmesh.from_edit_mesh(me)
# have a face selected and active
face = bm.select_history.active

link_faces = [f for e in face.edges
              for f in e.link_faces if f is not face]
# select them              
for f in link_faces:
    f.select = True
# update mesh    
bmesh.update_edit_mesh(me)
me.update()

Growing face selection by verts, (uses a set, includes original)

# from verts instead.
link_faces = set(f for v in face.verts
             for f in v.link_faces)
# select them              
for f in link_faces:
    f.select = True

To find vert with lowest global z in linked faces. Could grab the minimum. I have started to lean towards sorting, since all or any of the rest may have same min as first.

verts = set(v for f in link_faces for v in f.verts)
# order by global z
mw = ob.matrix_world
sorted_verts = sorted(verts, key=lambda v: (mw * v.co).z)
$\endgroup$

You must log in to answer this question.

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