|
34 | 34 | import traceback |
35 | 35 | import importlib |
36 | 36 |
|
| 37 | +print("---------------------------------------") |
| 38 | +print("Checking SOFA_ROOT and SOFAPYTHON3_ROOT") |
| 39 | + |
| 40 | +# check if SOFA_ROOT has been (well) set |
| 41 | +sofa_root = os.environ.get('SOFA_ROOT') |
| 42 | +if sofa_root: |
| 43 | + print("Using environment variable SOFA_ROOT: " + sofa_root) |
| 44 | +else: |
| 45 | + print("Warning: environment variable SOFA_ROOT is empty. Trying to guess it.") |
| 46 | + # try a guess from <sofa_root>/plugins/SofaPython3/lib/python3/site-packages/Sofa |
| 47 | + sofa_root_guess = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/../../../../../..') |
| 48 | + if os.path.isdir(os.path.abspath(sofa_root_guess + '/lib' )): |
| 49 | + print("Guessed SOFA_ROOT: " + sofa_root_guess) |
| 50 | + sofa_root = sofa_root_guess |
| 51 | + else: |
| 52 | + print("Warning: cannot guess SOFA_ROOT", |
| 53 | + "Loading SOFA libraries will likely fail and/or SOFA won't find its resources.") |
| 54 | + |
| 55 | +# check if SOFAPYTHON3_ROOT has been (well) set |
| 56 | +sofapython3_root = os.environ.get('SOFAPYTHON3_ROOT') |
| 57 | +if sofapython3_root: |
| 58 | + print("Using environment variable SOFAPYTHON3_ROOT: " + sofapython3_root) |
| 59 | +else: |
| 60 | + print("Warning: environment variable SOFAPYTHON3_ROOT is empty. Trying to guess it.") |
| 61 | + # try a guess from <sofapython3_root>/lib/python3/site-packages/Sofa |
| 62 | + sofapython3_root_guess = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/../../../..') |
| 63 | + if os.path.isdir(os.path.abspath(sofapython3_root_guess + '/lib' )): |
| 64 | + print("Guessed SOFAPYTHON3_ROOT: " + sofapython3_root_guess) |
| 65 | + sofapython3_root = sofapython3_root_guess |
| 66 | + else: |
| 67 | + print("Warning: cannot guess SOFAPYTHON3_ROOT", |
| 68 | + "Loading SofaPython3 modules will likely fail.") |
| 69 | + |
| 70 | +if sofa_root and sys.platform == 'win32': |
| 71 | + # Windows-only: starting from python 3.8, python wont read the env. variable PATH to get SOFA's dlls. |
| 72 | + # os.add_dll_directory() is the new way to add paths for python to get external libraries. |
| 73 | + sofa_bin_path = sofa_root + "\\bin" |
| 74 | + sofa_file_test = sofa_bin_path + "\\Sofa.Helper.dll" |
| 75 | + sofapython3_bin_path = sofapython3_root + "\\bin" |
| 76 | + sofapython3_file_test = sofapython3_bin_path + "\\SofaPython3.dll" |
| 77 | + |
| 78 | + if not os.path.isfile(sofa_file_test): |
| 79 | + print("Warning: environment variable SOFA_ROOT is set but seems invalid.", |
| 80 | + "Loading SOFA libraries will likely fail.") |
| 81 | + print("SOFA_ROOT is currently: " + sofa_root) |
| 82 | + if not os.path.isfile(sofapython3_file_test): |
| 83 | + print("Warning: cannot find SofaPython3.dll at path: " + sofapython3_bin_path) |
| 84 | + print("This path will NOT be added to the DLL search path.", |
| 85 | + "Loading SofaPython3 python modules will likely fail.") |
| 86 | + |
| 87 | + if sys.version_info.minor >= 8: |
| 88 | + # Starting from python3.8 we need to explicitly find SOFA libraries |
| 89 | + if os.path.isfile(sofa_file_test): |
| 90 | + os.add_dll_directory(sofa_bin_path) |
| 91 | + if os.path.isfile(sofapython3_file_test): |
| 92 | + os.add_dll_directory(sofapython3_bin_path) |
| 93 | + else: |
| 94 | + # Add temporarily the bin/lib path to the env variable PATH |
| 95 | + if os.path.isfile(sofa_file_test): |
| 96 | + os.environ['PATH'] = sofa_bin_path + os.pathsep + os.environ['PATH'] |
| 97 | + if os.path.isfile(sofapython3_file_test): |
| 98 | + os.environ['PATH'] = sofapython3_bin_path + os.pathsep + os.environ['PATH'] |
| 99 | + |
| 100 | +print("---------------------------------------") |
| 101 | +sys.stdout.flush() |
| 102 | + |
37 | 103 | import Sofa.constants |
38 | 104 | import Sofa.Helper |
39 | 105 | import Sofa.Core |
|
0 commit comments