Why wont this addon retain keymap user preferences?

bl_info = {
    "name" : "addon test", 
    "author" : "BD3D",   
    "description" : "",
    "blender" : (2, 80, 0),
    "location" : "Operator",
    "warning" : "",
    "category" : "Generic"
}

import bpy
from bpy.types import Menu
import webbrowser
from bpy.types import Operator, AddonPreferences
from bpy.props import StringProperty, IntProperty, BoolProperty
import rna_keymap_ui
context = bpy.context

#addon pref menu
class WebLauncherPref(AddonPreferences):
    # this must match the addon name, use '__package__'
    # when defining this in a submodule of a python package.
    bl_idname = __name__
    
    def draw(self, context):
        layout = self.layout
        addon_prefs = context.preferences.addons[__name__].preferences

        layout.label(text="Shortcut:",icon='KEYTYPE_JITTER_VEC')
        kc = bpy.context.window_manager.keyconfigs.addon
        for km, kmi in addon_keymaps:
            km = km.active()
            layout.context_pointer_set("keymap", km)
            rna_keymap_ui.draw_kmi([], kc, km, kmi, layout, 0)

        
 #operator
class Lgotopage_OT_Operator4(bpy.types.Operator): 
    bl_idname = "view3d.lgotopage4" #create bl id name 
    bl_label = ""
    bl_description = "Open web page"

    def execute(self, context):
        addon_prefs = context.preferences.addons[__name__].preferences
        webbrowser.open("https://www.artstation.com/dorianborremans")
        return {'FINISHED'}
    


#register/unregister
addon_keymaps = []

def register():
    bpy.utils.register_class(Lgotopage_OT_Operator4)
    bpy.utils.register_class(WebLauncherPref)

    #register keymap
    kc = bpy.context.window_manager.keyconfigs.addon
    km = kc.keymaps.new(name='Window', space_type='EMPTY', region_type='WINDOW')
    kmi = km.keymap_items.new(Lgotopage_OT_Operator4.bl_idname, 'F6', 'PRESS', ctrl=False)
    kmi.active = True
    addon_keymaps.append((km, kmi))


def unregister():
    bpy.utils.unregister_class(Lgotopage_OT_Operator4)
    bpy.utils.unregister_class(WebLauncherPref)


    #unregister keymap
    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon
    km = kc.keymaps['Window']
    
    for km, kmi in addon_keymaps:
        km.keymap_items.remove(kmi)
        wm.keyconfigs.addon.keymaps.remove(km)
    addon_keymaps.clear()


if __name__ == "__main__":
    register()

hello, this addon won’t retain the user keymap preferences, i don’t understand why ?
what am i doing wrong ?

i also experience some weird behavior if i enable multiple addon that have this keymap pref system. disabling one will disable all the other addon keymap. sometimes disabling one will get me an error msg about this system not working correctly.

enter image description here

for sure im doing something wrong, how can i remedy that ?

Becayse you’ve been pointing kc to addon and not user.

        kc = bpy.context.window_manager.keyconfigs.addon
        for km, kmi in addon_keymaps:
            km = km.active()
            layout.context_pointer_set("keymap", km)
            rna_keymap_ui.draw_kmi([], kc, km, kmi, layout, 0)

When you register a default keymap, use keyconfigs.addon.
But when you want to display a kmi prop in the UI, use keyconfigs.user.

Addon keymaps are not stored across blender sessions and will reset every time, however user keymaps are.

Also, in unregister(), don’t remove keymaps (km). Doing this will cause issues if other addons use the same keymap. Remove only the keymap items (kmi).

2 Likes

thanks a lot for the answers ! super useful