1
$\begingroup$

Is it possible to recursively delete a polygon and all vertices/faces connected to it, based on the index of this polygon?

In other words...knowing the index number of one polygon of a Cube, how can i delete the entire Cube mesh?

$\endgroup$

1 Answer 1

2
$\begingroup$

Yes, there are a couple of ways to do this. This one assumes you want all of the faces from the same island as the face you selected. Note that this will leave you with a mesh object that contains no mesh as it doesn't delete the object when it's done.

import bpy
import bmesh

# https://github.com/Aadjou/blender-scripts/blob/master/bmesh-get-linked-faces.py
def get_linked_faces(f):
    if f.tag:
        # If the face is already tagged, return empty list
        return []
    # Add the face to list that will be returned
    f_linked = [f]
    f.tag = True
    # Select edges that link two faces
    edges = [e for e in f.edges if len(e.link_faces) == 2]
    for e in edges:
        # Select all firs-degree linked faces, that are not yet tagged
        faces = [elem for elem in e.link_faces if not elem.tag]
        # Recursively call this function on all connected faces
        if not len(faces) == 0:
            for elem in faces:
                # Extend the list with second-degree connected faces
                f_linked.extend(get_linked_faces(elem))
    return f_linked

def del_adjacent(object, index):
    if not object or not object.type == 'MESH':
        return;

    if object.mode == 'OBJECT':
        bm = bmesh.new()
        bm.from_mesh(object.data)
    else:
        bm = bmesh.from_edit_mesh(object.data)
    
    bm.faces.ensure_lookup_table()
    bm.edges.ensure_lookup_table()

    for face in bm.faces:
        face.tag = False

    try:
        face = bm.faces[index]
    except IndexError:
        return;
    
    to_delete = get_linked_faces(face)
    # https://docs.blender.org/api/current/bmesh.ops.html#bmesh.ops.delete
    bmesh.ops.delete(bm, geom=to_delete, context='FACES')
    
    if object.mode == 'OBJECT':
        bm.to_mesh(object.data)
        bm.free()
    else:
        bmesh.update_edit_mesh(object.data)

if __name__ == "__main__":
    del_adjacent(bpy.context.active_object, 1)

If you only want to delete the specified face and the faces adjacent to it, then substitute this version of get_linked_faces:

def get_linked_faces(f):
    edges = [edge for edge in f.edges if len(edge.link_faces) == 2]
    faces = [f]
    for edge in edges:
        newfaces = [elem for elem in edge.link_faces if not elem == f]
        for elem in newfaces:
            faces.append(elem)
    return faces

In the cube example, this will leave you with one face, the one opposite the one you selected.

$\endgroup$
4
  • $\begingroup$ Thanks...in case i have 4 verts indexes? Can i remove the entire polygon? $\endgroup$
    – Thiago M.
    Commented Jan 6, 2022 at 17:11
  • $\begingroup$ Yes. You can write code to find the rest of the vertices that make up the polygon. I suggest you write a separate question for that case, as the code is a bit different. ince you're apparently a new user to Blender StackExchange, you might want to read What should I do when someone answers my question? and perhaps accept my answer. $\endgroup$ Commented Jan 6, 2022 at 17:47
  • $\begingroup$ I appreciate your help blender.stackexchange.com/questions/249321/… $\endgroup$
    – Thiago M.
    Commented Jan 6, 2022 at 18:59
  • $\begingroup$ I'll answer your new question in a few hours when I have time, if nobody else has answered it in the meantime. $\endgroup$ Commented Jan 6, 2022 at 19:14

You must log in to answer this question.

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