This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1528 from pypeclub/bugfix/1050-blender-cannot-ini…
…tialize-avalon-if-blender_user_scripts-is-already-used
- Loading branch information
Showing
3 changed files
with
149 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import os | ||
import traceback | ||
import importlib | ||
|
||
import bpy | ||
import addon_utils | ||
|
||
|
||
def load_scripts(paths): | ||
"""Copy of `load_scripts` from Blender's implementation. | ||
It is possible that whis function will be changed in future and usage will | ||
be based on Blender version. | ||
""" | ||
import bpy_types | ||
|
||
loaded_modules = set() | ||
|
||
previous_classes = [ | ||
cls | ||
for cls in bpy.types.bpy_struct.__subclasses__() | ||
] | ||
|
||
def register_module_call(mod): | ||
register = getattr(mod, "register", None) | ||
if register: | ||
try: | ||
register() | ||
except: | ||
traceback.print_exc() | ||
else: | ||
print("\nWarning! '%s' has no register function, " | ||
"this is now a requirement for registerable scripts" % | ||
mod.__file__) | ||
|
||
def unregister_module_call(mod): | ||
unregister = getattr(mod, "unregister", None) | ||
if unregister: | ||
try: | ||
unregister() | ||
except: | ||
traceback.print_exc() | ||
|
||
def test_reload(mod): | ||
# reloading this causes internal errors | ||
# because the classes from this module are stored internally | ||
# possibly to refresh internal references too but for now, best not to. | ||
if mod == bpy_types: | ||
return mod | ||
|
||
try: | ||
return importlib.reload(mod) | ||
except: | ||
traceback.print_exc() | ||
|
||
def test_register(mod): | ||
if mod: | ||
register_module_call(mod) | ||
bpy.utils._global_loaded_modules.append(mod.__name__) | ||
|
||
from bpy_restrict_state import RestrictBlend | ||
|
||
with RestrictBlend(): | ||
for base_path in paths: | ||
for path_subdir in bpy.utils._script_module_dirs: | ||
path = os.path.join(base_path, path_subdir) | ||
if not os.path.isdir(path): | ||
continue | ||
|
||
bpy.utils._sys_path_ensure_prepend(path) | ||
|
||
# Only add to 'sys.modules' unless this is 'startup'. | ||
if path_subdir != "startup": | ||
continue | ||
for mod in bpy.utils.modules_from_path(path, loaded_modules): | ||
test_register(mod) | ||
|
||
addons_paths = [] | ||
for base_path in paths: | ||
addons_path = os.path.join(base_path, "addons") | ||
if not os.path.exists(addons_path): | ||
continue | ||
addons_paths.append(addons_path) | ||
addons_module_path = os.path.join(addons_path, "modules") | ||
if os.path.exists(addons_module_path): | ||
bpy.utils._sys_path_ensure_prepend(addons_module_path) | ||
|
||
if addons_paths: | ||
# Fake addons | ||
origin_paths = addon_utils.paths | ||
|
||
def new_paths(): | ||
paths = origin_paths() + addons_paths | ||
return paths | ||
|
||
addon_utils.paths = new_paths | ||
addon_utils.modules_refresh() | ||
|
||
# load template (if set) | ||
if any(bpy.utils.app_template_paths()): | ||
import bl_app_template_utils | ||
bl_app_template_utils.reset(reload_scripts=False) | ||
del bl_app_template_utils | ||
|
||
for cls in bpy.types.bpy_struct.__subclasses__(): | ||
if cls in previous_classes: | ||
continue | ||
if not getattr(cls, "is_registered", False): | ||
continue | ||
for subcls in cls.__subclasses__(): | ||
if not subcls.is_registered: | ||
print( | ||
"Warning, unregistered class: %s(%s)" % | ||
(subcls.__name__, cls.__name__) | ||
) | ||
|
||
|
||
def append_user_scripts(): | ||
user_scripts = os.environ.get("OPENPYPE_BLENDER_USER_SCRIPTS") | ||
if not user_scripts: | ||
return | ||
|
||
try: | ||
load_scripts(user_scripts.split(os.pathsep)) | ||
except Exception: | ||
print("Couldn't load user scripts \"{}\"".format(user_scripts)) | ||
traceback.print_exc() |