diff options
author | Campbell Barton <ideasman42@gmail.com> | 2018-11-18 03:49:03 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2018-11-18 03:49:03 +0300 |
commit | 0f1a63d34c048b11f93b167449741540901feb01 (patch) | |
tree | ca8c4ec578b1d7c3b38fa28c1c8620b4fa449920 /release/scripts/modules | |
parent | f520f01b46b96467d1533a5614b39600e4de0b25 (diff) |
PyAPI: add bpy.utils.execfile
Wraps `importlib.util`, avoids duplicate preset loading calls.
Diffstat (limited to 'release/scripts/modules')
-rw-r--r-- | release/scripts/modules/bpy/utils/__init__.py | 22 | ||||
-rw-r--r-- | release/scripts/modules/bpy_extras/keyconfig_utils.py | 8 |
2 files changed, 16 insertions, 14 deletions
diff --git a/release/scripts/modules/bpy/utils/__init__.py b/release/scripts/modules/bpy/utils/__init__.py index ba25deddf55..9889b16829b 100644 --- a/release/scripts/modules/bpy/utils/__init__.py +++ b/release/scripts/modules/bpy/utils/__init__.py @@ -51,6 +51,7 @@ __all__ = ( "units", "unregister_class", "user_resource", + "execfile", ) from _bpy import ( @@ -75,6 +76,18 @@ _script_module_dirs = "startup", "modules" _is_factory_startup = _bpy.app.factory_startup +def execfile(filepath, mod=None): + # module name isn't used or added to 'sys.modules'. + # passing in 'mod' allows re-execution without having to reload. + + import importlib.util + mod_spec = importlib.util.spec_from_file_location("__main__", filepath) + if mod is None: + mod = importlib.util.module_from_spec(mod_spec) + mod_spec.loader.exec_module(mod) + return mod + + def _test_import(module_name, loaded_modules): use_time = _bpy.app.debug_python @@ -585,14 +598,7 @@ def keyconfig_set(filepath, report=None): try: error_msg = "" - with open(filepath, 'r', encoding='utf-8') as keyfile: - exec( - compile(keyfile.read(), filepath, "exec"), - { - "__file__": filepath, - "__name__": "__main__", - } - ) + execfile(filepath) except: import traceback error_msg = traceback.format_exc() diff --git a/release/scripts/modules/bpy_extras/keyconfig_utils.py b/release/scripts/modules/bpy_extras/keyconfig_utils.py index 4d5ec5286ec..eb727167d33 100644 --- a/release/scripts/modules/bpy_extras/keyconfig_utils.py +++ b/release/scripts/modules/bpy_extras/keyconfig_utils.py @@ -511,7 +511,7 @@ def keyconfig_import_from_data(name, keyconfig_data): def keyconfig_module_from_preset(name, preset_reference_filename=None): import os - import importlib.util + import bpy if preset_reference_filename is not None: preset_path = os.path.join(os.path.dirname(preset_reference_filename), name + ".py") else: @@ -521,11 +521,7 @@ def keyconfig_module_from_preset(name, preset_reference_filename=None): if not (preset_path and os.path.exists(preset_path)): preset_path = bpy.utils.preset_find(name, "keyconfig") - # module name isn't used or added to 'sys.modules'. - mod_spec = importlib.util.spec_from_file_location("__main__", preset_path) - mod = importlib.util.module_from_spec(mod_spec) - mod_spec.loader.exec_module(mod) - return mod + return bpy.utils.execfile(preset_path) # ----------------------------------------------------------------------------- |