fix [#35574] Export Key Map issue
authorCampbell Barton <ideasman42@gmail.com>
Tue, 11 Jun 2013 15:11:55 +0000 (15:11 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 11 Jun 2013 15:11:55 +0000 (15:11 +0000)
problem was the keymap failed to import but didnt give any feedback, now it displays error message.

release/scripts/modules/bpy/utils.py
release/scripts/startup/bl_operators/wm.py

index b78105dedb5818d28cf0e406b6cde40295fa3969..ec3891e8d761df4bd8077f80518b573e27a0a495 100644 (file)
@@ -482,8 +482,9 @@ def preset_find(name, preset_path, display_name=False, ext=".py"):
                 return filepath
 
 
-def keyconfig_set(filepath):
+def keyconfig_set(filepath, report=None):
     from os.path import basename, splitext
+    from itertools import chain
 
     if _bpy.app.debug_python:
         print("loading preset:", filepath)
@@ -496,25 +497,36 @@ def keyconfig_set(filepath):
         keyfile = open(filepath)
         exec(compile(keyfile.read(), filepath, "exec"), {"__file__": filepath})
         keyfile.close()
+        error_msg = ""
     except:
         import traceback
-        traceback.print_exc()
-
-    kc_new = [kc for kc in keyconfigs if kc not in keyconfigs_old][0]
-
-    kc_new.name = ""
-
-    # remove duplicates
-    name = splitext(basename(filepath))[0]
-    while True:
-        kc_dupe = keyconfigs.get(name)
-        if kc_dupe:
-            keyconfigs.remove(kc_dupe)
-        else:
-            break
-
-    kc_new.name = name
-    keyconfigs.active = kc_new
+        error_msg = traceback.format_exc()
+
+    if error_msg:
+        if report is not None:
+            report({'ERROR'}, error_msg)
+        print(error_msg)
+
+    kc_new = next(chain(iter(kc for kc in keyconfigs if kc not in keyconfigs_old), (None,)))
+    if kc_new is None:
+        if report is not None:
+            report({'ERROR'}, "Failed to load keymap %r" % filepath)
+        return False
+    else:
+        kc_new.name = ""
+
+        # remove duplicates
+        name = splitext(basename(filepath))[0]
+        while True:
+            kc_dupe = keyconfigs.get(name)
+            if kc_dupe:
+                keyconfigs.remove(kc_dupe)
+            else:
+                break
+
+        kc_new.name = name
+        keyconfigs.active = kc_new
+        return True
 
 
 def user_resource(resource_type, path="", create=False):
index a2a8c99125946dfabceacf4209128e6cce59f3df..9cd9131ec4be454b0492db37fc88bab5e6d94d72 100644 (file)
@@ -1199,9 +1199,10 @@ class WM_OT_keyconfig_activate(Operator):
             )
 
     def execute(self, context):
-        bpy.utils.keyconfig_set(self.filepath)
-        return {'FINISHED'}
-
+        if bpy.utils.keyconfig_set(self.filepath, report=self.report):
+            return {'FINISHED'}
+        else:
+            return {'CANCELLED'}
 
 class WM_OT_appconfig_default(Operator):
     bl_idname = "wm.appconfig_default"
@@ -1386,9 +1387,10 @@ class WM_OT_keyconfig_import(Operator):
             return {'CANCELLED'}
 
         # sneaky way to check we're actually running the code.
-        bpy.utils.keyconfig_set(path)
-
-        return {'FINISHED'}
+        if bpy.utils.keyconfig_set(path, report=self.report):
+            return {'FINISHED'}
+        else:
+            return {'CANCELLED'}
 
     def invoke(self, context, event):
         wm = context.window_manager