1
$\begingroup$

My add-on shows the User Preferences view, selects the "Add-ons" tab and search for a specific add-on.

I'm trying to expand that add-on, but it doesn't work consistently. I get this error:

AttributeError: 'NoneType' object has no attribute 'bl_info'

It works in console, though.

The code in question is this, where xxx is the name of the .py file or the add-on folder.

class XxxOperator(bpy.types.Operator):

    bl_idname = "xxx.configure"
    bl_label = "Show Plugin"

    def execute(self, context):
            bpy.context.user_preferences.active_section = 'ADDONS'
            window = bpy.ops.screen.userpref_show('INVOKE_DEFAULT')
            bpy.data.window_managers["WinMan"].addon_search = "xxx"
            bpy.ops.wm.addon_expand(module = "xxx")
            return {'FINISHED'}

It's an option in the menu. The init file (I'll omit the bl_info and the imports):

class XxxMenu(bpy.types.Menu):
bl_idname = "OBJECT_MT_XxxMenu"
bl_label = "Xxx"

def draw(self, context):
    layout = self.layout
    layout.operator("xxx.submit", text="Submit")
    layout.operator("xxx.configure", text="Show Plugin")

def add_to_menu(self, context):
    self.layout.menu("OBJECT_MT_XxxMenu", icon = "PLUGIN");

def register():
    bpy.utils.register_module(__name__)
    bpy.types.INFO_MT_render.append(add_to_menu)

def unregister():
    bpy.utils.unregister_module(__name__)
    bpy.types.INFO_MT_render.remove(add_to_menu)

if __name__ == "__main__":
    register()
$\endgroup$
5
  • $\begingroup$ BSE automatically makes inverted double quotes, even when the plain text of the title uses the right symbols. (just FYI no point editing the title, i tried) $\endgroup$
    – zeffii
    Jun 9, 2015 at 10:00
  • 1
    $\begingroup$ are you getting " AttributeError: 'NoneType' object has no attribute 'bl_info' " $\endgroup$
    – zeffii
    Jun 9, 2015 at 10:12
  • $\begingroup$ Yes, I get that error. I edited the question, thanks. $\endgroup$
    – Revan
    Jun 9, 2015 at 10:37
  • $\begingroup$ Can we get the full script, and in which context it is used (e.g. is it a plain addon? a script to be executed from Text window? etc.) $\endgroup$
    – mont29
    Jun 9, 2015 at 11:55
  • $\begingroup$ Sorry, I can't share the full script (that sucks, I know), but I'll add more info in the question. $\endgroup$
    – Revan
    Jun 9, 2015 at 12:33

2 Answers 2

2
$\begingroup$

Whether an addon is displayed expanded or not in the User Preferences is stored in the bl_info dictionary, in the property called show_expanded.

addon_utils manages a cache of addons. It uses the global addons_fake_modules. It can be used to determine and change the expand state:

import bpy
import addon_utils

# Get a module reference by name
mod = addon_utils.addons_fake_modules.get("space_view3d_3d_navigation")

# If found, expand it
if mod is not None:
    mod.bl_info['show_expanded'] = True

    # Find User Preferences area and redraw it
    for window in bpy.context.window_manager.windows:
        for area in window.screen.areas:
            if area.type == 'USER_PREFERENCES':
                area.tag_redraw()

Note that running this code as part of an addon is tricky, see e.g.:

$\endgroup$
1
$\begingroup$

from startp/bl_operators/wm.py the WM_OT_addon_expand operator gives a hint how to check if it is expanded or not:

class WM_OT_addon_expand(Operator):
    "Display more information on this addon"
    bl_idname = "wm.addon_expand"
    bl_label = ""
    bl_options = {'INTERNAL'}

    module = StringProperty(
            name="Module",
            description="Module name of the addon to expand",
            )

    def execute(self, context):
        import addon_utils

        module_name = self.module

        # unlikely to fail, module should have already been imported
        try:
            # mod = __import__(module_name)
            mod = addon_utils.addons_fake_modules.get(module_name)
        except:
            import traceback
            traceback.print_exc()
            return {'CANCELLED'}

        info = addon_utils.module_bl_info(mod)
        info["show_expanded"] = not info["show_expanded"]
        return {'FINISHED'}

Thus.. i think something like this, you'll probably want to refine it

import bpy
import addon_utils

module_name = "sverchok"
addon_utils.modules_refresh()

bpy.ops.screen.userpref_show("INVOKE_DEFAULT")
bpy.context.user_preferences.active_section = "ADDONS"
bpy.data.window_managers['WinMan'].addon_search = "sverch"

try:
    mod = addon_utils.addons_fake_modules.get(module_name)
    info = addon_utils.module_bl_info(mod)
    if not info["show_expanded"]:
        info["show_expanded"] = True

except:
    import traceback
    traceback.print_exc()

it's just these lines really:

mod = addon_utils.addons_fake_modules.get(module_name)
info = addon_utils.module_bl_info(mod)
info["show_expanded"] = True  # or False to Collapse
$\endgroup$
5
  • $\begingroup$ Thanks, but that's exactly what I have, and it doesn't work in my add-on. $\endgroup$
    – Revan
    Jun 9, 2015 at 10:07
  • $\begingroup$ When used in console it works, and if it's already expanded, it contracts it again. $\endgroup$
    – Revan
    Jun 10, 2015 at 16:28
  • $\begingroup$ But expand/unexpand was always the only problem... the rest of the code always worked fine. EDIT: sorry, I didn't see your last answer. So the only problem now is to detect if the add-on was already expanded? $\endgroup$
    – Revan
    Jun 10, 2015 at 17:19
  • $\begingroup$ The bl_info error disappears if I delete the "addon_expand" line. $\endgroup$
    – Revan
    Jun 10, 2015 at 17:30
  • 1
    $\begingroup$ Actually, it's possible to detect if an addon is displayed expanded and can be toggled without an operator. See my answer for example code. $\endgroup$
    – CodeManX
    Jun 10, 2015 at 17:42

You must log in to answer this question.

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