From 1c953fca2e44291300fa29483c669823f28818d6 Mon Sep 17 00:00:00 2001
From: Brecht Van Lommel <brecht@noreply.localhost>
Date: Wed, 26 Feb 2025 15:19:23 +0100
Subject: [PATCH] Fix: Blender as Python Module shared library directory wrong

This could affect for example the USD and MaterialX Python modules
that are now bundled, and need appropriate paths to their libraries.

Ref #134676

Pull Request: https://projects.blender.org/blender/blender/pulls/134937
---
 scripts/site/sitecustomize.py | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/scripts/site/sitecustomize.py b/scripts/site/sitecustomize.py
index e199df651339..30aa6cd0ce85 100644
--- a/scripts/site/sitecustomize.py
+++ b/scripts/site/sitecustomize.py
@@ -7,15 +7,25 @@
 import sys
 import os
 
-exe_dir, exe_file = os.path.split(sys.executable)
-is_python = exe_file.startswith("python")
-
 # Path to Blender shared libraries.
 shared_lib_dirname = "blender.shared" if sys.platform == "win32" else "lib"
-if is_python:
-    shared_lib_dir = os.path.abspath(os.path.join(exe_dir, "..", "..", "..", shared_lib_dirname))
+
+if os.path.basename(__file__) == "bpy_site_customize.py":
+    # Blender as Python Module.
+    is_python = True
+    shared_lib_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
+    # On Windows no subdirectory is used.
+    if sys.platform != "win32":
+        shared_lib_dir = os.path.join(shared_lib_dir, shared_lib_dirname)
 else:
-    shared_lib_dir = os.path.abspath(os.path.join(exe_dir, shared_lib_dirname))
+    exe_dir, exe_file = os.path.split(sys.executable)
+    is_python = exe_file.startswith("python")
+    if is_python:
+        # Python executable bundled with Blender.
+        shared_lib_dir = os.path.abspath(os.path.join(exe_dir, "..", "..", "..", shared_lib_dirname))
+    else:
+        # Blender executable.
+        shared_lib_dir = os.path.abspath(os.path.join(exe_dir, shared_lib_dirname))
 
 if sys.platform == "win32":
     # Directory for extensions to find DLLs.
-- 
GitLab