Changeset View
Changeset View
Standalone View
Standalone View
release/scripts/modules/addon_utils.py
| Show First 20 Lines • Show All 327 Lines • ▼ Show 20 Lines | def enable(module_name, *, default_set=False, persistent=False, handle_error=None): | ||||
| # Split registering up into 3 steps so we can undo | # Split registering up into 3 steps so we can undo | ||||
| # if it fails par way through. | # if it fails par way through. | ||||
| # disable the context, using the context at all is | # disable the context, using the context at all is | ||||
| # really bad while loading an addon, don't do it! | # really bad while loading an addon, don't do it! | ||||
| with RestrictBlend(): | with RestrictBlend(): | ||||
| # BEGIN TEST-ONLY | |||||
| import _bpy as _hack_module | |||||
| _hack_module._addon_current_hack = module_name | |||||
| # END TEST-ONLY | |||||
| # 1) try import | # 1) try import | ||||
| try: | try: | ||||
| mod = __import__(module_name) | mod = __import__(module_name) | ||||
| mod.__time__ = os.path.getmtime(mod.__file__) | mod.__time__ = os.path.getmtime(mod.__file__) | ||||
| mod.__addon_enabled__ = False | mod.__addon_enabled__ = False | ||||
| except Exception as ex: | except Exception as ex: | ||||
| # if the addon doesn't exist, dont print full traceback | # if the addon doesn't exist, dont print full traceback | ||||
| if type(ex) is ImportError and ex.name == module_name: | if type(ex) is ImportError and ex.name == module_name: | ||||
| print("addon not found: %r" % module_name) | print("addon not found: %r" % module_name) | ||||
| else: | else: | ||||
| handle_error(ex) | handle_error(ex) | ||||
| if default_set: | if default_set: | ||||
| _addon_remove(module_name) | _addon_remove(module_name) | ||||
| _hack_module._addon_current_hack = None # TEST ONLY | |||||
| return None | return None | ||||
| # 2) try register collected modules | # 2) try register collected modules | ||||
| # removed, addons need to handle own registration now. | # removed, addons need to handle own registration now. | ||||
| # 3) try run the modules register function | # 3) try run the modules register function | ||||
| try: | try: | ||||
| mod.register() | mod.register() | ||||
| _hack_module._addon_current_hack = None # TEST ONLY | |||||
| except Exception as ex: | except Exception as ex: | ||||
| _hack_module._addon_current_hack = None # TEST ONLY | |||||
| print("Exception in module register(): %r" % | print("Exception in module register(): %r" % | ||||
| getattr(mod, "__file__", module_name)) | getattr(mod, "__file__", module_name)) | ||||
| handle_error(ex) | handle_error(ex) | ||||
| del sys.modules[module_name] | del sys.modules[module_name] | ||||
| if default_set: | if default_set: | ||||
| _addon_remove(module_name) | _addon_remove(module_name) | ||||
| _hack_module._addon_current_hack = None # TEST ONLY | |||||
| return None | return None | ||||
| # * OK loaded successfully! * | # * OK loaded successfully! * | ||||
| mod.__addon_enabled__ = True | mod.__addon_enabled__ = True | ||||
| mod.__addon_persistent__ = persistent | mod.__addon_persistent__ = persistent | ||||
| if _bpy.app.debug_python: | if _bpy.app.debug_python: | ||||
| print("\taddon_utils.enable", mod.__name__) | print("\taddon_utils.enable", mod.__name__) | ||||
| ▲ Show 20 Lines • Show All 122 Lines • Show Last 20 Lines | |||||