To get all polygons adjacent to the desired one, we can use the “bmesh” module, which provides access to all mesh data in Blender.
First, let’s import the bmesh module, and initialize the interface to the active mesh data using the “from_edit_mesh” method.
The active mesh must be in edit mode.
1 2 3 4 5 |
import bpy import bmesh obj_data = bpy.context.object.data bm = bmesh.from_edit_mesh(obj_data) |
Get the currently selected face by checking the “select” property of all faces of the mesh:
1 |
selected_face = next((face for face in bm.faces if face.select), None) |
Let’s define a function that, given a polygon passed to it, returns a list of all polygons that are nearby to it.
1 2 3 4 5 |
def nearby_faces(face): faces = [] for vert in face.verts: faces.extend(vert.link_faces) return faces |
We cycle over all the vertices of the face and got all the faces that contain these vertices.
Now we can call our function, pass the currently selected polygon to it, and select all the polygons that will be returned.
1 2 3 4 |
faces_to_select = nearby_faces(selected_face) for face in faces_to_select: face.select = True |
To see the resulting selection, apply changes to the mesh data.
1 |
bmesh.update_edit_mesh(obj_data) |
Full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import bpy import bmesh def nearby_faces(face): faces = [] for vert in face.verts: faces.extend(vert.link_faces) return faces obj_data = bpy.context.object.data bm = bmesh.from_edit_mesh(obj_data) selected_face = next((face for face in bm.faces if face.select), None) if selected_face: faces_to_select = nearby_faces(selected_face) for face in faces_to_select: face.select = True bmesh.update_edit_mesh(obj_data) |