Nope, it's a little different. How you call keymaps.new
will determine which context will trigger the menu. Currently you add the shortcut to Mesh, which is EDIT_MODE
, therefore the menu only appears when you are in edit mode.
Instead of adding it to Mesh
and Object Mode
separately you might consider adding it the generic 3D View
keymappings, this will then trigger the menu whenever the mouse is in 3d view and you press the key-combo.
km = kc.keymaps.new('3D View', space_type='VIEW_3D', region_type='WINDOW')
Then inside your Menu layout you add logic which determines what kind of things to show depending on the mode of the Object.
import bpy
from bpy.types import Menu
class NewPie(Menu):
bl_label = "Select Mode"
bl_idname = 'kwanza'
def draw(self, context):
layout = self.layout
pie = layout.menu_pie()
pie.operator("mesh.primitive_cube_add")
# if context.mode == 'OBJECT':
# something with object mode
# else:
# something with edit mode
addon_keymaps = []
def register():
bpy.utils.register_class(NewPie)
wm = bpy.context.window_manager
kc = wm.keyconfigs.addon
km = kc.keymaps.new('3D View', space_type='VIEW_3D', region_type='WINDOW')
keymaps = km.keymap_items
new_shortcut = keymaps.new("wm.call_menu_pie", "SEMI_COLON", "PRESS", shift=True)
new_shortcut.properties.name = 'kwanza'
addon_keymaps.append((km, new_shortcut))
def unregister():
bpy.utils.unregister_class(NewPie)
for km, shortcut in addon_keymaps:
km.keymap_items.remove(shortcut)
addon_keymaps.clear()
if __name__ == "__main__":
register()
A small note about the code you pasted in your Question.
The last line should be bpy.ops.wm.call_menu_pie(name="mypie")
.
When you don't add a bl_idname
to the Menu definition then you can call operators directly using the class name, otherwise if bl_idname
is specified then you must use that instead.
A useful way to get a handle on how keymaps work is by setting them manually first, then using the keymap export feature to generate a .py file, and in this file do a search for your operator name or menu name
When I added kwanza to 3D View (Global)
it added this to the keymaps export
# Map 3D View
km = kc.keymaps.new('3D View', space_type='VIEW_3D', region_type='WINDOW', modal=False)
... (snipped)
kmi = km.keymap_items.new('wm.call_menu_pie', 'SEMI_COLON', 'PRESS', shift=True)
kmi_props_setattr(kmi.properties, 'name', 'kwanza')