Programmatically assign keyboard shortcuts to my operators?

I wondered if there was a way from my script to assign keyboard shortcuts to the operators I’m adding. In my initial searches, even here, I see people ask about this, and end up being satisfied just manually adding the shortcut. I’m about to add quite a few operators, so I was hoping to assign defaults in my source. I notice the output window that normally shows equivalent Python calls for stuff doesn’t bring up anything when I add a shortcut. In the past, that’s implied to me there isn’t such a possible thing in Blender. So, is it possible at all?

in the register function of your script:

bpy.data.window_managers[0].keyconfigs.active.keymaps['Mesh'].keymap_items.new('op.idname',value='PRESS',type='A',ctrl=True,alt=True,shift=True,oskey=True)

changing, of course, the name of the keymap, the type (which key) and leaving the modifiers out as necessary.

1 Like

Found this, quite useful :slight_smile:

One of the easiest ways to get started with your own keymap is to make a few manual changes to the key config, export it, and then open that exported key config in a text or code editor.

The key config export isn’t any special format, it’s just a .py file that’s executed when imported.

Personally, I do registering and unregistering of my operators in my key config, so all I have to do is import my key config and everything is set up in one step.

Also setting bpy.app.debug = True and then checking the Help -> System Console will show a lot more information about operators that don’t show up normally in Blender’s info window.