3
$\begingroup$

My add-on requires Cycles to be enabled as I append some classes to existing Cycles types at registration.

I made a function to check if Cycles is enabled:

def cycles_check():
    import addon_utils
    is_enabled,is_loaded = addon_utils.check('cycles')
    if not is_loaded:
        print ('Cycles is required to use Photographer, enabling the add-on automatically.')
        success = addon_utils.enable('cycles')
        if success:
            print ('Cycles is now enabled.')
            return True
    else:
        return True

It "seems" to work: if Cycles is disabled when I enable my add-on, it enables it and my add-on can register properly. I can see Cycles in the render engine lists.

Cycles is required to use Photographer, enabling the add-on.
module changed on disk: 'C:\\Blender\\daily\\blender-2.92.0-6fb7d0f55927-windows64\\2.92\\scripts\\addons\\cycles\\__init__.py' reloading...
Cycles is now enabled.

But... Cycles is actually not properly enabled. The add-on in the preferences is not checked, and the Cycles Device property is missing, which breaks the UI but also crashes Blender if I start a render.

enter image description here

Traceback (most recent call last):
  File "c:\Blender\daily\blender-2.92.0-6fb7d0f55927-windows64\2.92\scripts\modules\bpy_types.py", line 808, in draw_ls
    func(self, context)
  File "c:\Blender\daily\blender-2.92.0-6fb7d0f55927-windows64\2.92\scripts\addons\cycles\ui.py", line 2217, in draw_device
    col.active = show_device_active(context)
  File "c:\Blender\daily\blender-2.92.0-6fb7d0f55927-windows64\2.92\scripts\addons\cycles\ui.py", line 114, in show_device_active
    return context.preferences.addons[__package__].preferences.has_active_device()
$\endgroup$
3
  • $\begingroup$ Shouldn’t you be using the “is enabled“ result instead of “is loaded“ result? Cycles might be loaded, but left disabled (I THINK). $\endgroup$
    – TheLabCat
    Mar 14, 2021 at 14:44
  • $\begingroup$ I have tried both, the problem is not that the condition is not met, but that addon_utils.enable('cycles') doesn't properly enable Cycles. Here is what the console prints: Cycles is required to use Photographer, enabling the add-on. module changed on disk: 'C:\\Blender\\daily\\blender-2.92.0-6fb7d0f55927-windows64\\2.92\\scripts\\addons\\cycles\\__init__.py' reloading... Cycles is now enabled. $\endgroup$
    – chafouin
    Mar 14, 2021 at 20:16
  • $\begingroup$ Huh 🤔 That is odd. Well, you can of course double check the python API in the blender documentation, but this may be a bug. I would let it be confirmed by someone wiser than me, though. $\endgroup$
    – TheLabCat
    Mar 14, 2021 at 20:27

2 Answers 2

2
$\begingroup$

I found a solution looking at how bpy.ops.preferences.addon_enable(module="cycles") was using addon_utils.enable, it needed default_set=True as an argument, which forced the add-on to be checked in the preferences and fixed the errors.

def cycles_check():
    import addon_utils
    is_enabled,is_loaded = addon_utils.check('cycles')
    if not is_loaded:
        print ('Cycles is required to use Photographer, enabling the add-on automatically.')
        success = addon_utils.enable('cycles', default_set=True)
        if success:
            print ('Cycles is now enabled.')
            return True
    else:
        return True
$\endgroup$
0
$\begingroup$

Seems weird but you can try bpy.ops.preferences.addon_enable(module="cycles")

Here's modified code:

def cycles_check():
    import addon_utils
    is_enabled,is_loaded = addon_utils.check('cycles')
    if not is_loaded:
        print ('Cycles is required to use Photographer, enabling the add-on automatically.')
        bpy.ops.preferences.addon_enable(module="cycles")
        is_enabled,is_loaded = addon_utils.check('cycles')    #check if cycles is properly enabled or not
        if is_loaded:
            print ('Cycles is now enabled.')
        return True
    else:
        return True
    
#test call
cycles_check()
$\endgroup$
1
  • 1
    $\begingroup$ This operator doesn't seem to work at registration, it is not in the right context. AttributeError: '_RestrictContext' object has no attribute 'view_layer' However, looking at the code, I found a solution to my problem. $\endgroup$
    – chafouin
    Mar 16, 2021 at 4:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .