0
$\begingroup$

I'm trying to add a keymap that overrides an actual shortcut in Blender (M key in Grease Pencil's Draw Mode).

By default, M triggers: enter image description here

I would like to add my own action (opening a panel) in that same key. The code I have:

addon_keymaps = []

def _register_hotkey():
    # Add the hotkey
    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon
    if kc:
        name = 'Grease Pencil Draw'
        space_view = 'VIEW_3D'
        km = wm.keyconfigs.addon.keymaps.new(name=name, space_type=space_view)
        kmi = km.keymap_items.new("wm.call_panel", type='M', value='PRESS')
        kmi.properties.name = "TEST_PT_my_panel"  
        addon_keymaps.append((km, kmi))

But I had no luck, M triggers that gpencil.draw.

Also, I'm not sure if I'm doing it propperly. I'm not sure if that name I'm entering is the correct and I dont know what it refers to (to be honest, the API says nothing about it). So I don't know if it can be any name, if it must match the name of the keymap I want to override (if so, which is its name? 'Grease Pencil Draw'? 'Grease Pencil Stroke Paint (Draw brush)'?).

Also, Im not fully sure if is possible.

I'm calling _register_hotkey() in:

bpy.app.handlers.load_post

(Since I read here that when loading the keymaps are not accesible, just to be sure. But I got the same result like when Im calling _register_hotkey() in register())

Thank you so much for the help.

$\endgroup$
3
  • 1
    $\begingroup$ If this addon is meant to be shared and used by other people, I would advise against replacing keymap entries. Though obviously if it's only for you, it's fair game :) In any case, I think this might help $\endgroup$
    – L0Lock
    Jul 31, 2023 at 18:44
  • $\begingroup$ @L0Lock Thank you so much for the help friend, worked perfectly! Also, I think you are totally right, but at the same time, the original operator (gpencil.draw) seems weird (at least for me). Is like it stops the input until you click/draw again. I really don't know which uses could it have, to be honest (seems like a good thing to call from code, but not from the UI). Again, thank you so much for the help, much appreciated :) $\endgroup$
    – Ommadawn
    Aug 1, 2023 at 11:05
  • 1
    $\begingroup$ My pleasure. I posted an answer with just a quote from my link for good form, feel free to edit it to make it better if you want or post your own answer. $\endgroup$
    – L0Lock
    Aug 1, 2023 at 14:19

1 Answer 1

1
$\begingroup$

Quoting the solution from the blenderartists forum:

The default keymap often isn’t accessible during addon registration, so you probably need to add a timer.

Note: This example only checks for kmi entries that matches the kmi.idname, so if you used something like wm.call_pie_menu it would disable every entry, unless you added a conditional based off from the kmi operator property.

def disable_default_kmi(km=None, idname=None, retries=10):
    wm = bpy.context.window_manager

    if not (km and idname) or retries < 1:
        return

    # the default keyconfig
    kc = wm.keyconfigs['blender']
    for kmi in kc.keymaps[km].keymap_items:
        if kmi.idname == idname:
            kmi.active = False
            print("Disabled", kmi.name)
            return

    print("Retrying..")
    # add some delay
    bpy.app.timers.register(
        lambda: disable_default_kmi(km, idname, retries - 1),
        first_interval=0.1)

Then during register() you can run something like this.

def register():
    ...
    disable_default_kmi('Screen Editing', 'screen.area_options')

Source: Removing a specific default hotkey / shortcut via a script? - Coding / Python Support - Blender Artists Community

Visit the source for more.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .