4
$\begingroup$

Is there any way to call a function on object delete or on changing count of bpy.data.objects? There is nothing suitable in bpy.app.handlers and I coudn't find something anywhere else.

$\endgroup$
4
  • 1
    $\begingroup$ could set up a scene_update handler to do this with a global dic or similar and check with something like this blender.stackexchange.com/questions/34860/… $\endgroup$
    – batFINGER
    Oct 28, 2016 at 9:56
  • $\begingroup$ Thanks. I Tried to avoid scene_update handler because it calls too often but it's ok if this is an only option... $\endgroup$
    – Anton
    Oct 28, 2016 at 10:02
  • $\begingroup$ Could use a modal timer operator, or even a draw method to flag change. The scene_update handler is AFAIK the "catch all" way to check on delete. $\endgroup$
    – batFINGER
    Oct 28, 2016 at 10:26
  • 2
    $\begingroup$ You can override the delete function. blender.stackexchange.com/questions/28932/… $\endgroup$ Mar 22, 2019 at 14:54

1 Answer 1

3
$\begingroup$

Piotr commented that you could override the Delete operator, as described for a different purpose in this answer: https://blender.stackexchange.com/a/28933/66651

I am using it to remove derivative objects. It works very well!

class delete_override(bpy.types.Operator):
    """delete objects and their derivatives"""
    bl_idname = "object.delete"
    bl_label = "Object Delete Operator"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        for obj in context.selected_objects:

            # replace with your function:
            my_function(obj)

            bpy.data.objects.remove(obj)
        return {'FINISHED'}


def register():
    bpy.utils.register_class(delete_override)

def unregister():
    bpy.utils.unregister_class(delete_override)
$\endgroup$
2
  • 1
    $\begingroup$ What if two add-ons override like this? $\endgroup$
    – Ray
    Nov 21 at 22:14
  • 1
    $\begingroup$ @Ray, good point! The later one would win. Good enough for experiments, not good enough for release quality. $\endgroup$
    – bogl
    Nov 26 at 11:37

You must log in to answer this question.

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