[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [61073] trunk/blender/release/scripts/ modules/bpy_extras/keyconfig_utils.py: Fix [#37275] can't import 2. 68a keymap

Bastien Montagne montagne29 at wanadoo.fr
Sun Nov 3 19:04:46 CET 2013


Revision: 61073
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=61073
Author:   mont29
Date:     2013-11-03 18:04:45 +0000 (Sun, 03 Nov 2013)
Log Message:
-----------
Fix [#37275] can't import 2.68a keymap

Now exported keymaps will still be usable accross versions, even if some operator properties disappear (write a warning in console in this case, instead of "crashing").

Also factorized a bit of code here!

Modified Paths:
--------------
    trunk/blender/release/scripts/modules/bpy_extras/keyconfig_utils.py

Modified: trunk/blender/release/scripts/modules/bpy_extras/keyconfig_utils.py
===================================================================
--- trunk/blender/release/scripts/modules/bpy_extras/keyconfig_utils.py	2013-11-03 16:02:33 UTC (rev 61072)
+++ trunk/blender/release/scripts/modules/bpy_extras/keyconfig_utils.py	2013-11-03 18:04:45 UTC (rev 61073)
@@ -151,7 +151,7 @@
     return merged_keymaps
 
 
-def _export_properties(prefix, properties, lines=None):
+def _export_properties(prefix, properties, kmi_id, lines=None):
     from bpy.types import OperatorProperties
 
     if lines is None:
@@ -171,20 +171,58 @@
         if pname != "rna_type":
             value = getattr(properties, pname)
             if isinstance(value, OperatorProperties):
-                _export_properties(prefix + "." + pname, value, lines)
+                _export_properties(prefix + "." + pname, value, kmi_id, lines)
             elif properties.is_property_set(pname):
                 value = string_value(value)
                 if value != "":
-                    lines.append("%s.%s = %s\n" % (prefix, pname, value))
+                    lines.append("set_kmi_prop(%s, '%s', %s, '%s')\n" % (prefix, pname, value, kmi_id))
     return lines
 
 
+def _kmistr(kmi, is_modal):
+    if is_modal:
+        kmi_id = kmi.propvalue
+        kmi_newfunc = 'new_modal'
+    else:
+        kmi_id = kmi.idname
+        kmi_newfunc = 'new'
+    s = ["kmi = km.keymap_items.%s(\'%s\', \'%s\', \'%s\'" % (kmi_newfunc, kmi_id, kmi.type, kmi.value)]
+
+    if kmi.any:
+        s.append(", any=True")
+    else:
+        if kmi.shift:
+            s.append(", shift=True")
+        if kmi.ctrl:
+            s.append(", ctrl=True")
+        if kmi.alt:
+            s.append(", alt=True")
+        if kmi.oskey:
+            s.append(", oskey=True")
+    if kmi.key_modifier and kmi.key_modifier != 'NONE':
+        s.append(", key_modifier=\'%s\'" % kmi.key_modifier)
+
+    s.append(")\n")
+
+    props = kmi.properties
+
+    if props is not None:
+        _export_properties("kmi.properties", props, kmi_id, s)
+
+    return "".join(s)
+
+
 def keyconfig_export(wm, kc, filepath):
 
     f = open(filepath, "w")
 
     f.write("import bpy\n")
     f.write("import os\n\n")
+    f.write("def set_kmi_prop(kmiprops, prop, value, kmiid):\n"
+            "    if hasattr(kmiprops, prop):\n"
+            "        setattr(kmiprops, prop, value)\n"
+            "    else:\n"
+            "        print(\"Warning: property '%s' not found in keymap item '%s'\" % (prop, kmiid))\n\n")
     f.write("wm = bpy.context.window_manager\n")
     f.write("kc = wm.keyconfigs.new(os.path.splitext(os.path.basename(__file__))[0])\n\n")  # keymap must be created by caller
 
@@ -216,30 +254,7 @@
         f.write("# Map %s\n" % km.name)
         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))
         for kmi in km.keymap_items:
-            if km.is_modal:
-                f.write("kmi = km.keymap_items.new_modal('%s', '%s', '%s'" % (kmi.propvalue, kmi.type, kmi.value))
-            else:
-                f.write("kmi = km.keymap_items.new('%s', '%s', '%s'" % (kmi.idname, kmi.type, kmi.value))
-            if kmi.any:
-                f.write(", any=True")
-            else:
-                if kmi.shift:
-                    f.write(", shift=True")
-                if kmi.ctrl:
-                    f.write(", ctrl=True")
-                if kmi.alt:
-                    f.write(", alt=True")
-                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(")\n")
-
-            props = kmi.properties
-
-            if props is not None:
-                f.write("".join(_export_properties("kmi.properties", props)))
-
+            f.write(_kmistr(kmi, km.is_modal))
         f.write("\n")
 
     f.close()
@@ -250,50 +265,22 @@
     def testEntry(kc, entry, src=None, parent=None):
         result = False
 
-        def kmistr(kmi):
-            if km.is_modal:
-                s = ["kmi = km.keymap_items.new_modal(\'%s\', \'%s\', \'%s\'" % (kmi.propvalue, kmi.type, kmi.value)]
-            else:
-                s = ["kmi = km.keymap_items.new(\'%s\', \'%s\', \'%s\'" % (kmi.idname, kmi.type, kmi.value)]
-
-            if kmi.any:
-                s.append(", any=True")
-            else:
-                if kmi.shift:
-                    s.append(", shift=True")
-                if kmi.ctrl:
-                    s.append(", ctrl=True")
-                if kmi.alt:
-                    s.append(", alt=True")
-                if kmi.oskey:
-                    s.append(", oskey=True")
-            if kmi.key_modifier and kmi.key_modifier != 'NONE':
-                s.append(", key_modifier=\'%s\'" % kmi.key_modifier)
-
-            s.append(")\n")
-
-            props = kmi.properties
-
-            if props is not None:
-                _export_properties("kmi.properties", props, s)
-
-            return "".join(s).strip()
-
         idname, spaceid, regionid, children = entry
 
         km = kc.keymaps.find(idname, space_type=spaceid, region_type=regionid)
 
         if km:
             km = km.active()
+            is_modal = km.is_modal
 
             if src:
                 for item in km.keymap_items:
                     if src.compare(item):
                         print("===========")
                         print(parent.name)
-                        print(kmistr(src))
+                        print(_kmistr(src, is_modal).strip())
                         print(km.name)
-                        print(kmistr(item))
+                        print(_kmistr(item, is_modal).strip())
                         result = True
 
                 for child in children:
@@ -312,8 +299,8 @@
                         if src.compare(item):
                             print("===========")
                             print(km.name)
-                            print(kmistr(src))
-                            print(kmistr(item))
+                            print(_kmistr(src, is_modal).strip())
+                            print(_kmistr(item, is_modal).strip())
                             result = True
 
                 for child in children:




More information about the Bf-blender-cvs mailing list