3
$\begingroup$

This is very simple script example from Blender templates:

import bpy


class SimpleCustomMenu(bpy.types.Menu):
    bl_label = "Simple Custom Menu"
    bl_idname = "OBJECT_MT_simple_custom_menu"

    def draw(self, context):
        layout = self.layout

        layout.operator("wm.open_mainfile")
        layout.operator("wm.save_as_mainfile")


def register():
    bpy.utils.register_class(SimpleCustomMenu)


def unregister():
    bpy.utils.unregister_class(SimpleCustomMenu)


if __name__ == "__main__":
    register()

    # The menu can also be called from scripts
    bpy.ops.wm.call_menu(name=SimpleCustomMenu.bl_idname)

All I care about is the ability opening the menu in separate floating window without need to go thru menu all the time, as I have my own code (add-on) and trying to implement this (no success yet - I am still scripting newbie, so you know).

Thus merit of my question is: how can we make what line bpy.ops.wm.call_menu(name=SimpleCustomMenu.bl_idname) is doing with hardcoded keyboard shortcut right in the script itslef?

Like: when I press, let's say, Shift+Q it would open that small floating window with my menu - anyone with a working example script based on the code above?

I DID TRY several ways, but still I was unable making it work, that is reason why I would like to see actual working code, not just some kind of statement (I saw such statements already and they were of no help at all to me).

EDIT: I reformated my question so it would be clear to everyone what I mean (sorry, english is not my native language)

$\endgroup$
0

1 Answer 1

2
$\begingroup$

Just set the keymap item identifier to WindowManager.call_menu and assign bl_idname of the menu to the name property. Basic example on how create a shortcut to call a menu using CtrlW:

enter image description here

import bpy

class SimpleCustomMenu(bpy.types.Menu):
    bl_label = "Simple Custom Menu"
    bl_idname = "OBJECT_MT_simple_custom_menu"

    def draw(self, context):
        layout = self.layout
        layout.operator("wm.open_mainfile")
        layout.operator("wm.save_as_mainfile")

addon_keymaps = []

def register():
    bpy.utils.register_class(SimpleCustomMenu)
    
    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon
    if kc:
        km = wm.keyconfigs.addon.keymaps.new(name='3D View', space_type='VIEW_3D')
        kmi = km.keymap_items.new('wm.call_menu', 'W', 'PRESS', ctrl=True, shift=False, alt=False)
        kmi.properties.name =  SimpleCustomMenu.bl_idname
        addon_keymaps.append((km, kmi))

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

if __name__ == "__main__":
    register()

Related: Create keyboard shortcut for an operator using python?

$\endgroup$
0

You must log in to answer this question.

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