# Mesh / object mode with Pie menu?

The following command in edit or object mode is the same to create a new mesh cube:

bpy.ops.mesh.primitive_cube_add


I structured my pie this way but nothing happens - only when I am in edit mode. I assume because mesh.primitive_cube_add is a mesh object? But it is the same readout in the console, when in object mode I press shift A and select the cube.

import bpy

bl_label = "Select Mode"
bl_idname = "mypie"
def draw(self, context):
layout = self.layout

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

wm = bpy.context.window_manager
kmi = km.keymap_items.new("wm.call_menu_pie", "E", "PRESS", shift=True, ctrl=True).properties.name="mypie"

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

if __name__ == "__main__":
register()



You have to add the shortcut to the Object Mode shortcuts so it would be captured when you are in object mode, so the register function would be like this :

def register():
bpy.utils.register_class(NewPie)
wm = bpy.context.window_manager
#Edit Mode
kmi = km.keymap_items.new("wm.call_menu_pie", "E", "PRESS",shift=True, ctrl=True).properties.name="mypie"
#Object Mode
kmi = km.keymap_items.new("wm.call_menu_pie", "E", "PRESS", shift=True, ctrl=True).properties.name="mypie"

• Aha - I was not aware of that Mesh is in deed referring to the edit mode. I understood it as the mesh area in the keymaps for sorting purpose in the preferences UI. – Claas Kuhnen Jan 9 '16 at 13:38
• I just want some clarification here: the hotkey is determined by the section of the keymap it's in, not the viewport context? I was under the impression blender is based almost entirely on context. Maybe I'm misunderstanding the term. – Italic_ Jan 15 '16 at 3:33

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

bl_label = "Select Mode"
bl_idname = 'kwanza'

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

# if context.mode == 'OBJECT':
#     something with object mode
# else:
#     something with edit mode

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

wm = bpy.context.window_manager
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'

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

km.keymap_items.remove(shortcut)

if __name__ == "__main__":
register()


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')

• Thank you for all the extra information - trying to understand the mode switch you talk about. I was curious already about how to define commands based on the mode you are in. – Claas Kuhnen Jan 9 '16 at 16:40
• have a look at space_view3d.py lines like if context.mode != 'OBJECT':  – zeffii Jan 9 '16 at 16:42
• I see it makes a function available if the mode is right or shades it inactive if the mode is wrong. I think I need to write the code for drawing the buttons with a condition of what mode the user is in. Let me try to figure this out - but might fail hahahaha. – Claas Kuhnen Jan 9 '16 at 18:40
• The inactive shading is done explicitely: see blender.stackexchange.com/questions/41387/… – zeffii Jan 9 '16 at 18:54
• or through an if context mode is edit mesh like in the official pie menu as I just learned – Claas Kuhnen Jan 9 '16 at 20:08