forked from bartvdbraak/blender
Keymap: load/save improvements
- avoid passing redundant operator name to keymap property set function. - avoid double attr lookups when setting each property. - handle exceptions on value type mismatch. - avoid resource warning on failed load.
This commit is contained in:
parent
7afbdbf812
commit
2a9e33fba5
@ -494,10 +494,10 @@ def keyconfig_set(filepath, report=None):
|
|||||||
keyconfigs_old = keyconfigs[:]
|
keyconfigs_old = keyconfigs[:]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
keyfile = open(filepath)
|
|
||||||
exec(compile(keyfile.read(), filepath, "exec"), {"__file__": filepath})
|
|
||||||
keyfile.close()
|
|
||||||
error_msg = ""
|
error_msg = ""
|
||||||
|
with open(filepath, 'r', encoding='utf-8') as keyfile:
|
||||||
|
exec(compile(keyfile.read(), filepath, "exec"),
|
||||||
|
{"__file__": filepath})
|
||||||
except:
|
except:
|
||||||
import traceback
|
import traceback
|
||||||
error_msg = traceback.format_exc()
|
error_msg = traceback.format_exc()
|
||||||
|
@ -175,7 +175,7 @@ def _export_properties(prefix, properties, kmi_id, lines=None):
|
|||||||
elif properties.is_property_set(pname):
|
elif properties.is_property_set(pname):
|
||||||
value = string_value(value)
|
value = string_value(value)
|
||||||
if value != "":
|
if value != "":
|
||||||
lines.append("set_kmi_prop(%s, '%s', %s, '%s')\n" % (prefix, pname, value, kmi_id))
|
lines.append("kmi_props_setattr(%s, '%s', %s)\n" % (prefix, pname, value))
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
|
|
||||||
@ -221,11 +221,14 @@ def keyconfig_export(wm, kc, filepath):
|
|||||||
|
|
||||||
f.write("import bpy\n")
|
f.write("import bpy\n")
|
||||||
f.write("import os\n\n")
|
f.write("import os\n\n")
|
||||||
f.write("def set_kmi_prop(kmiprops, prop, value, kmiid):\n"
|
f.write("def kmi_props_setattr(kmi_props, attr, value):\n"
|
||||||
" if hasattr(kmiprops, prop):\n"
|
" try:\n"
|
||||||
" setattr(kmiprops, prop, value)\n"
|
" setattr(kmi_props, attr, value)\n"
|
||||||
" else:\n"
|
" except AttributeError:\n"
|
||||||
" print(\"Warning: property '%s' not found in keymap item '%s'\" % (prop, kmiid))\n\n")
|
" print(\"Warning: property '%s' not found in keymap item '%s'\" %\n"
|
||||||
|
" (attr, kmi_props.__class__.__name__))\n"
|
||||||
|
" except Exception as e:\n"
|
||||||
|
" print(\"Warning: %r\" % e)\n\n")
|
||||||
f.write("wm = bpy.context.window_manager\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
|
f.write("kc = wm.keyconfigs.new(os.path.splitext(os.path.basename(__file__))[0])\n\n") # keymap must be created by caller
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user