Fix error enabling an already enabled addon
authorCampbell Barton <ideasman42@gmail.com>
Sun, 14 Jun 2015 12:19:26 +0000 (22:19 +1000)
committerCampbell Barton <ideasman42@gmail.com>
Sun, 14 Jun 2015 12:26:42 +0000 (22:26 +1000)
Would run register() twice causing warning with `register_module()`
which expects new classes to be defined.

Now run unregister() before re-registering.

release/scripts/modules/addon_utils.py

index 4168f0a1f92febabcd8a0d44ff5caa7712123b58..e14b25b1e65ac3a30b0e96d866b4560afebcc3a5 100644 (file)
@@ -278,6 +278,20 @@ def enable(module_name, default_set=False, persistent=False, handle_error=None):
     mod = sys.modules.get(module_name)
     # chances of the file _not_ existing are low, but it could be removed
     if mod and os.path.exists(mod.__file__):
+
+        if getattr(mod, "__addon_enabled__", False):
+            # This is an unlikely situation,
+            # re-register if the module is enabled.
+            # Note: the UI doesn't allow this to happen,
+            # in most cases the caller should 'check()' first.
+            try:
+                mod.unregister()
+            except:
+                print("Exception in module unregister(): %r" %
+                      getattr(mod, "__file__", module_name))
+                handle_error()
+                return None
+
         mod.__addon_enabled__ = False
         mtime_orig = getattr(mod, "__time__", 0)
         mtime_new = os.path.getmtime(mod.__file__)