5
$\begingroup$

I've been messing around with keymaps lately and I've come across behavior that doesn't make much sense to me. I am searching the current keyconfig for a specific keymap item, removing it, then adding a new keymap item with the same assignments. I am doing this in register(), which I think might not be able to mix the two keyconfigs.

def register():
    wm = bpy.context.window_manager
    km = wm.keyconfigs.user.keymaps['Mesh']

    # remove default keybinding
    for kmi in wm.keyconfigs.user.keymaps['Mesh'].keymap_items:
        if kmi.idname == 'wm.call_menu' and kmi.properties.name == "VIEW3D_MT_edit_mesh_select_mode":
            km.keymap_items.remove(kmi)
            break

    # add multiselect keybinding
    km = wm.keyconfigs.addon.keymaps.new(name='Mesh')
    kmi = km.keymap_items.new('wm.call_menu', 'TAB', 'PRESS', ctrl=True)
    kmi.properties.name = "VIEW3D_MT_Multiselect_Menu"

Note the difference between wm.keyconfigs.USER and wm.keyconfigs.ADDON

However when I activate the add-on, the original keymap item is gone and the new keymap item has not been registered. If I register the new keymap item under .user, it works well enough to get by. Similarly, I have another function elsewhere that (un)registers keymap items in the .addon space that also presents inconsistencies:

I have a toggle button in the user prefs to toggle between one menu and another (pie and list menus). Using .addon, I can toggle the menus and the pie is registered properly and list is unregistered, but toggling back removes the pie and does not register the list menu. If I use .user, both keymap items register and unregister just fine.

Let me explain why I want to do it this way.

First off, I'm using a custom keyconfig, which presents its own difficulties when searching the active keyconfig for an existing keymap item. This is why I'm using .user instead of .active (.active was telling me 'Mesh' was an invalid keymap and didn't exist. Using default Blender keyconfig does not give this error).

Second, I want to not overwrite or write into the existing keyconfig. Presumably this is why .addon exists. I want to keep it 'clean' for lack of a better word.

Ultimately, I want to make a system that auto-detects a specific keymap item and its assignments (in the case of a custom hotkey assignment), stores all that data, then on unregistration restores the original keymap item and its assignments. There are many other inconsistencies I've come across, but each of those deserves their own questions.

My Question:

Is there a way I can combine .addon and .user spaces OR use .active without it telling me 'Mesh' keymap doesn't exist (there are clearly keymap items in the Mesh category)? I see other add-ons use .addon without issue (yafaray, for example), but they don't try to mix the two spaces.

$\endgroup$
0
$\begingroup$

OR use .active without it telling me 'Mesh' keymap doesn't exist

Have you tried checking for and adding it if necessary?

kc=bpy.context.window_manager.keyconfigs.active
km='Mesh' in kc.keymaps and kc.keymaps['Mesh'] or kc.keymaps.new('Mesh')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.