I have a problem with my keymaps

(Syler) #1

so i have made two addons in one the keymap works as expected.
in the on where it works (as i expect it)i disable the keymap and restart blender the keymap is still disabled in the other the keymaps just don’t care if i disable the keymap or not here is my code (Note: the addons don’t use the same code for registration) that registers the the keymaps:

# -----------------------------------------------------------------------------
#    Keymap      
# -----------------------------------------------------------------------------

addon_keymaps = []

def add_hotkey():
    
    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon

    if not kc:
        return
    
    km = kc.keymaps.new(name='Object Mode', space_type='EMPTY')

    kmi = km.keymap_items.new(SM_PIE_Add_Call.bl_idname, 'A', 'PRESS', ctrl=False, shift=True)
    addon_keymaps.append((km, kmi))
    kmi = km.keymap_items.new(SM_PIE_Q_Menu_Call.bl_idname, 'Q', 'PRESS', ctrl=False, shift=False)
    addon_keymaps.append((km, kmi))
    kmi = km.keymap_items.new(SM_PIE_A_OM_Call.bl_idname, 'A', 'PRESS', ctrl=False, shift=False)
    addon_keymaps.append((km, kmi))
    
    km = kc.keymaps.new(name='Node Generic', space_type='NODE_EDITOR')

    kmi = km.keymap_items.new(SM_PIE_Add_Node_Call.bl_idname, 'A', 'PRESS', ctrl=False, shift=True)
    addon_keymaps.append((km, kmi))

    kmi = km.keymap_items.new(SM_PIE_Q_Node_Call.bl_idname, 'Q', 'PRESS', ctrl=False, shift=False)
    addon_keymaps.append((km, kmi))

    kmi = km.keymap_items.new(SM_PIE_A_NODE_Call.bl_idname, 'A', 'PRESS', ctrl=False, shift=False)
    addon_keymaps.append((km, kmi))
   
def remove_hotkey():
    for km, kmi in addon_keymaps:
        km.keymap_items.remove(kmi)

    addon_keymaps.clear()

the definitions get called in the init file add_hotkey in register and the other in unregister, is there some reason why i get this weird behavior? other things in my addon work as expected like if i disable a option in the prefs and restart blender it saves that and it also save the ui layout that i was in but not weather i disabled the keymap or not, im sure that i can make it work with bool props that save if a hotkey is enabled but that seems unnecessary.

0 Likes

(iceythe) #2

You can’t save changes done to keymaps on the addon layer. This is because they are recreated every time blender is restarted.

Blender creates an invisible user layer for every existing keymap item (kmi). When a default kmi (by addon or blender) is modified by a user, it is the kmi on the user layer which is being saved when preferences are saved.

In your preferences, you are listing the addon kmi and try to allow the user make changes on the addon layer. You should instead list the user kmi. When a user disables the kmi from your preferences, it needs to happen on the user layer, which then will be saved.

You also need to use a context pointer which tells blender to propagate the changes made from the ui to the rna.

Here’s for the prefs.py. I’ve made the necessary changes:


    def add_keymap_to_ui(self, context, layout, k_name, idname):
        # keymap_item = context.window_manager.keyconfigs.addon.keymaps[k_name].keymap_items
        keymap_item = context.window_manager.keyconfigs.user.keymaps[k_name].keymap_items
        row = layout.row()
        km = context.window_manager.keyconfigs.user.keymaps[k_name]  # added
        layout.context_pointer_set("keymap", km)  # added
        row.prop(keymap_item[idname], 'active', text="",full_event=True)
        row.prop(keymap_item[idname], 'type', text=keymap_item[idname].name, full_event=True) 

1 Like

(Syler) #3

thank you i have been trying to fix that problem for hours and found no solution

0 Likes