Page MenuHome

unregister() never called on Run Script and F8
Closed, ArchivedPublic

Description

--- Operating System, Graphics card ---
Windows 7

--- Blender version with error, and version that worked ---
2.69 RC1 and before

--- Short description of error ---
Re-running a script does register it again, but unregister() is never called - is that how it is supposed to be?

Run the UI menu template multiple times for instance, and you'll end up with a bunch of "Custom Menu" entries in the info header. F8 gets rid of them however.

In other cases, F8 doesn't help, e.g. if you need to remove a draw handler and modal timer. A restart of blender is the only way.

Event Timeline

codemanx edited a custom field.Oct 10 2013, 2:32 AM

The F8 update works by removing custom bpy.types and reimporting the modules. This runs the conditional at the bottom:

if __name__ == "__main__":
register()

For class registration this usually works fine, because re-registering menu classes etc. replaces the existing bpy.types. However, for draw functions appended to draw lists (like the INFO_HT_header extension in the UI menu template) this does not work. For replacing these functions they have to be explicitly removed using unregister.

I can't think of an easy way to accomplish this - modules don't get notified of unloading in python by default. It might also make template scripts too complicated, these are intended as simple examples and should not have too much confusing bookkeeping code imo. If really needed, a workaround might be using the python atexit package: http://stackoverflow.com/questions/570636/detect-when-a-python-module-unloads

For addons the register/unregister procedure can be implemented in a clean way, because they support the register/unregister functions. When an addon is unloaded it's unregister function will be called, allowing you to remove additional function pointers as shown in the template scripts.

This is definitively not a bug! Run script does exactly what it says, it runs the script! If you want to call unregister(), add it to the 'if __name__ = "'main":' block…

As for F8, it is only designed to reload installed addons/ui scripts/etc., not any kind of random py file.

Bastien Montagne (mont29) changed the task status from Unknown Status to Archived.Oct 13 2013, 3:25 PM

it can't be added to "if __name__ == 'main':" block! It will throw an error as the given classes don't exist (as they are not global)