Q (1) What’s the best practice with addon key bindings?
I couldn’t find clear consensus on this. It seems that addon authors do as they see best.
However, there is an approach laid out by Bookyakuno which attmepts to make this easier by creating a layout for items in a list of keymap items (addon_keymaps
). This code might be helpful for others as-is, or as a starting point:
I would welcome further input on this
Q (2) How can I associate a keymap item with a property (like colour) in addon preferences?
I felt like I was going in circles on this one!
Demo/example:
I’m not sure why this happens, but seems that if, say, the .active
property is set to True
on the keybinding, this causes the binding to update. This is the case even if kmi.active == True
already!
This feels like a bug, but I don’t have the experience to say for sure.
Obviously guddling with the console to update a key binding-associated property is not convenient!
Thankfully there is a workaround- a “save key bindings” operator can set the required property:
Code
class SAMPLE_OT_DirtyKeymap(bpy.types.Operator) :
bl_idname = "addon.sample_dirty_keymap"
bl_label = "Save Keymap"
def execute(self, context):
km = context.window_manager.keyconfigs.user.keymaps["MyAddon"] :
km.show_expanded_items = km.show_expanded_items
for kmi in km.keymap_items :
kmi.active = kmi.active
context.preferences.is_dirty = True
return {'FINISHED'}
This seems to work:
But it is very much a workaround!