Skip to content

Commit 858be20

Browse files
committed
Merge remote-tracking branch 'upstream/master' into pr-add-baselink-attribute-forwarding
2 parents d37460b + cfd8709 commit 858be20

File tree

16 files changed

+227
-8
lines changed

16 files changed

+227
-8
lines changed

.github/workflows/macos.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
fail-fast: false
1414
matrix:
1515
os: [macos-10.15]
16-
sofa_branch: [master, v21.06]
16+
sofa_branch: [master]
1717
env:
1818
SOFA_ROOT: /opt/sofa
1919
SOFA_OS: MacOS

.github/workflows/ubuntu.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
fail-fast: false
1414
matrix:
1515
os: [ubuntu-20.04]
16-
sofa_branch: [master, v21.06]
16+
sofa_branch: [master]
1717
env:
1818
SOFA_ROOT: /opt/sofa
1919
SOFA_OS: Linux

Plugin/src/SofaPython3/PythonEnvironment.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,15 @@ void PythonEnvironment::Init()
233233
// Lastly, we (try to) add modules from the root of SOFA
234234
addPythonModulePathsFromDirectory( Utils::getSofaPathPrefix() );
235235

236-
py::module::import("SofaRuntime");
236+
try
237+
{
238+
py::module::import("SofaRuntime");
239+
}
240+
catch (pybind11::error_already_set)
241+
{
242+
msg_error("SofaPython3") << "Could not import SofaRuntime module, initializing python3 for SOFA is not possible";
243+
return;
244+
}
237245
getStaticData()->m_sofamodule = py::module::import("Sofa");
238246

239247

@@ -255,6 +263,8 @@ void PythonEnvironment::Init()
255263
pluginLibraryPath = elem.first;
256264
}
257265
}
266+
267+
s_isInitialized = true;
258268
}
259269

260270
// Single implementation for the three different versions

Plugin/src/SofaPython3/PythonEnvironment.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class SOFAPYTHON3_API PythonEnvironment
5959
static void Init();
6060
static void Release();
6161

62+
static bool isInitialized()
63+
{
64+
return s_isInitialized;
65+
};
66+
6267
static pybind11::module importFromFile(const std::string& module,
6368
const std::string& path,
6469
pybind11::object* globals = nullptr);
@@ -150,6 +155,7 @@ class SOFAPYTHON3_API PythonEnvironment
150155
private:
151156
static PythonEnvironmentData* getStaticData() ;
152157
static std::string pluginLibraryPath;
158+
static inline bool s_isInitialized{false};
153159
};
154160

155161
} // namespace sofapython3

Plugin/src/SofaPython3/initModule.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ SOFAPYTHON3_API const char* getModuleVersion();
3737
SOFAPYTHON3_API const char* getModuleLicense();
3838
SOFAPYTHON3_API const char* getModuleDescription();
3939
SOFAPYTHON3_API const char* getModuleComponentList();
40+
SOFAPYTHON3_API bool moduleIsInitialized();
4041

4142
void initExternalModule()
4243
{
@@ -80,4 +81,9 @@ const char* getModuleComponentList()
8081
return "";
8182
}
8283

84+
bool moduleIsInitialized()
85+
{
86+
return PythonEnvironment::isInitialized();
87+
}
88+
8389
}

bindings/Sofa/package/__init__.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,72 @@
3434
import traceback
3535
import importlib
3636

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+
37103
import Sofa.constants
38104
import Sofa.Helper
39105
import Sofa.Core

bindings/Sofa/tests/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ set(PYTHON_FILES
1818
${CMAKE_CURRENT_SOURCE_DIR}/Core/ForceField.py
1919
${CMAKE_CURRENT_SOURCE_DIR}/Core/DataEngine.py
2020
${CMAKE_CURRENT_SOURCE_DIR}/Core/MyRestShapeForceField.py
21-
${CMAKE_CURRENT_SOURCE_DIR}/Core/PythonRestShapeForceField.py
2221
${CMAKE_CURRENT_SOURCE_DIR}/Core/BaseLink.py
2322
${CMAKE_CURRENT_SOURCE_DIR}/Helper/Message.py
2423
${CMAKE_CURRENT_SOURCE_DIR}/Types/RGBAColor.py

bindings/Sofa/tests/Core/Base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_addNewDataFromParent_brokenLink(self):
116116

117117
def test_getClassName(self):
118118
root = create_scene("root")
119-
self.assertEqual(root.getClassName(), "DAGNode")
119+
self.assertEqual(root.getClassName(), "Node")
120120

121121
def test_getTemplateName(self):
122122
root = create_scene("root")

bindings/SofaExporter/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ project(Bindings.SofaExporter)
33
set(HEADER_FILES
44
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaExporter/Binding_STLExporter.h
55
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaExporter/Binding_STLExporter_doc.h
6+
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaExporter/Binding_OBJExporter.h
7+
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaExporter/Binding_OBJExporter_doc.h
68
)
79

810
set(SOURCE_FILES
911
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaExporter/Binding_STLExporter.cpp
12+
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaExporter/Binding_OBJExporter.cpp
1013
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaExporter/Module_SofaExporter.cpp
1114
)
1215

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/******************************************************************************
2+
* SofaPython3 plugin *
3+
* (c) 2021 CNRS, University of Lille, INRIA *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify it *
6+
* under the terms of the GNU Lesser General Public License as published by *
7+
* the Free Software Foundation; either version 2.1 of the License, or (at *
8+
* your option) any later version. *
9+
* *
10+
* This program is distributed in the hope that it will be useful, but WITHOUT *
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
13+
* for more details. *
14+
* *
15+
* You should have received a copy of the GNU Lesser General Public License *
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17+
*******************************************************************************
18+
* Contact information: [email protected] *
19+
******************************************************************************/
20+
21+
#include <SofaPython3/Sofa/Core/Binding_Base.h>
22+
#include <SofaExporter/Binding_OBJExporter.h>
23+
#include <SofaExporter/Binding_OBJExporter_doc.h>
24+
25+
#include <SofaPython3/PythonFactory.h>
26+
#include <SofaPython3/Sofa/Core/Binding_BaseObject.h>
27+
#include <SofaExporter/OBJExporter.h>
28+
29+
using sofa::component::exporter::OBJExporter;
30+
31+
namespace py { using namespace pybind11; }
32+
33+
namespace sofapython3 {
34+
35+
void moduleAddOBJExporter(py::module &m)
36+
{
37+
PythonFactory::registerType<OBJExporter>([](sofa::core::objectmodel::Base* object)
38+
{
39+
return py::cast(dynamic_cast<OBJExporter*>(object));
40+
});
41+
42+
py::class_<OBJExporter, sofa::core::objectmodel::BaseObject, py_shared_ptr<OBJExporter>> p(m, "OBJExporter");
43+
44+
p.def("write", &OBJExporter::write, sofapython3::doc::SofaExporter::OBJExporter::write::docstring);
45+
}
46+
47+
} // namespace sofapython3

0 commit comments

Comments
 (0)