1
$\begingroup$

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
from bpy.types import Menu

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

        pie = layout.menu_pie()
        pie.operator("mesh.primitive_cube_add")

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

    wm = bpy.context.window_manager
    km = wm.keyconfigs.addon.keymaps.new(name="Mesh")
    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()

    bpy.ops.wm.call_menu_pie(name="NewPie")
$\endgroup$
2
$\begingroup$

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)
    #Add shortcuts
    wm = bpy.context.window_manager
    #Edit Mode    
    km = wm.keyconfigs.addon.keymaps.new(name="Mesh")
    kmi = km.keymap_items.new("wm.call_menu_pie", "E", "PRESS",shift=True, ctrl=True).properties.name="mypie"
    #Object Mode
    km = wm.keyconfigs.addon.keymaps.new(name="Object Mode")
    kmi = km.keymap_items.new("wm.call_menu_pie", "E", "PRESS", shift=True, ctrl=True).properties.name="mypie"
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Claas Kuhnen Jan 9 '16 at 13:38
  • $\begingroup$ 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. $\endgroup$ – Italic_ Jan 15 '16 at 3:33
2
$\begingroup$

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')
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Claas Kuhnen Jan 9 '16 at 16:40
  • $\begingroup$ have a look at space_view3d.py lines like if context.mode != 'OBJECT': $\endgroup$ – zeffii Jan 9 '16 at 16:42
  • $\begingroup$ 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. $\endgroup$ – Claas Kuhnen Jan 9 '16 at 18:40
  • $\begingroup$ The inactive shading is done explicitely: see blender.stackexchange.com/questions/41387/… $\endgroup$ – zeffii Jan 9 '16 at 18:54
  • $\begingroup$ or through an if context mode is edit mesh like in the official pie menu as I just learned $\endgroup$ – Claas Kuhnen Jan 9 '16 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.