1
$\begingroup$

I'm creating a plugin for Blender 3.3. At the moment I'm storing in a list the IDs of some Faces for later use, and I would like to subscribe to an event when a Face is deleted (or subdivided or any other event that would make the original Face disappear) so I can delete the original ID's Face from that list.

What would be the best approach to do that?

$\endgroup$

1 Answer 1

2
$\begingroup$

You can subscribe to the depsgraph_update_post application handler to listen to changes in your mesh.

enter image description here

Here's an example script that prints out which face has been deleted. It is not perfect but you can use it and continue to tinker with it. It is still limited and you have to be in Edit Mode before you run the script. It basically checks if the vertices have changed to know which face has been deleted.

import bpy
import bmesh

print("START =====")

obj = bpy.context.object
mesh = obj.data
bm = bmesh.from_edit_mesh(mesh)

bpy.app.handlers.depsgraph_update_post.clear()

faces_curr = {}
faces_prev = {}

def update_face_vertices(face_sets, face, i):
    list = [v.index for v in face.verts]
    face_sets[i] = set(list)

def set_new_face_vertices():
    faces_curr.clear()
    for i, face in enumerate(bm.faces):
        update_face_vertices(faces_curr, face, i)

def handle_geometry_changed(scene):
    set_new_face_vertices()
    bmesh.update_edit_mesh(mesh)
    for index in faces_curr:
        if faces_curr[index] != faces_prev[index]:
            print("face with index deleted:", index)
            break
    faces_prev.update(faces_curr)

bpy.app.handlers.depsgraph_update_post.append(handle_geometry_changed)

set_new_face_vertices()
faces_prev.update(faces_curr)
$\endgroup$
1
  • 1
    $\begingroup$ Great, thanks so much! That will point me in the right direction $\endgroup$
    – Walrfi
    Commented Feb 28, 2023 at 9:39

You must log in to answer this question.

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