# <pep8 compliant>
import bpy
-import os.path
+import os, re, shutil
# General UI Theme Settings (User Interface)
def ui_items_general(col, context):
layout.operator_context = 'INVOKE_DEFAULT'
op = layout.operator("wm.keyconfig_export", "Export Key Configuration...")
op.path = "keymap.py"
+ op = layout.operator("wm.keyconfig_import", "Import Key Configuration...")
+ op.path = "keymap.py"
class USERPREF_PT_tabs(bpy.types.Panel):
subsplit = sub.split()
subcol = subsplit.column()
- subcol.prop_object(wm, "active_keyconfig", wm, "keyconfigs", text="Configuration:")
+ row = subcol.row()
+ row.prop_object(wm, "active_keyconfig", wm, "keyconfigs", text="Configuration:")
- subcol = subsplit.column()
- subcol.prop(kc, "filter", icon="VIEWZOOM")
+ layout.set_context_pointer("keyconfig", wm.active_keyconfig)
+ row.operator("wm.keyconfig_remove", text="", icon='X')
+
+ row.prop(kc, "filter", icon="VIEWZOOM")
col.separator()
return result
+class WM_OT_keyconfig_import(bpy.types.Operator):
+ "Import key configuration from a python script."
+ bl_idname = "wm.keyconfig_import"
+ bl_label = "Import Key Configuration..."
+
+ path = bpy.props.StringProperty(name="File Path", description="File path to write file to.")
+ filename = bpy.props.StringProperty(name="File Name", description="Name of the file.")
+ directory = bpy.props.StringProperty(name="Directory", description="Directory of the file.")
+ filter_folder = bpy.props.BoolProperty(name="Filter folders", description="", default=True, hidden=True)
+ filter_text = bpy.props.BoolProperty(name="Filter text", description="", default=True, hidden=True)
+ filter_python = bpy.props.BoolProperty(name="Filter python", description="", default=True, hidden=True)
+
+ keep_original = bpy.props.BoolProperty(name="Keep original", description="Keep original file after copying to configuration folder", default=True)
+
+ def execute(self, context):
+ if not self.properties.path:
+ raise Exception("File path not set.")
+
+ f = open(self.properties.path, "r")
+ if not f:
+ raise Exception("Could not open file.")
+
+ name_pattern = re.compile("^kc = wm.add_keyconfig\('(.*)'\)$")
+
+ for line in f.readlines():
+ match = name_pattern.match(line)
+
+ if match:
+ config_name = match.groups()[0]
+
+ f.close()
+
+ path = bpy.context.user_preferences.filepaths.python_scripts_directory
+
+ if not path:
+ path = os.path.split(__file__)[0]
+
+ path += os.path.sep + config_name + ".py"
+
+ if self.properties.keep_original:
+ shutil.copy(self.properties.path, path)
+ else:
+ shutil.move(self.properties.path, path)
+
+ __import__(config_name)
+
+ wm = bpy.data.window_managers[0]
+ wm.active_keyconfig = wm.keyconfigs[config_name]
+
+ return {'FINISHED'}
+ def invoke(self, context, event):
+ wm = context.manager
+ wm.add_fileselect(self)
+ return {'RUNNING_MODAL'}
+
class WM_OT_keyconfig_export(bpy.types.Operator):
"Export key configuration to a python script."
bl_idname = "wm.keyconfig_export"
path = bpy.props.StringProperty(name="File Path", description="File path to write file to.")
filename = bpy.props.StringProperty(name="File Name", description="Name of the file.")
directory = bpy.props.StringProperty(name="Directory", description="Directory of the file.")
- filter_folder = bpy.props.BoolProperty(name="Filter folders", description="", default=True)
- filter_text = bpy.props.BoolProperty(name="Filter text", description="", default=True)
- filter_python = bpy.props.BoolProperty(name="Filter python", description="", default=True)
+ filter_folder = bpy.props.BoolProperty(name="Filter folders", description="", default=True, hidden=True)
+ filter_text = bpy.props.BoolProperty(name="Filter text", description="", default=True, hidden=True)
+ filter_python = bpy.props.BoolProperty(name="Filter python", description="", default=True, hidden=True)
def execute(self, context):
if not self.properties.path:
else:
name = kc.name
- f.write('# Configuration %s\n' % name)
+ f.write("# Configuration %s\n" % name)
+ f.write("import bpy\n\n")
f.write("wm = bpy.data.window_managers[0]\n")
- f.write("kc = wm.add_keyconfig(\'%s\')\n\n" % name)
+ f.write("kc = wm.add_keyconfig('%s')\n\n" % name)
for km in kc.keymaps:
km = km.active()
f.write("# Map %s\n" % km.name)
- f.write("km = kc.add_keymap(\'%s\', space_type=\'%s\', region_type=\'%s\', modal=%s)\n\n" % (km.name, km.space_type, km.region_type, km.modal))
+ f.write("km = kc.add_keymap('%s', space_type='%s', region_type='%s', modal=%s)\n\n" % (km.name, km.space_type, km.region_type, km.modal))
for kmi in km.items:
if km.modal:
- f.write("kmi = km.add_modal_item(\'%s\', \'%s\', \'%s\'" % (kmi.propvalue, kmi.type, kmi.value))
+ f.write("kmi = km.add_modal_item('%s', '%s', '%s'" % (kmi.propvalue, kmi.type, kmi.value))
else:
- f.write("kmi = km.add_item(\'%s\', \'%s\', \'%s\'" % (kmi.idname, kmi.type, kmi.value))
+ f.write("kmi = km.add_item('%s', '%s', '%s'" % (kmi.idname, kmi.type, kmi.value))
if kmi.any:
f.write(", any=True")
else:
if kmi.oskey:
f.write(", oskey=True")
if kmi.key_modifier and kmi.key_modifier != 'NONE':
- f.write(", key_modifier=\'%s\'" % kmi.key_modifier)
+ f.write(", key_modifier='%s'" % kmi.key_modifier)
f.write(")\n")
def export_properties(prefix, properties):
f.write("\n")
- f.write("wm.active_keyconfig = wm.keyconfigs[\'%s\']\n" % name)
f.close()
return {'FINISHED'}
km.remove_item(kmi)
return {'FINISHED'}
+class WM_OT_keyconfig_remove(bpy.types.Operator):
+ "Remove key config."
+ bl_idname = "wm.keyconfig_remove"
+ bl_label = "Remove Key Config"
+
+ def poll(self, context):
+ wm = context.manager
+ return wm.active_keyconfig.user_defined
+
+ def execute(self, context):
+ wm = context.manager
+
+ keyconfig = wm.active_keyconfig
+
+ module = __import__(keyconfig.name)
+
+ os.remove(module.__file__)
+
+ wm.remove_keyconfig(keyconfig)
+ return {'FINISHED'}
+
bpy.types.register(WM_OT_keyconfig_export)
+bpy.types.register(WM_OT_keyconfig_import)
bpy.types.register(WM_OT_keyconfig_test)
+bpy.types.register(WM_OT_keyconfig_remove)
bpy.types.register(WM_OT_keymap_edit)
bpy.types.register(WM_OT_keymap_restore)
bpy.types.register(WM_OT_keyitem_add)