Call Operator when Object is changed / leaves Editmode

Hi,
I would like to call an operator everytime I change an Object.
So, like I change a curve, leave the Editmode and now a I want to automatically run an operator.

Maybe I can monitor a mesh and check if something changed.
Or, and I would prefer this, everytime I leave the Editmode on a certain object, the operator is executed.

Any suggestions?

You want the depsgraph_update handler, which is called whenever data is modified or modes are switched.
https://docs.blender.org/api/current/bpy.app.handlers.html#bpy.app.handlers.depsgraph_update_post
You can filter this so it only does whatever on the correct object just by adding an active object check to your function

2 Likes

Sweet, I will check this out! Thanks!

Ok, it works, basicly…

I append a function to bpy.app.handlers.depsgraph_update_post …
But, I still try fo figure out how to check if a function is already there, or how to remove it…

    i = 0
    for handler in bpy.app.handlers.depsgraph_update_post:
        
        if handler.__name__ == "test_handler_name":
            bpy.app.handlers.depsgraph_update_post.remove(handler)
            
        i = i + 1

Ok this this worked

That works, or you can do this:

def unregister():
    ...
    if test_handler_name in bpy.app.handlers.depsgraph_update_post:
        bpy.app.handlers.depsgraph_update_post.remove(test_handler_name)

It’s good practice to have a register() and unregister() function in your scripts

Oh, I have, but I am in the middle of… finding the best practices out :wink:

This somehow isnt working when I run the script from texteditor… unregister seems to not being called

Oh yeah it won’t work from text editor, I assumed you were making a stand-alone script file. In that case, ignore me and carry on :slight_smile: