bpy.ops.wm.addon_expand(module="module_name") works in console, but not in add-on 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()
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.:


http://wiki.blender.org/index.php/Extensions:2.6/Py/API_Changes#Restricted_Context
https://blender.stackexchange.com/a/18134/1363