When creating add-ons for Blender, developers almost always create the user UI panel with tools that provide the main functionality of the add-on. Typically, such custom panels are placed in the N-panel block, called by pressing the N key. However, if it is assumed that the user will often call the panel with add-on tools, its call can be bound to a keyboard shortcut and the panel can be called from the keyboard.
For example, let’s create a simple custom panel with two buttons, to which we will bind a call to the basic operators of adding a cube and Suzanne to the scene.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class TEST_PT_panel(from bpy.types.Panel): bl_idname = 'TEST_PT_panel' bl_label = 'TEST' bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_category = 'TEST' def draw(self, context): self.layout.operator( operator='mesh.primitive_cube_add', icon='MESH_CUBE', text='' ) self.layout.operator( operator='mesh.primitive_monkey_add', icon='MESH_MONKEY', text='' ) |
Register our panel class in the Blender Python API:
1 |
register_class(TEST_PT_panel) |
Now the N-panel has a tab with the name of our custom panel – “TEST”. By clicking on it, we can open the panel and click on the operator buttons to add meshes to the scene.
In order to call this panel by pressing certain keys on the keyboard, first we need to get a pointer to the keymap:
1 2 3 4 |
km = bpy.context.window_manager.keyconfigs.addon.keymaps.new( name='Window', space_type='EMPTY' ) |
Now we can create a new keymap element in which we initiate execution of the “wm.call_panel” operator. In this command, we also set the key combination for calling the operator – in our example, let it be the combination of Shift + T keys:
1 2 3 4 5 6 7 8 9 |
kmi = km.keymap_items.new( 'wm.call_panel', 'T', 'PRESS', ctrl=False, alt=False, shift=True, repeat=False ) |
Set for this keymap element the “bl_idname” of our panel that will be called:
1 |
kmi.properties.name = 'TEST_PT_panel' |
And additionally, turn off the “keep_open” parameter so that the panel closes immediately after pressing the buttons on it.
1 |
kmi.properties.keep_open = False |
Now, when we press the Shift + T key combination, our panel will be opened in a separate pop-up window.