Post #2873: ![](https://blenderartists.org/user_avatar/blenderartists.org/flattiefolks/48/43

๐Ÿ“‹ Metadata

๐Ÿท๏ธ Tags

hotkeys configuration advanced solved

  • Hotkey Configuration
  • Python Scripting

๐Ÿ’ฌ Content

Flattiefolks:

Is there any way to call the workspaces dropdown menu from your script within any blender view window by keyboard shortcut? So far this only worked for a โ€œRun Scriptโ€ shortcut within the text editor area only.

Yes, use Screen Editing keymap which allows to override all existing keyboard shortcuts in all areas.
Eg (ctrl+shift+Space shortcut):

import bpy


class WORKSPACE_OT_select(bpy.types.Operator):
    bl_idname = "workspace.select"
    bl_label = "Select Workspace"
    bl_description = "Select workspace"
    bl_property = "workspace"

    enum_items = None

    def get_items(self, context):
        if WORKSPACE_OT_select.enum_items is None:
            enum_items = []

            for w in bpy.data.workspaces:
                identifier, name, description = \
                    w.name, w.name, w.name
                if context.workspace.name == identifier:
                    name += "|Active"
                enum_items.append((
                    identifier,
                    name,
                    description))

            WORKSPACE_OT_select.enum_items = enum_items

        return WORKSPACE_OT_select.enum_items

    workspace = bpy.props.EnumProperty(items=get_items)

    def execute(self, context):
        if not self.workspace or self.workspace not in bpy.data.workspaces:
            return {'CANCELLED'}

        context.window.workspace = bpy.data.workspaces[self.workspace]
        return {'FINISHED'}

    def invoke(self, context, event):
        WORKSPACE_OT_select.enum_items = None
        context.window_manager.invoke_search_popup(self)
        return {'FINISHED'}


addon_keymaps = []


def register():
    bpy.utils.register_class(WORKSPACE_OT_select)
    
    wm = bpy.context.window_manager
    km = wm.keyconfigs.addon.keymaps.new(
        name='Screen Editing')

    kmi = km.keymap_items.new(
        WORKSPACE_OT_select.bl_idname, 'SPACE', 'PRESS', 
        ctrl=True, shift=True)

    addon_keymaps.append((km, kmi))


def unregister():
    for km, kmi in addon_keymaps:
        km.keymap_items.remove(kmi)
        
    addon_keymaps.clear()
    
    bpy.utils.unregister_class(WORKSPACE_OT_select)


if __name__ == "__main__":
    register()

โค๏ธ 4 likes


๐Ÿ”— View on Blender Artists