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/bpy/utils/__init__.py | |
parent | f520f01b46b96467d1533a5614b39600e4de0b25 (diff) |
PyAPI: add bpy.utils.execfile
Wraps `importlib.util`, avoids duplicate preset loading calls.
Diffstat (limited to 'release/scripts/modules/bpy/utils/__init__.py')
-rw-r--r-- | release/scripts/modules/bpy/utils/__init__.py | 22 |
1 files changed, 14 insertions, 8 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() |