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.
Details
Diff Detail
- Repository
- rB Blender
Event Timeline
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/blender_module_auto_loader.py 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.