[Bf-blender-cvs] [a56da7b0451] master: Fix bpy.utils.script_paths() ignoring environment variables

Campbell Barton noreply at git.blender.org
Thu Sep 29 09:32:44 CEST 2022


Commit: a56da7b0451dc0076e48a48d3b073f9a103c09d5
Author: Campbell Barton
Date:   Thu Sep 29 17:12:10 2022 +1000
Branches: master
https://developer.blender.org/rBa56da7b0451dc0076e48a48d3b073f9a103c09d5

Fix bpy.utils.script_paths() ignoring environment variables

When check_all=True was passed,

- `os.path.join(bpy.utils.resource_path('USER'), "scripts")`
  was used instead of BLENDER_USER_SCRIPTS.
- `os.path.join(bpy.utils.resource_path('SYSTEM'), "scripts")`
  was used instead of BLENDER_SYSTEM_SCRIPTS.

Other minor changes:

- Simplify collecting paths.
- Don't add user-directories multiple times when check_all=True.
- Normalize paths before before checking duplicates to reduce the
  change the same path is added multiple times.

Found these issues while investigating T101389.

===================================================================

M	release/scripts/modules/bpy/utils/__init__.py

===================================================================

diff --git a/release/scripts/modules/bpy/utils/__init__.py b/release/scripts/modules/bpy/utils/__init__.py
index a1e34731a94..22577dbd1cf 100644
--- a/release/scripts/modules/bpy/utils/__init__.py
+++ b/release/scripts/modules/bpy/utils/__init__.py
@@ -358,56 +358,50 @@ def script_paths(*, subdir=None, user_pref=True, check_all=False, use_user=True)
     :return: script paths.
     :rtype: list
     """
-    scripts = []
 
-    # Only script paths Blender uses.
-    #
-    # This is needed even when `check_all` is enabled.
-    # NOTE: Use `_script_base_dir` instead of `_bpy_script_paths()[0]` as it's taken from this files path.
-    base_paths = (_script_base_dir, )
-    if use_user:
-        base_paths += _bpy_script_paths()[1:]
+    if check_all or use_user:
+        path_system, path_user = _bpy_script_paths()
 
-    # Defined to be (system, user) so we can skip the second if needed.
-    if not use_user:
-        base_paths = base_paths[:1]
+    base_paths = []
 
     if check_all:
-        # All possible paths, no duplicates, keep order.
+        # Order: 'LOCAL', 'USER', 'SYSTEM' (where user is optional).
+        if path_local := resource_path('LOCAL'):
+            base_paths.append(_os.path.join(path_local, "scripts"))
         if use_user:
-            test_paths = ('LOCAL', 'USER', 'SYSTEM')
-        else:
-            test_paths = ('LOCAL', 'SYSTEM')
-
-        base_paths = (
-            *(path for path in (
-                _os.path.join(resource_path(res), "scripts")
-                for res in test_paths) if path not in base_paths),
-            *base_paths,
-        )
-
-    test_paths = (
-        *base_paths,
-        *((script_path_user(),) if use_user else ()),
-        *((script_path_pref(),) if user_pref else ()),
-    )
+            base_paths.append(path_user)
+        base_paths.append(path_system)  # Same as: `system_resource('SCRIPTS')`.
+
+    # Note that `_script_base_dir` may be either:
+    # - `os.path.join(bpy.utils.resource_path('LOCAL'), "scripts")`
+    # - `bpy.utils.system_resource('SCRIPTS')`.
+    # When `check_all` is enabled duplicate paths will be added however
+    # paths are de-duplicated so it wont cause problems.
+    base_paths.append(_script_base_dir)
+
+    if not check_all:
+        if use_user:
+            base_paths.append(path_user)
+
+    if user_pref:
+        base_paths.append(script_path_pref())
 
-    for path in test_paths:
-        if path:
-            path = _os.path.normpath(path)
-            if path not in scripts and _os.path.isdir(path):
-                scripts.append(path)
+    scripts = []
+    for path in base_paths:
+        if not path:
+            continue
 
-    if subdir is None:
-        return scripts
+        path = _os.path.normpath(path)
+        if subdir is not None:
+            path = _os.path.join(path, subdir)
 
-    scripts_subdir = []
-    for path in scripts:
-        path_subdir = _os.path.join(path, subdir)
-        if _os.path.isdir(path_subdir):
-            scripts_subdir.append(path_subdir)
+        if path in scripts:
+            continue
+        if not _os.path.isdir(path):
+            continue
+        scripts.append(path)
 
-    return scripts_subdir
+    return scripts
 
 
 def refresh_script_paths():



More information about the Bf-blender-cvs mailing list