Skip to content

Commit c22962c

Browse files
committed
python context: set the current cwd to the scene's one
1 parent e6fb484 commit c22962c

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

bindings/Sofa/package/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,25 @@ def _select_bin_with_dll(candidates, dll_names):
160160
if sofapython3_file_test and sofapython3_bin_path:
161161
os.environ['PATH'] = sofapython3_bin_path + os.pathsep + os.environ.get('PATH', '')
162162

163+
# Set the Current Working Directory of the process with the path of the scene file.
164+
# (to be consistent with the original runSofa)
165+
def get_entry_script():
166+
# Try the main module's __file__ first (works for -m and normal scripts)
167+
import __main__
168+
if hasattr(__main__, '__file__'):
169+
return os.path.abspath(__main__.__file__)
170+
171+
# Fallback to sys.argv[0] if available
172+
if sys.argv[0]:
173+
return os.path.abspath(sys.argv[0])
174+
175+
# Interactive shell or unknown context
176+
return None
177+
178+
entry_point = get_entry_script()
179+
if entry_point:
180+
os.chdir(os.path.abspath(os.path.dirname(entry_point)))
181+
163182
print("---------------------------------------")
164183
if sys.stdout is not None:
165184
sys.stdout.flush()

0 commit comments

Comments
 (0)