Using the information from this answer and this answer, I wrote a very simple script to enable rigify if it it wasn't already enabled
import bpy
import addon_utils
addon = bpy.context.preferences.addons.get('rigify')
if not addon:
addon_utils.enable('Rigify')
When it failed to work I expanded it to get some diagnostic input and so I extended it to this:
import bpy
import addon_utils
for addon in bpy.context.preferences.addons:
print(addon.module)
for module in addon_utils.modules():
print(module.bl_info.get('name'))
addon = bpy.context.preferences.addons.get('rigify')
if not addon:
for module in addon_utils.modules():
if module.bl_info.get('name') == 'Rigify':
print(module)
addon_utils.enable(module.name)
but it fails with the traceback:
<module 'rigify' from 'C:\\Program Files\\Blender Foundation\\Blender 2.93\\2.93\\scripts\\addons\\rigify\\__init__.py'>
Exception in module register(): C:\Program Files\Blender Foundation\Blender 2.93\2.93\scripts\addons\rigify\__init__.py
Traceback (most recent call last):
File "C:\Program Files\Blender Foundation\Blender 2.93\2.93\scripts\modules\addon_utils.py", line 386, in enable
mod.register()
File "C:\Program Files\Blender Foundation\Blender 2.93\2.93\scripts\addons\rigify\__init__.py", line 622, in register
if legacy_loaded or bpy.context.preferences.addons['rigify'].preferences.legacy_mode:
KeyError: 'bpy_prop_collection[key]: key "rigify" not found'
after which it is impossible to even manually enable rigify, for a different error that seems to imply that the first attempt partially succeeded.
I have manually verified that rigify is installed, and that if I manually enable it it works fine.
I have also looked at the output of the first two for loops and found rigify in the first output when it is enabled, but not when it is disabled, as expected. It also appears in the second output as expected.
This is probably a bug in addon_utils, but if so, my question is: how do I work around the bug and enable rigify from python? And if it's not a bug, then my question is: why doesn't the second script work?
I'll file a bug report, but addon_utils isn't a supported API, so it will probably be closed.
addon_utils.disable('rigify')doesn't appear to work. – Marty Fouts Nov 08 '21 at 19:45default_set=Trueto disable too. – scurest Nov 08 '21 at 19:46