You're likely the first person who has attempted this, and it looks like its quite possible;
Notes
- Save this script to your addons dir with a unique name (ending with
.py)
- The keymaps are added as is normal for an addon.
- There is one bug at the moment, pressing the 'X' buttons isn't properly removing them. (needs further investigation we should be able to fix)
- Finally - this uses a part of the API we consider internal, so its not guaranteed to work in future versions (realistically it probably wont change any time soon though).
Script:
bl_info = {"name": "KeyMap Test", "category": "Object"}
import bpy
from bpy.types import Operator, AddonPreferences
from bpy.props import StringProperty, IntProperty, BoolProperty
# this is an internal API at the moment
import rna_keymap_ui
class ExampleAddonPreferences(AddonPreferences):
# this must match the addon name, use '__package__'
# when defining this in a submodule of a python package.
bl_idname = __name__
filepath = StringProperty(
name="Example File Path",
subtype='FILE_PATH',
)
number = IntProperty(
name="Example Number",
default=4,
)
boolean = BoolProperty(
name="Example Boolean",
default=False,
)
def draw(self, context):
layout = self.layout
layout.label(text="This is a preferences view for our addon")
layout.prop(self, "filepath")
row = layout.row()
row.prop(self, "number")
row.prop(self, "boolean")
col = layout.column()
kc = bpy.context.window_manager.keyconfigs.addon
for km, kmi in addon_keymaps:
km = km.active()
col.context_pointer_set("keymap", km)
rna_keymap_ui.draw_kmi([], kc, km, kmi, col, 0)
class OBJECT_OT_addon_prefs_example(Operator):
"""Display example preferences"""
bl_idname = "object.addon_prefs_example"
bl_label = "Addon Preferences Example"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
user_preferences = context.user_preferences
addon_prefs = user_preferences.addons[__name__].preferences
info = ("Path: %s, Number: %d, Boolean %r" %
(addon_prefs.filepath, addon_prefs.number, addon_prefs.boolean))
self.report({'INFO'}, info)
print(info)
return {'FINISHED'}
addon_keymaps = []
# Registration
def register():
bpy.utils.register_class(OBJECT_OT_addon_prefs_example)
bpy.utils.register_class(ExampleAddonPreferences)
# Keymapping
kc = bpy.context.window_manager.keyconfigs.addon
km = kc.keymaps.new(name="3D View", space_type='VIEW_3D')
kmi = km.keymap_items.new("object.editmode_toggle", 'NUMPAD_SLASH', 'PRESS', shift=True)
kmi.active = True
addon_keymaps.append((km, kmi))
kmi = km.keymap_items.new("object.transform_apply", 'NUMPAD_SLASH', 'PRESS', shift=True)
kmi.active = True
addon_keymaps.append((km, kmi))
def unregister():
bpy.utils.unregister_class(OBJECT_OT_addon_prefs_example)
bpy.utils.unregister_class(ExampleAddonPreferences)
# handle the keymap
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
rna_keymap_uiuse. – ideasman42 Aug 20 '13 at 02:51