有时候插件有些功能需要用快捷键快速调用,比较麻烦的是不知道哪些快捷键已经被设定好了,我就用最简单的方法一个一个按,确定ctrl+D没有调用任何功能,那就先用ctrl+D来当快捷键
先创建个Operator
class TestShortcutOperator(bpy.types.Operator):
bl_idname = "test_shortcut.operator"
bl_label = "快捷键"
def execute(self, context):
print("ctrl + D")
return {'FINISHED'}
注册这个Operator和创建快捷键
def register():
bpy.utils.register_class(TestShortcutOperator)
keymaps = bpy.context.window_manager.keyconfigs.addon.keymaps
km = None
if '3D View' in keymaps:
km = keymaps['3D View']
else:
km = keymaps.new(name='3D View', space_type='VIEW_3D')
km.keymap_items.new(TestShortcutOperator.bl_idname, type='D', value='PRESS', ctrl=True)
最后注销和删除快捷键
def unregister():
keymaps = bpy.context.window_manager.keyconfigs.addon.keymaps
if '3D View' in keymaps:
km = keymaps['3D View']
for kmi in km.keymap_items:
if kmi.idname == TestShortcutOperator.bl_idname:
km.keymap_items.remove(kmi)
bpy.utils.unregister_class(TestShortcutOperator)