This code shows you how can you create a pie menu with a hotkey so you can start your customized pie menu under your Blender add-on. A Pie menu is especially needed in the 3D View window while 3D artists create a new model.
Scripting –> Templates –> Python –> UI Pie Menu
Although there are many examples of creating a pie menu in your Blender add-on or your plugin most of them add hotkey their pie menus manually. If you want to add your hotkey to your pie menu you just need to register your hotkeys.
#Addon shortcuts
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_pie", type='V', value='PRESS', shift=True)
kmi.properties.name = 'VIEW3D_MT_PIE_template'
addon_keymaps.append((km, kmi))
Nearly every button click can be implemented in the blender scripting so first of all for creating a new hotkey we need to reach preferences we need wm = bpy.context.window_manager after creating new keymaps for 3D View we need km = wm.keyconfigs.addon.keymaps.new(name='3D View', space_type='VIEW_3D')
after creating a new section for keymap now we can describe our shortcut properties kmi = km.keymap_items.new("wm.call_menu_pie", type='V', value='PRESS', shift=True)
this line makes our hotkey can be available with
the command. Now we just need to describe our specific "wm.call_menu_pie"
kmi.properties.name = 'VIEW3D_MT_PIE_template'
class VIEW3D_MT_PIE_template(Menu)’s bl_idaname.
Edit–> Preferences–> Keymap –> Select Mode
Full code of the pie menu addon
import bpy
from bpy.types import Menu
# spawn an edit mode selection pie (run while object is in edit mode to get a valid output)
class VIEW3D_MT_PIE_template(Menu):
# label is displayed at the center of the pie menu.
bl_idanme = 'VIEW3D_MT_PIE_template'
bl_label = "Select Mode"
def draw(self, context):
layout = self.layout
pie = layout.menu_pie()
# operator_enum will just spread all available options
# for the type enum of the operator on the pie
pie.operator_enum("mesh.select_mode", "type")
pie.operator("mesh.primitive_cube_add", text="Add Cube", icon='MESH_CUBE')
addon_keymaps = []
def register():
bpy.utils.register_class(VIEW3D_MT_PIE_template)
#Addon shortcuts
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_pie", type='V', value='PRESS', shift=True)
kmi.properties.name = 'VIEW3D_MT_PIE_template'
addon_keymaps.append((km, kmi))
def unregister():
bpy.utils.unregister_class(VIEW3D_MT_PIE_template)
for km,kmi in add_keymaps:
km.keymap_item.remove(kmi)
addon_keymaps.clear()
if __name__ == "__main__":
register()
#bpy.ops.wm.call_menu_pie(name="VIEW3D_MT_PIE_template")
Detailed YouTube Video