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 #####
22 ('Window', 'EMPTY', 'WINDOW', []), # file save, window change, exit
23 ('Screen', 'EMPTY', 'WINDOW', [ # full screen, undo, screenshot
24 ('Screen Editing', 'EMPTY', 'WINDOW', []), # resizing, action corners
27 ('View2D', 'EMPTY', 'WINDOW', []), # view 2d navigation (per region)
28 ('View2D Buttons List', 'EMPTY', 'WINDOW', []), # view 2d with buttons navigation
29 ('Header', 'EMPTY', 'WINDOW', []), # header stuff (per region)
30 ('Grease Pencil', 'EMPTY', 'WINDOW', []), # grease pencil stuff (per region)
32 ('3D View', 'VIEW_3D', 'WINDOW', [ # view 3d navigation and generic stuff (select, transform)
33 ('Object Mode', 'EMPTY', 'WINDOW', []),
34 ('Mesh', 'EMPTY', 'WINDOW', []),
35 ('Curve', 'EMPTY', 'WINDOW', []),
36 ('Armature', 'EMPTY', 'WINDOW', []),
37 ('Metaball', 'EMPTY', 'WINDOW', []),
38 ('Lattice', 'EMPTY', 'WINDOW', []),
39 ('Font', 'EMPTY', 'WINDOW', []),
41 ('Pose', 'EMPTY', 'WINDOW', []),
43 ('Vertex Paint', 'EMPTY', 'WINDOW', []),
44 ('Weight Paint', 'EMPTY', 'WINDOW', []),
45 ('Face Mask', 'EMPTY', 'WINDOW', []),
46 ('Image Paint', 'EMPTY', 'WINDOW', []), # image and view3d
47 ('Sculpt', 'EMPTY', 'WINDOW', []),
49 ('Armature Sketch', 'EMPTY', 'WINDOW', []),
50 ('Particle', 'EMPTY', 'WINDOW', []),
52 ('Object Non-modal', 'EMPTY', 'WINDOW', []), # mode change
54 ('3D View Generic', 'VIEW_3D', 'WINDOW', []) # toolbar and properties
57 ('Frames', 'EMPTY', 'WINDOW', []), # frame navigation (per region)
58 ('Markers', 'EMPTY', 'WINDOW', []), # markers (per region)
59 ('Animation', 'EMPTY', 'WINDOW', []), # frame change on click, preview range (per region)
60 ('Animation Channels', 'EMPTY', 'WINDOW', []),
61 ('Graph Editor', 'GRAPH_EDITOR', 'WINDOW', [
62 ('Graph Editor Generic', 'GRAPH_EDITOR', 'WINDOW', [])
64 ('Dopesheet', 'DOPESHEET_EDITOR', 'WINDOW', []),
65 ('NLA Editor', 'NLA_EDITOR', 'WINDOW', [
66 ('NLA Channels', 'NLA_EDITOR', 'WINDOW', []),
67 ('NLA Generic', 'NLA_EDITOR', 'WINDOW', [])
70 ('Image', 'IMAGE_EDITOR', 'WINDOW', [
71 ('UV Editor', 'EMPTY', 'WINDOW', []), # image (reverse order, UVEdit before Image
72 ('Image Paint', 'EMPTY', 'WINDOW', []), # image and view3d
73 ('Image Generic', 'IMAGE_EDITOR', 'WINDOW', [])
76 ('Timeline', 'TIMELINE', 'WINDOW', []),
77 ('Outliner', 'OUTLINER', 'WINDOW', []),
79 ('Node Editor', 'NODE_EDITOR', 'WINDOW', [
80 ('Node Generic', 'NODE_EDITOR', 'WINDOW', [])
82 ('Sequencer', 'SEQUENCE_EDITOR', 'WINDOW', []),
83 ('Logic Editor', 'LOGIC_EDITOR', 'WINDOW', []),
85 ('File Browser', 'FILE_BROWSER', 'WINDOW', [
86 ('File Browser Main', 'FILE_BROWSER', 'WINDOW', []),
87 ('File Browser Buttons', 'FILE_BROWSER', 'WINDOW', [])
90 ('Property Editor', 'PROPERTIES', 'WINDOW', []), # align context menu
92 ('Script', 'SCRIPTS_WINDOW', 'WINDOW', []),
93 ('Text', 'TEXT_EDITOR', 'WINDOW', []),
94 ('Console', 'CONSOLE', 'WINDOW', []),
95 ('Clip Editor', 'CLIP_EDITOR', 'WINDOW', []),
96 ('Clip Globals', 'CLIP_EDITOR', 'WINDOW', []),
98 ('View3D Gesture Circle', 'EMPTY', 'WINDOW', []),
99 ('Gesture Border', 'EMPTY', 'WINDOW', []),
100 ('Standard Modal Map', 'EMPTY', 'WINDOW', []),
101 ('Transform Modal Map', 'EMPTY', 'WINDOW', []),
102 ('View3D Fly Modal', 'EMPTY', 'WINDOW', []),
103 ('View3D Rotate Modal', 'EMPTY', 'WINDOW', []),
104 ('View3D Move Modal', 'EMPTY', 'WINDOW', []),
105 ('View3D Zoom Modal', 'EMPTY', 'WINDOW', []),
109 # -----------------------------------------------------------------------------
112 def km_exists_in(km, export_keymaps):
113 for km2, kc in export_keymaps:
114 if km2.name == km.name:
119 def keyconfig_merge(kc1, kc2):
120 """ note: kc1 takes priority over kc2
122 merged_keymaps = [(km, kc1) for km in kc1.keymaps]
124 merged_keymaps.extend((km, kc2) for km in kc2.keymaps if not _km_exists_in(km, merged_keymaps))
126 return merged_keymaps
129 def keyconfig_export(wm, kc, filepath):
130 from bpy.types import OperatorProperties
132 def string_value(value):
133 if isinstance(value, str) or isinstance(value, bool) or isinstance(value, float) or isinstance(value, int):
135 elif getattr(value, '__len__', False):
136 return repr(list(value))
138 print("Export key configuration: can't write ", value)
142 def export_properties(prefix, properties, lines=None):
146 for pname in properties.bl_rna.properties.keys():
147 if pname != "rna_type" and not properties.is_property_hidden(pname):
148 value = getattr(properties, pname)
149 if isinstance(value, OperatorProperties):
150 export_properties(prefix + "." + pname, value, lines)
151 elif properties.is_property_set(pname):
152 value = string_value(value)
154 lines.append("%s.%s = %s\n" % (prefix, pname, value))
157 f = open(filepath, "w")
159 f.write("import bpy\n")
160 f.write("import os\n\n")
161 f.write("wm = bpy.context.window_manager\n")
162 f.write("kc = wm.keyconfigs.new(os.path.splitext(os.path.basename(__file__))[0])\n\n") # keymap must be created by caller
164 # Generate a list of keymaps to export:
166 # First add all user_modified keymaps (found in keyconfigs.user.keymaps list),
167 # then add all remaining keymaps from the currently active custom keyconfig.
169 # This will create a final list of keymaps that can be used as a 'diff' against
170 # the default blender keyconfig, recreating the current setup from a fresh blender
171 # without needing to export keymaps which haven't been edited.
173 class FakeKeyConfig():
175 edited_kc = FakeKeyConfig()
176 for km in wm.keyconfigs.user.keymaps:
177 if km.is_user_modified:
178 edited_kc.keymaps.append(km)
179 # merge edited keymaps with non-default keyconfig, if it exists
180 if kc != wm.keyconfigs.default:
181 export_keymaps = keyconfig_merge(edited_kc, kc)
183 export_keymaps = keyconfig_merge(edited_kc, edited_kc)
185 for km, kc_x in export_keymaps:
189 f.write("# Map %s\n" % km.name)
190 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))
191 for kmi in km.keymap_items:
193 f.write("kmi = km.keymap_items.new_modal('%s', '%s', '%s'" % (kmi.propvalue, kmi.type, kmi.value))
195 f.write("kmi = km.keymap_items.new('%s', '%s', '%s'" % (kmi.idname, kmi.type, kmi.value))
197 f.write(", any=True")
200 f.write(", shift=True")
202 f.write(", ctrl=True")
204 f.write(", alt=True")
206 f.write(", oskey=True")
207 if kmi.key_modifier and kmi.key_modifier != 'NONE':
208 f.write(", key_modifier='%s'" % kmi.key_modifier)
211 props = kmi.properties
213 if props is not None:
214 f.write("".join(export_properties("kmi.properties", props)))
221 def keyconfig_test(kc):
223 def testEntry(kc, entry, src=None, parent=None):
228 s = ["kmi = km.keymap_items.new_modal(\'%s\', \'%s\', \'%s\'" % (kmi.propvalue, kmi.type, kmi.value)]
230 s = ["kmi = km.keymap_items.new(\'%s\', \'%s\', \'%s\'" % (kmi.idname, kmi.type, kmi.value)]
233 s.append(", any=True")
236 s.append(", shift=True")
238 s.append(", ctrl=True")
240 s.append(", alt=True")
242 s.append(", oskey=True")
243 if kmi.key_modifier and kmi.key_modifier != 'NONE':
244 s.append(", key_modifier=\'%s\'" % kmi.key_modifier)
248 props = kmi.properties
250 if props is not None:
251 export_properties("kmi.properties", props, s)
253 return "".join(s).strip()
255 idname, spaceid, regionid, children = entry
257 km = kc.keymaps.find(idname, space_type=spaceid, region_type=regionid)
263 for item in km.keymap_items:
264 if src.compare(item):
272 for child in children:
273 if testEntry(kc, child, src, parent):
276 for i in range(len(km.keymap_items)):
277 src = km.keymap_items[i]
279 for child in children:
280 if testEntry(kc, child, src, km):
283 for j in range(len(km.keymap_items) - i - 1):
284 item = km.keymap_items[j + i + 1]
285 if src.compare(item):
292 for child in children:
293 if testEntry(kc, child):
298 # -------------------------------------------------------------------------
302 for entry in KM_HIERARCHY:
303 if testEntry(kc, entry):