From 8a62f77e64fb1ab6c1f063d34d4162b2a50db663 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= <sybren@stuvel.eu>
Date: Tue, 27 Mar 2018 12:41:01 +0200
Subject: [PATCH] Bone Selection Sets: better keymap handling

Previously the keymap registration would cause an error when there was
no Pose keymap yet. Now the code adheres to the example code in
[the tutorial](https://docs.blender.org/api/blender_python_api_master/info_tutorial_addon.html#keymap).
---
 bone_selection_sets.py | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/bone_selection_sets.py b/bone_selection_sets.py
index 32ca81c14..6d5ab4823 100644
--- a/bone_selection_sets.py
+++ b/bone_selection_sets.py
@@ -514,6 +514,10 @@ def uniqify(name: str, other_names: list) -> str:
     return "{}.{:03d}".format(name, min_index)
 
 
+# store keymaps here to access after registration
+addon_keymaps = []
+
+
 def register():
     for cls in classes:
         bpy.utils.register_class(cls)
@@ -530,10 +534,11 @@ def register():
     )
 
     wm = bpy.context.window_manager
-    km = wm.keyconfigs.active.keymaps['Pose']
+    km = wm.keyconfigs.addon.keymaps.new(name='Pose')
 
     kmi = km.keymap_items.new('wm.call_menu', 'W', 'PRESS', alt=True, shift=True)
     kmi.properties.name = 'POSE_MT_selection_sets'
+    addon_keymaps.append((km, kmi))
 
     bpy.types.VIEW3D_MT_select_pose.append(add_sss_button)
 
@@ -545,6 +550,11 @@ def unregister():
     del bpy.types.Object.selection_sets
     del bpy.types.Object.active_selection_set
 
+    # handle the keymap
+    for km, kmi in addon_keymaps:
+        km.keymap_items.remove(kmi)
+    addon_keymaps.clear()
+
 
 if __name__ == "__main__":
     import doctest
-- 
GitLab