7
$\begingroup$

What is the best way to manage handlers in an add-on?

I am making an add-on that uses the scene_update_pre handler. In the register() function, I have added this line:

bpy.app.handlers.scene_update_pre.append(my_handler) 

The issue is how do I setup the unregister() function to remove the handler if the add-on is disabled? I can't use pop because it might mess up another add-on, correct?

Whats the best practice when making an Add-on?

$\endgroup$

1 Answer 1

10
$\begingroup$

It is simple:

def unregister():
    bpy.app.handlers.scene_update_pre.remove(my_handler)

Blender will call unregister() in the moment you untick the addon in the User Preferences.

my_handler is a function in the global scope of your script, and you append it to the scene update handlers. You can remove it in the same way, by giving a function reference to remove(). It doesn't work if you try to remove a handler callback from outside, because another script hasn't the same function.

$\endgroup$
1
  • 1
    $\begingroup$ With a draw_handler, you pass the "handle" to be removed. For app handlers, you remove the function, no handle is returned with bpy.app_handlers.scene_update_pre.append(my_function) $\endgroup$
    – patmo141
    Commented Sep 7, 2015 at 14:52

You must log in to answer this question.

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