1
$\begingroup$

I want to check if an addon exists and if it is enabled. So i tried:

loaded_default, loaded_state = addon_utils.check(addon_name)

But i found that i can not distinguish between the 2 cases:

  • Addon is not installed
  • Addon is not enabled

So in the case where i want to programmatically enable an addon, i can only do:

addon_name="myaddon"
default, state = addon_utils.check(addon_name)
print ("%s: default:%s, state:%s" % (addon_name, default, state) )
if default == False and state is None:
    print("%s is not installed or not enabled by default" % addon_name)

if state != True :
    print("Try to enable module %s On The Fly ..." % addon_name )
    try:
        mod = addon_utils.enable(addon_name, default_set=False, persistent=True)
        if mod:
            state = True
    except:
        print("Could not enable %s on the fly. Ignore" % addon_name )

But this is ugly. Especially when the module does not exist then i get a stack trace in the console, even when i use a try/except block. For the the example above i get this in the console:

myaddon: default:False, state:None
myaddon is not installed or not enabled by default
Try to enable module myaddon On The Fly ...
Traceback (most recent call last):
  File "D:\blendergit\cmake-build\bin\Release\2.71\scripts\modules\addon_utils.py", line 299, in enable
    mod = __import__(module_name)
ImportError: No module named 'myaddon'

Is it possible to make this more elegant and avoid to see the stack trace when the child addon does not exist ?

$\endgroup$
2
$\begingroup$

If the addon is not 'installed' (i.e. unknown from Blender side), then it won’t be listed in addon_utils.addons_fake_modules. So the solution is:

mod = None
addon_name = "myaddon"
if addon_name not in addon_utils.addons_fake_modules:
    print("%s: Addon not installed." % addon_name)
else:
    default, state = addon_utils.check(addon_name)
    if not state:
        try:
            mod = addon_utils.enable(addon_name, default_set=False, persistent=False)
        except:
            print("%s: Could not enable Addon on the fly." % addon_name )

if mod:
    print("%s: enabled and running." % addon_name)
$\endgroup$
  • $\begingroup$ Yes, that was the missing link :) $\endgroup$ – Gaia Clary Sep 7 '14 at 12:57
  • 1
    $\begingroup$ You should replace if state != True by if not state $\endgroup$ – CoDEmanX Sep 12 '14 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.