Page MenuHome

Addons: ModuleLoader utility class

Authored by Andrew Williams (sobakasu) on Feb 1 2019, 7:50 AM.



Here is a utility class called ModuleLoader which can be used in blender addons to simplify the code concerned with loading and reloading modules, and registering and unregistering classes.

Diff Detail

Event Timeline

Andrew Williams (sobakasu) created this revision.
This comment was removed by Andrew Williams (sobakasu).
Andrew Williams (sobakasu) retitled this revision from Addons: module loader utility class to Addons: ModuleLoader utility class.

Updated to iterate registerable classes in the order that they were defined, which is sometimes required when they have dependencies.

  • added some documentation
  • added a 'LoadedModule' class
  • added register()/unregister() which registers/unregisters classes and also calls register()/unregister() on submodules
  • simplified constructor to just take module name, so caller doesn't need to import sys.
  • fix to only register classes defined by each module and not imported ones
  • constructor now takes package argument
  • added 'include_main' option to register/unregister, to optionally skip main module (needed for rigify legacy mode)
  • added support for dynamically generated classes (used by rigify)

If we include this, I'd rather this be exposed as a 3rd party module addons/modules/ for eg.

I find this kind of abstraction complicates debugging and hands off otherwise fairly simple (if a little tedious) logic off to someone else. Then any problems with order-of-initialization issues are not at all simple to investigate and resolve.

Recursively scanning modules is going to slow down Blender's startup too - although it's hard to say by how much.

If people like to use *magic* to do basic operations like loading modules and registering classes, we can support it, but I wouldn't promote this as an official feature.

Would rather not hide this away from developers.
Having a class lists register isn't so complicated and any errors can be more easily debugged.