1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
25 ('Window', 'EMPTY', 'WINDOW', []), # file save, window change, exit
26 ('Screen', 'EMPTY', 'WINDOW', [ # full screen, undo, screenshot
27 ('Screen Editing', 'EMPTY', 'WINDOW', []), # resizing, action corners
30 ('View2D', 'EMPTY', 'WINDOW', []), # view 2d navigation (per region)
31 ('View2D Buttons List', 'EMPTY', 'WINDOW', []), # view 2d with buttons navigation
32 ('Header', 'EMPTY', 'WINDOW', []), # header stuff (per region)
33 ('Grease Pencil', 'EMPTY', 'WINDOW', []), # grease pencil stuff (per region)
35 ('3D View', 'VIEW_3D', 'WINDOW', [ # view 3d navigation and generic stuff (select, transform)
36 ('Object Mode', 'EMPTY', 'WINDOW', []),
37 ('Mesh', 'EMPTY', 'WINDOW', []),
38 ('Curve', 'EMPTY', 'WINDOW', []),
39 ('Armature', 'EMPTY', 'WINDOW', []),
40 ('Metaball', 'EMPTY', 'WINDOW', []),
41 ('Lattice', 'EMPTY', 'WINDOW', []),
42 ('Font', 'EMPTY', 'WINDOW', []),
44 ('Pose', 'EMPTY', 'WINDOW', []),
46 ('Vertex Paint', 'EMPTY', 'WINDOW', []),
47 ('Weight Paint', 'EMPTY', 'WINDOW', []),
48 ('Face Mask', 'EMPTY', 'WINDOW', []),
49 ('Image Paint', 'EMPTY', 'WINDOW', []), # image and view3d
50 ('Sculpt', 'EMPTY', 'WINDOW', []),
52 ('Armature Sketch', 'EMPTY', 'WINDOW', []),
53 ('Particle', 'EMPTY', 'WINDOW', []),
55 ('Object Non-modal', 'EMPTY', 'WINDOW', []), # mode change
57 ('3D View Generic', 'VIEW_3D', 'WINDOW', []) # toolbar and properties
60 ('Frames', 'EMPTY', 'WINDOW', []), # frame navigation (per region)
61 ('Markers', 'EMPTY', 'WINDOW', []), # markers (per region)
62 ('Animation', 'EMPTY', 'WINDOW', []), # frame change on click, preview range (per region)
63 ('Animation Channels', 'EMPTY', 'WINDOW', []),
64 ('Graph Editor', 'GRAPH_EDITOR', 'WINDOW', [
65 ('Graph Editor Generic', 'GRAPH_EDITOR', 'WINDOW', [])
67 ('Dopesheet', 'DOPESHEET_EDITOR', 'WINDOW', []),
68 ('NLA Editor', 'NLA_EDITOR', 'WINDOW', [
69 ('NLA Channels', 'NLA_EDITOR', 'WINDOW', []),
70 ('NLA Generic', 'NLA_EDITOR', 'WINDOW', [])
73 ('Image', 'IMAGE_EDITOR', 'WINDOW', [
74 ('UV Editor', 'EMPTY', 'WINDOW', []), # image (reverse order, UVEdit before Image
75 ('Image Paint', 'EMPTY', 'WINDOW', []), # image and view3d
76 ('Image Generic', 'IMAGE_EDITOR', 'WINDOW', [])
79 ('Timeline', 'TIMELINE', 'WINDOW', []),
80 ('Outliner', 'OUTLINER', 'WINDOW', []),
82 ('Node Editor', 'NODE_EDITOR', 'WINDOW', [
83 ('Node Generic', 'NODE_EDITOR', 'WINDOW', [])
85 ('Sequencer', 'SEQUENCE_EDITOR', 'WINDOW', []),
86 ('Logic Editor', 'LOGIC_EDITOR', 'WINDOW', []),
88 ('File Browser', 'FILE_BROWSER', 'WINDOW', [
89 ('File Browser Main', 'FILE_BROWSER', 'WINDOW', []),
90 ('File Browser Buttons', 'FILE_BROWSER', 'WINDOW', [])
93 ('Property Editor', 'PROPERTIES', 'WINDOW', []), # align context menu
95 ('Script', 'SCRIPTS_WINDOW', 'WINDOW', []),
96 ('Text', 'TEXT_EDITOR', 'WINDOW', []),
97 ('Console', 'CONSOLE', 'WINDOW', []),
98 ('Clip', 'CLIP_EDITOR', 'WINDOW', []),
100 ('View3D Gesture Circle', 'EMPTY', 'WINDOW', []),
101 ('Gesture Border', 'EMPTY', 'WINDOW', []),
102 ('Standard Modal Map', 'EMPTY', 'WINDOW', []),
103 ('Transform Modal Map', 'EMPTY', 'WINDOW', []),
104 ('View3D Fly Modal', 'EMPTY', 'WINDOW', []),
105 ('View3D Rotate Modal', 'EMPTY', 'WINDOW', []),
106 ('View3D Move Modal', 'EMPTY', 'WINDOW', []),
107 ('View3D Zoom Modal', 'EMPTY', 'WINDOW', []),
111 def _km_exists_in(km, export_keymaps):
112 for km2, kc in export_keymaps:
113 if km2.name == km.name:
118 def _merge_keymaps(kc1, kc2):
119 """ note: kc1 takes priority over kc2
121 merged_keymaps = [(km, kc1) for km in kc1.keymaps]
123 merged_keymaps.extend((km, kc2) for km in kc2.keymaps if not _km_exists_in(km, merged_keymaps))
125 return merged_keymaps
128 class USERPREF_MT_keyconfigs(bpy.types.Menu):
129 bl_label = "KeyPresets"
130 preset_subdir = "keyconfig"
131 preset_operator = "wm.keyconfig_activate"
133 def draw(self, context):
134 props = self.layout.operator("wm.context_set_value", text="Blender (default)")
135 props.data_path = "window_manager.keyconfigs.active"
136 props.value = "context.window_manager.keyconfigs.default"
138 # now draw the presets
139 bpy.types.Menu.draw_preset(self, context)
142 class InputKeyMapPanel:
143 bl_space_type = 'USER_PREFERENCES'
145 bl_region_type = 'WINDOW'
146 bl_options = {'HIDE_HEADER'}
148 def draw_entry(self, display_keymaps, entry, col, level=0):
149 idname, spaceid, regionid, children = entry
151 for km, kc in display_keymaps:
152 if km.name == idname and km.space_type == spaceid and km.region_type == regionid:
153 self.draw_km(display_keymaps, kc, km, children, col, level)
156 km = kc.keymaps.find(idname, space_type=spaceid, region_type=regionid)
159 km = kc.keymaps.find(idname, space_type=spaceid, region_type=regionid)
162 self.draw_km(kc, km, children, col, level)
165 def indented_layout(self, layout, level):
168 level = 0.0001 # Tweak so that a percentage of 0 won't split by half
169 indent = level * indentpx / bpy.context.region.width
171 split = layout.split(percentage=indent)
176 def draw_km(self, display_keymaps, kc, km, children, layout, level):
179 layout.context_pointer_set("keymap", km)
181 col = self.indented_layout(layout, level)
184 row.prop(km, "show_expanded_children", text="", emboss=False)
185 row.label(text=km.name)
191 row.label(text="", icon='LINKED')
192 if km.is_user_defined:
193 row.operator("wm.keymap_restore", text="Restore")
195 row.operator("wm.keymap_edit", text="Edit")
197 if km.show_expanded_children:
199 # Put the Parent key map's entries in a 'global' sub-category
200 # equal in hierarchy to the other children categories
201 subcol = self.indented_layout(col, level + 1)
202 subrow = subcol.row()
203 subrow.prop(km, "show_expanded_items", text="", emboss=False)
204 subrow.label(text="%s (Global)" % km.name)
206 km.show_expanded_items = True
209 if km.show_expanded_items:
210 for kmi in km.keymap_items:
211 self.draw_kmi(display_keymaps, kc, km, kmi, col, level + 1)
213 # "Add New" at end of keymap item list
214 col = self.indented_layout(col, level + 1)
215 subcol = col.split(percentage=0.2).column()
216 subcol.enabled = km.is_user_defined
217 subcol.operator("wm.keyitem_add", text="Add New", icon='ZOOMIN')
223 subcol = col.column()
226 for entry in children:
227 self.draw_entry(display_keymaps, entry, col, level + 1)
230 def draw_kmi_properties(box, properties, title=None):
233 box.label(text=title)
234 flow = box.column_flow(columns=2)
235 for pname, value in properties.bl_rna.properties.items():
236 if pname != "rna_type" and not properties.is_property_hidden(pname):
237 if isinstance(value, bpy.types.OperatorProperties):
238 InputKeyMapPanel.draw_kmi_properties(box, value, title=pname)
240 flow.prop(properties, pname)
242 def draw_kmi(self, display_keymaps, kc, km, kmi, layout, level):
243 map_type = kmi.map_type
245 col = self.indented_layout(layout, level)
247 if km.is_user_defined:
248 col = col.column(align=True)
253 split = box.split(percentage=0.05)
257 row.prop(kmi, "show_expanded", text="", emboss=False)
260 row.enabled = km.is_user_defined
261 row.prop(kmi, "active", text="", emboss=False)
264 row.prop(kmi, "propvalue", text="")
266 row.label(text=kmi.name)
269 row.enabled = km.is_user_defined
270 row.prop(kmi, "map_type", text="")
271 if map_type == 'KEYBOARD':
272 row.prop(kmi, "type", text="", full_event=True)
273 elif map_type == 'MOUSE':
274 row.prop(kmi, "type", text="", full_event=True)
275 elif map_type == 'TWEAK':
277 subrow.prop(kmi, "type", text="")
278 subrow.prop(kmi, "value", text="")
279 elif map_type == 'TIMER':
280 row.prop(kmi, "type", text="")
284 if not kmi.is_user_defined:
285 op = row.operator("wm.keyitem_restore", text="", icon='BACK')
287 op = row.operator("wm.keyitem_remove", text="", icon='X')
290 # Expanded, additional event settings
291 if kmi.show_expanded:
294 box.enabled = km.is_user_defined
296 if map_type not in {'TEXTINPUT', 'TIMER'}:
297 split = box.split(percentage=0.4)
301 sub.prop(kmi, "propvalue", text="")
304 # sub.prop_search(kmi, "idname", bpy.context.window_manager, "operators_all", text="")
305 sub.prop(kmi, "idname", text="")
308 subrow = sub.row(align=True)
310 if map_type == 'KEYBOARD':
311 subrow.prop(kmi, "type", text="", event=True)
312 subrow.prop(kmi, "value", text="")
313 elif map_type == 'MOUSE':
314 subrow.prop(kmi, "type", text="")
315 subrow.prop(kmi, "value", text="")
318 subrow.scale_x = 0.75
319 subrow.prop(kmi, "any")
320 subrow.prop(kmi, "shift")
321 subrow.prop(kmi, "ctrl")
322 subrow.prop(kmi, "alt")
323 subrow.prop(kmi, "oskey", text="Cmd")
324 subrow.prop(kmi, "key_modifier", text="", event=True)
326 # Operator properties
327 props = kmi.properties
328 if props is not None:
329 InputKeyMapPanel.draw_kmi_properties(box, props)
331 # Modal key maps attached to this operator
333 kmm = kc.keymaps.find_modal(kmi.idname)
335 self.draw_km(display_keymaps, kc, kmm, None, layout, level + 1)
336 layout.context_pointer_set("keymap", km)
338 def draw_filtered(self, display_keymaps, filter_text, layout):
339 for km, kc in display_keymaps:
341 layout.context_pointer_set("keymap", km)
343 filtered_items = [kmi for kmi in km.keymap_items if filter_text in kmi.name.lower()]
345 if len(filtered_items) != 0:
346 col = layout.column()
349 row.label(text=km.name, icon="DOT")
354 if km.is_user_defined:
355 row.operator("wm.keymap_restore", text="Restore")
357 row.operator("wm.keymap_edit", text="Edit")
359 for kmi in filtered_items:
360 self.draw_kmi(display_keymaps, kc, km, kmi, col, 1)
362 # "Add New" at end of keymap item list
363 col = self.indented_layout(layout, 1)
364 subcol = col.split(percentage=0.2).column()
365 subcol.enabled = km.is_user_defined
366 subcol.operator("wm.keyitem_add", text="Add New", icon='ZOOMIN')
368 def draw_hierarchy(self, display_keymaps, layout):
369 for entry in KM_HIERARCHY:
370 self.draw_entry(display_keymaps, entry, layout)
372 def draw_keymaps(self, context, layout):
373 wm = context.window_manager
374 kc = wm.keyconfigs.active
375 defkc = wm.keyconfigs.default
377 col = layout.column()
380 subsplit = sub.split()
381 subcol = subsplit.column()
383 row = subcol.row(align=True)
385 #row.prop_search(wm.keyconfigs, "active", wm, "keyconfigs", text="Key Config:")
386 text = bpy.path.display_name(context.window_manager.keyconfigs.active.name)
388 text = "Blender (default)"
389 row.menu("USERPREF_MT_keyconfigs", text=text)
390 row.operator("wm.keyconfig_preset_add", text="", icon="ZOOMIN")
391 row.operator("wm.keyconfig_preset_add", text="", icon="ZOOMOUT").remove_active = True
393 # layout.context_pointer_set("keyconfig", wm.keyconfigs.active)
394 # row.operator("wm.keyconfig_remove", text="", icon='X')
396 row.prop(context.space_data, "filter_text", icon="VIEWZOOM")
400 display_keymaps = _merge_keymaps(kc, defkc)
401 if context.space_data.filter_text != "":
402 filter_text = context.space_data.filter_text.lower()
403 self.draw_filtered(display_keymaps, filter_text, col)
405 self.draw_hierarchy(display_keymaps, col)
408 from bpy.props import StringProperty, BoolProperty, IntProperty
411 def export_properties(prefix, properties, lines=None):
415 for pname in properties.bl_rna.properties.keys():
416 if pname != "rna_type" and not properties.is_property_hidden(pname):
417 value = getattr(properties, pname)
418 if isinstance(value, bpy.types.OperatorProperties):
419 export_properties(prefix + "." + pname, value, lines)
420 elif properties.is_property_set(pname):
421 value = _string_value(value)
423 lines.append("%s.%s = %s\n" % (prefix, pname, value))
427 class WM_OT_keyconfig_test(bpy.types.Operator):
428 "Test keyconfig for conflicts"
429 bl_idname = "wm.keyconfig_test"
430 bl_label = "Test Key Configuration for Conflicts"
432 def testEntry(self, kc, entry, src=None, parent=None):
437 s = ["kmi = km.keymap_items.new_modal(\'%s\', \'%s\', \'%s\'" % (kmi.propvalue, kmi.type, kmi.value)]
439 s = ["kmi = km.keymap_items.new(\'%s\', \'%s\', \'%s\'" % (kmi.idname, kmi.type, kmi.value)]
442 s.append(", any=True")
445 s.append(", shift=True")
447 s.append(", ctrl=True")
449 s.append(", alt=True")
451 s.append(", oskey=True")
452 if kmi.key_modifier and kmi.key_modifier != 'NONE':
453 s.append(", key_modifier=\'%s\'" % kmi.key_modifier)
457 props = kmi.properties
459 if props is not None:
460 export_properties("kmi.properties", props, s)
462 return "".join(s).strip()
464 idname, spaceid, regionid, children = entry
466 km = kc.keymaps.find(idname, space_type=spaceid, region_type=regionid)
472 for item in km.keymap_items:
473 if src.compare(item):
481 for child in children:
482 if self.testEntry(kc, child, src, parent):
485 for i in range(len(km.keymap_items)):
486 src = km.keymap_items[i]
488 for child in children:
489 if self.testEntry(kc, child, src, km):
492 for j in range(len(km.keymap_items) - i - 1):
493 item = km.keymap_items[j + i + 1]
494 if src.compare(item):
501 for child in children:
502 if self.testEntry(kc, child):
507 def testConfig(self, kc):
509 for entry in KM_HIERARCHY:
510 if self.testEntry(kc, entry):
514 def execute(self, context):
515 wm = context.window_manager
516 kc = wm.keyconfigs.default
518 if self.testConfig(kc):
524 def _string_value(value):
525 if isinstance(value, str) or isinstance(value, bool) or isinstance(value, float) or isinstance(value, int):
527 elif getattr(value, '__len__', False):
528 return repr(list(value))
530 print("Export key configuration: can't write ", value)
535 class WM_OT_keyconfig_import(bpy.types.Operator):
536 "Import key configuration from a python script"
537 bl_idname = "wm.keyconfig_import"
538 bl_label = "Import Key Configuration..."
540 filepath = StringProperty(name="File Path", description="Filepath to write file to", default="keymap.py")
541 filter_folder = BoolProperty(name="Filter folders", description="", default=True, options={'HIDDEN'})
542 filter_text = BoolProperty(name="Filter text", description="", default=True, options={'HIDDEN'})
543 filter_python = BoolProperty(name="Filter python", description="", default=True, options={'HIDDEN'})
545 keep_original = BoolProperty(name="Keep original", description="Keep original file after copying to configuration folder", default=True)
547 def execute(self, context):
548 from os.path import basename
550 if not self.filepath:
551 raise Exception("Filepath not set")
553 f = open(self.filepath, "r")
555 raise Exception("Could not open file")
557 config_name = basename(self.filepath)
559 path = bpy.utils.user_resource('SCRIPTS', os.path.join("presets", "keyconfig"), create=True)
560 path = os.path.join(path, config_name)
562 if self.keep_original:
563 shutil.copy(self.filepath, path)
565 shutil.move(self.filepath, path)
567 # sneaky way to check we're actually running the code.
568 bpy.utils.keyconfig_set(path)
572 def invoke(self, context, event):
573 wm = context.window_manager
574 wm.fileselect_add(self)
575 return {'RUNNING_MODAL'}
577 # This operator is also used by interaction presets saving - AddPresetBase
580 class WM_OT_keyconfig_export(bpy.types.Operator):
581 "Export key configuration to a python script"
582 bl_idname = "wm.keyconfig_export"
583 bl_label = "Export Key Configuration..."
585 filepath = StringProperty(name="File Path", description="Filepath to write file to", default="keymap.py")
586 filter_folder = BoolProperty(name="Filter folders", description="", default=True, options={'HIDDEN'})
587 filter_text = BoolProperty(name="Filter text", description="", default=True, options={'HIDDEN'})
588 filter_python = BoolProperty(name="Filter python", description="", default=True, options={'HIDDEN'})
590 def execute(self, context):
591 if not self.filepath:
592 raise Exception("Filepath not set")
594 f = open(self.filepath, "w")
596 raise Exception("Could not open file")
598 wm = context.window_manager
599 kc = wm.keyconfigs.active
601 f.write("import bpy\n")
602 f.write("import os\n\n")
603 f.write("wm = bpy.context.window_manager\n")
604 f.write("kc = wm.keyconfigs.new(os.path.splitext(os.path.basename(__file__))[0])\n\n") # keymap must be created by caller
606 # Generate a list of keymaps to export:
608 # First add all user_defined keymaps (found in inputs.edited_keymaps list),
609 # then add all remaining keymaps from the currently active custom keyconfig.
611 # This will create a final list of keymaps that can be used as a 'diff' against
612 # the default blender keyconfig, recreating the current setup from a fresh blender
613 # without needing to export keymaps which haven't been edited.
615 class FakeKeyConfig():
617 edited_kc = FakeKeyConfig()
618 edited_kc.keymaps.extend(context.user_preferences.inputs.edited_keymaps)
619 # merge edited keymaps with non-default keyconfig, if it exists
620 if kc != wm.keyconfigs.default:
621 export_keymaps = _merge_keymaps(edited_kc, kc)
623 export_keymaps = _merge_keymaps(edited_kc, edited_kc)
625 for km, kc_x in export_keymaps:
629 f.write("# Map %s\n" % km.name)
630 f.write("km = kc.keymaps.new('%s', space_type='%s', region_type='%s', modal=%s)\n\n" % (km.name, km.space_type, km.region_type, km.is_modal))
631 for kmi in km.keymap_items:
633 f.write("kmi = km.keymap_items.new_modal('%s', '%s', '%s'" % (kmi.propvalue, kmi.type, kmi.value))
635 f.write("kmi = km.keymap_items.new('%s', '%s', '%s'" % (kmi.idname, kmi.type, kmi.value))
637 f.write(", any=True")
640 f.write(", shift=True")
642 f.write(", ctrl=True")
644 f.write(", alt=True")
646 f.write(", oskey=True")
647 if kmi.key_modifier and kmi.key_modifier != 'NONE':
648 f.write(", key_modifier='%s'" % kmi.key_modifier)
651 props = kmi.properties
653 if props is not None:
654 f.write("".join(export_properties("kmi.properties", props)))
662 def invoke(self, context, event):
663 wm = context.window_manager
664 wm.fileselect_add(self)
665 return {'RUNNING_MODAL'}
668 class WM_OT_keymap_edit(bpy.types.Operator):
669 "Edit stored key map"
670 bl_idname = "wm.keymap_edit"
671 bl_label = "Edit Key Map"
673 def execute(self, context):
679 class WM_OT_keymap_restore(bpy.types.Operator):
681 bl_idname = "wm.keymap_restore"
682 bl_label = "Restore Key Map(s)"
684 all = BoolProperty(name="All Keymaps", description="Restore all keymaps to default")
686 def execute(self, context):
687 wm = context.window_manager
690 for km in wm.keyconfigs.default.keymaps:
691 km.restore_to_default()
694 km.restore_to_default()
699 class WM_OT_keyitem_restore(bpy.types.Operator):
700 "Restore key map item"
701 bl_idname = "wm.keyitem_restore"
702 bl_label = "Restore Key Map Item"
704 item_id = IntProperty(name="Item Identifier", description="Identifier of the item to remove")
707 def poll(cls, context):
708 keymap = getattr(context, "keymap", None)
709 return keymap and keymap.is_user_defined
711 def execute(self, context):
713 kmi = km.keymap_items.from_id(self.item_id)
715 if not kmi.is_user_defined:
716 km.restore_item_to_default(kmi)
721 class WM_OT_keyitem_add(bpy.types.Operator):
723 bl_idname = "wm.keyitem_add"
724 bl_label = "Add Key Map Item"
726 def execute(self, context):
730 km.keymap_items.new_modal("", 'A', 'PRESS') # kmi
732 km.keymap_items.new("none", 'A', 'PRESS') # kmi
734 # clear filter and expand keymap so we can see the newly added item
735 if context.space_data.filter_text != "":
736 context.space_data.filter_text = ""
737 km.show_expanded_items = True
738 km.show_expanded_children = True
743 class WM_OT_keyitem_remove(bpy.types.Operator):
744 "Remove key map item"
745 bl_idname = "wm.keyitem_remove"
746 bl_label = "Remove Key Map Item"
748 item_id = IntProperty(name="Item Identifier", description="Identifier of the item to remove")
751 def poll(cls, context):
752 return hasattr(context, "keymap") and context.keymap.is_user_defined
754 def execute(self, context):
756 kmi = km.keymap_items.from_id(self.item_id)
757 km.keymap_items.remove(kmi)
761 class WM_OT_keyconfig_remove(bpy.types.Operator):
763 bl_idname = "wm.keyconfig_remove"
764 bl_label = "Remove Key Config"
767 def poll(cls, context):
768 wm = context.window_manager
769 keyconf = wm.keyconfigs.active
770 return keyconf and keyconf.is_user_defined
772 def execute(self, context):
773 wm = context.window_manager
774 keyconfig = wm.keyconfigs.active
775 wm.keyconfigs.remove(keyconfig)
778 if __name__ == "__main__": # only for live edit.
779 bpy.utils.register_module(__name__)