Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ jobs:
-DCMAKE_C_COMPILER_LAUNCHER=ccache
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache
-DCMAKE_PREFIX_PATH=$SOFA_ROOT/lib/cmake
-DCMAKE_BUILD_TYPE=Release
.
&& ninja && ninja install
&& echo ${CCACHE_BASEDIR}
Expand Down Expand Up @@ -125,24 +126,22 @@ jobs:
- name: Binding.Sofa.Tests
if: ${{ always() }}
run: |
export DYLD_LIBRARY_PATH=$GITHUB_WORKSPACE/SofaPython3/lib:$SOFA_ROOT/lib:$DYLD_LIBRARY_PATH
export PYTHONPATH=$GITHUB_WORKSPACE/SofaPython3/lib/python3/site-packages:$PYTHONPATH
export SOFA_PLUGIN_PATH=$SOFA_ROOT/plugins
export DYLD_LIBRARY_PATH=$GITHUB_WORKSPACE/SofaPython3/lib:$SOFA_ROOT/lib
export PYTHONPATH=$GITHUB_WORKSPACE/SofaPython3/lib/python3/site-packages
chmod +x SofaPython3/bin/Bindings.Sofa.Tests
./SofaPython3/bin/Bindings.Sofa.Tests
- name: Bindings.SofaRuntime.Tests
if: ${{ always() }}
run: |
export DYLD_LIBRARY_PATH=$GITHUB_WORKSPACE/SofaPython3/lib:$SOFA_ROOT/lib:$DYLD_LIBRARY_PATH
export PYTHONPATH=$GITHUB_WORKSPACE/SofaPython3/lib/python3/site-packages:$PYTHONPATH
export SOFA_PLUGIN_PATH=$SOFA_ROOT/plugins
export DYLD_LIBRARY_PATH=$GITHUB_WORKSPACE/SofaPython3/lib:$SOFA_ROOT/lib
export PYTHONPATH=$GITHUB_WORKSPACE/SofaPython3/lib/python3/site-packages
chmod +x SofaPython3/bin/Bindings.SofaRuntime.Tests
./SofaPython3/bin/Bindings.SofaRuntime.Tests
- name: Bindings.SofaTypes.Tests
if: ${{ always() }}
run: |
export DYLD_LIBRARY_PATH=$GITHUB_WORKSPACE/SofaPython3/lib:$SOFA_ROOT/lib:$DYLD_LIBRARY_PATH
export PYTHONPATH=$GITHUB_WORKSPACE/SofaPython3/lib/python3/site-packages:$PYTHONPATH
export DYLD_LIBRARY_PATH=$GITHUB_WORKSPACE/SofaPython3/lib:$SOFA_ROOT/lib
export PYTHONPATH=$GITHUB_WORKSPACE/SofaPython3/lib/python3/site-packages
chmod +x SofaPython3/bin/Bindings.SofaTypes.Tests
./SofaPython3/bin/Bindings.SofaTypes.Tests

4 changes: 1 addition & 3 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ jobs:
-DCMAKE_C_COMPILER_LAUNCHER=ccache
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache
-DCMAKE_PREFIX_PATH=$SOFA_ROOT/lib/cmake
-DCMAKE_BUILD_TYPE=Release
.
&& ninja && ninja install
&& echo ${CCACHE_BASEDIR}
Expand Down Expand Up @@ -143,23 +144,20 @@ jobs:
run: |
export LD_LIBRARY_PATH=$GITHUB_WORKSPACE/SofaPython3/lib:$SOFA_ROOT/lib:$LD_LIBRARY_PATH
export PYTHONPATH=$GITHUB_WORKSPACE/SofaPython3/lib/python3/site-packages:$PYTHONPATH
export SOFA_PLUGIN_PATH=$SOFA_ROOT/plugins
chmod +x SofaPython3/bin/Bindings.Sofa.Tests
./SofaPython3/bin/Bindings.Sofa.Tests
- name: Bindings.SofaRuntime.Tests
if: ${{ always() }}
run: |
export LD_LIBRARY_PATH=$GITHUB_WORKSPACE/SofaPython3/lib:$SOFA_ROOT/lib:$LD_LIBRARY_PATH
export PYTHONPATH=$GITHUB_WORKSPACE/SofaPython3/lib/python3/site-packages:$PYTHONPATH
export SOFA_PLUGIN_PATH=$SOFA_ROOT/plugins
chmod +x SofaPython3/bin/Bindings.SofaRuntime.Tests
./SofaPython3/bin/Bindings.SofaRuntime.Tests
- name: Bindings.SofaTypes.Tests
if: ${{ always() }}
run: |
export LD_LIBRARY_PATH=$GITHUB_WORKSPACE/SofaPython3/lib:$SOFA_ROOT/lib:$LD_LIBRARY_PATH
export PYTHONPATH=$GITHUB_WORKSPACE/SofaPython3/lib/python3/site-packages:$PYTHONPATH
export SOFA_PLUGIN_PATH=$SOFA_ROOT/plugins
chmod +x SofaPython3/bin/Bindings.SofaTypes.Tests
./SofaPython3/bin/Bindings.SofaTypes.Tests

3 changes: 2 additions & 1 deletion CMake/SofaPython3Tools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,13 @@ function(SP3_add_python_module)
PUBLIC $<INSTALL_INTERFACE:include>
)

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") # Clang or AppleCLang
target_compile_options(${A_TARGET} PUBLIC -fsized-deallocation)
endif()

if (NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
target_compile_options(${A_TARGET} PRIVATE -Dregister=)
target_compile_options(${A_TARGET} PRIVATE -fvisibility=hidden)
endif()

target_link_libraries(${A_TARGET} PUBLIC pybind11::module)
Expand Down
17 changes: 15 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ if(MSVC)
# With C++17 (/std:c++17), to get MSVC to behave, you need /permissive-
# see https://github.com/pybind/pybind11/issues/1616
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /permissive-")

# use M_PI in cmath.h
add_definitions("-D_USE_MATH_DEFINES")
endif()

set(CMAKE_CXX_STANDARD 17)
Expand Down Expand Up @@ -172,13 +175,23 @@ if (SP3_LINK_TO_USER_SITE AND SP3_PYTHON_PACKAGES_LINK_DIRECTORY)
install(DIRECTORY DESTINATION ${SP3_PYTHON_PACKAGES_LINK_DIRECTORY})
foreach(directory ${directories})
if(IS_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${directory})
install(CODE "\
if(WIN32)
install(CODE "\
execute_process( \
COMMAND ${CMAKE_COMMAND} -E create_symlink \
COMMAND ${CMAKE_COMMAND} -E copy_directory \
${CMAKE_INSTALL_PREFIX}/${LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${directory}/ \
${SP3_PYTHON_PACKAGES_LINK_DIRECTORY}/${directory} \
)"
)
else()
install(CODE "\
execute_process( \
COMMAND ${CMAKE_COMMAND} -E create_symlink \
${CMAKE_INSTALL_PREFIX}/${LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${directory}/ \
${SP3_PYTHON_PACKAGES_LINK_DIRECTORY}/${directory} \
)"
)
endif()
endif()
endforeach()
endif()
1 change: 0 additions & 1 deletion Plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ endif(SP3_BUILD_TEST)
find_package(pybind11 CONFIG REQUIRED)
find_package(SofaFramework REQUIRED)
find_package(SofaSimulation REQUIRED)
find_package(SofaGeneral REQUIRED)

add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES})
add_library(SofaPython3::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
Expand Down
15 changes: 0 additions & 15 deletions Plugin/src/SofaPython3/DataHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,6 @@ void fillBaseObjectdescription(sofa::core::objectmodel::BaseObjectDescription& d
}
}

PythonTrampoline::~PythonTrampoline(){}
void PythonTrampoline::setInstance(py::object s)
{
s.inc_ref();

// TODO(bruno-marques) ici ça crash dans SOFA.
//--ref_counter;

pyobject = std::shared_ptr<PyObject>( s.ptr(), [](PyObject* ob)
{
// runSofa Sofa/tests/pyfiles/ScriptController.py => CRASH
// Py_DECREF(ob);
});
}

std::ostream& operator<<(std::ostream& out, const py::buffer_info& p)
{
out << "buffer{"<< p.format << ", " << p.ndim << ", " << p.shape[0];
Expand Down
63 changes: 20 additions & 43 deletions Plugin/src/SofaPython3/DataHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ along with sofaqtquick. If not, see <http://www.gnu.org/licenses/>.
#include <sofa/core/objectmodel/Base.h>
#include <sofa/core/objectmodel/BaseObject.h>
#include <sofa/core/objectmodel/BaseNode.h>

#include "config.h"
#include <SofaPython3/config.h>

////////////////////////// FORWARD DECLARATION ///////////////////////////
namespace sofa {
Expand Down Expand Up @@ -183,7 +182,6 @@ namespace sofa {
/////////////////////////////// DECLARATION //////////////////////////////
namespace sofapython3
{
namespace py { using namespace pybind11; }

using sofa::core::objectmodel::Base;
using sofa::core::objectmodel::BaseData;
Expand All @@ -192,61 +190,40 @@ using sofa::core::objectmodel::BaseNode;
using sofa::core::objectmodel::BaseObject;
using sofa::defaulttype::AbstractTypeInfo;

SOFAPYTHON3_API void setItem2D(pybind11::array a, pybind11::slice slice, pybind11::object o);
SOFAPYTHON3_API void setItem2D(pybind11::array a, const pybind11::slice& slice,
const pybind11::slice& slice1, pybind11::object o);
SOFAPYTHON3_API void setItem1D(pybind11::array a, pybind11::slice slice, pybind11::object o);
SOFAPYTHON3_API void setItem(pybind11::array a, pybind11::slice slice, pybind11::object value);

class SOFAPYTHON3_API PythonTrampoline
{
protected:
std::shared_ptr<PyObject> pyobject;
public:
virtual ~PythonTrampoline();
virtual void setInstance(py::object s);
};

template <typename T> class py_shared_ptr : public sofa::core::sptr<T>
{
public:
py_shared_ptr(T *ptr) : sofa::core::sptr<T>(ptr)
{
auto nptr = dynamic_cast<PythonTrampoline*>(ptr);
if(nptr)
nptr->setInstance( py::cast(ptr) ) ;
}
};

SOFAPYTHON3_API void setItem2D(py::array a, py::slice slice, py::object o);
SOFAPYTHON3_API void setItem2D(py::array a, const py::slice& slice,
const py::slice& slice1, py::object o);
SOFAPYTHON3_API void setItem1D(py::array a, py::slice slice, py::object o);
SOFAPYTHON3_API void setItem(py::array a, py::slice slice, py::object value);

SOFAPYTHON3_API py::slice toSlice(const py::object& o);
SOFAPYTHON3_API pybind11::slice toSlice(const pybind11::object& o);
SOFAPYTHON3_API std::string getPathTo(Base* b);
SOFAPYTHON3_API const char* getFormat(const AbstractTypeInfo& nfo);

SOFAPYTHON3_API std::map<void*, py::array>& getObjectCache();
SOFAPYTHON3_API std::map<void*, pybind11::array>& getObjectCache();
SOFAPYTHON3_API void trimCache();

SOFAPYTHON3_API bool hasArrayFor(BaseData* d);
SOFAPYTHON3_API py::array resetArrayFor(BaseData* d);
SOFAPYTHON3_API py::array getPythonArrayFor(BaseData* d);
SOFAPYTHON3_API pybind11::array resetArrayFor(BaseData* d);
SOFAPYTHON3_API pybind11::array getPythonArrayFor(BaseData* d);



py::buffer_info SOFAPYTHON3_API toBufferInfo(BaseData& m);
py::object SOFAPYTHON3_API convertToPython(BaseData* d);
pybind11::buffer_info SOFAPYTHON3_API toBufferInfo(BaseData& m);
pybind11::object SOFAPYTHON3_API convertToPython(BaseData* d);

void SOFAPYTHON3_API copyFromListScalar(BaseData& d, const AbstractTypeInfo& nfo, const py::list& l);
void SOFAPYTHON3_API copyFromListScalar(BaseData& d, const AbstractTypeInfo& nfo, const pybind11::list& l);

std::string SOFAPYTHON3_API toSofaParsableString(const py::handle& p);
std::string SOFAPYTHON3_API toSofaParsableString(const pybind11::handle& p);

//py::object SOFAPYTHON3_API dataToPython(BaseData* d);
//pybind11::object SOFAPYTHON3_API dataToPython(BaseData* d);

/// RVO optimized function. Don't care about copy on the return code.
void SOFAPYTHON3_API fillBaseObjectdescription(sofa::core::objectmodel::BaseObjectDescription& desc,
const py::dict& dict);
const pybind11::dict& dict);

template<typename T>
void copyScalar(BaseData* a, const AbstractTypeInfo& nfo, py::array_t<T, py::array::c_style> src)
void copyScalar(BaseData* a, const AbstractTypeInfo& nfo, pybind11::array_t<T, pybind11::array::c_style> src)
{
void* ptr = a->beginEditVoidPtr();

Expand All @@ -271,7 +248,7 @@ size_t SOFAPYTHON3_API getNDim(BaseData* self);
/// https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.size.html#numpy.ndarray.size
size_t SOFAPYTHON3_API getSize(BaseData* self);

SOFAPYTHON3_API std::ostream& operator<<(std::ostream& out, const py::buffer_info& p);
SOFAPYTHON3_API std::ostream& operator<<(std::ostream& out, const pybind11::buffer_info& p);

// TODO: move this somewhere else as we will probably need it in several other places.
template <class T> class raw_ptr
Expand Down Expand Up @@ -317,8 +294,8 @@ class scoped_writeonly_access
~scoped_writeonly_access(){ data->endEditVoidPtr(); }
};

SOFAPYTHON3_API BaseData* addData(py::object py_self, const std::string& name, py::object value = py::none(), py::object defaultValue = py::none(), const std::string& help = "", const std::string& group = "Property", std::string type = "");
SOFAPYTHON3_API BaseLink* addLink(py::object py_self, const std::string& name, py::object value, const std::string& help);
SOFAPYTHON3_API BaseData* addData(pybind11::object py_self, const std::string& name, pybind11::object value = pybind11::none(), pybind11::object defaultValue = pybind11::none(), const std::string& help = "", const std::string& group = "Property", std::string type = "");
SOFAPYTHON3_API BaseLink* addLink(pybind11::object py_self, const std::string& name, pybind11::object value, const std::string& help);
SOFAPYTHON3_API bool isProtectedKeyword(const std::string& name);

} // namespace sofapython3
Expand Down
6 changes: 4 additions & 2 deletions Plugin/src/SofaPython3/Prefab.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "Prefab.h"

#include <SofaPython3/Prefab.h>
#include <SofaPython3/DataHelper.h>
#include <SofaPython3/PythonFactory.h>
#include <SofaPython3/PythonEnvironment.h>
Expand All @@ -15,6 +14,9 @@ using sofa::simulation::VisualInitVisitor;
#include <sofa/simulation/Simulation.h>
using sofa::simulation::Simulation;

/// Makes an alias for the pybind11 namespace to increase readability.
namespace py { using namespace pybind11; }

namespace sofapython3
{
using sofa::core::objectmodel::Event;
Expand Down
2 changes: 1 addition & 1 deletion Plugin/src/SofaPython3/Prefab.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SOFAPYTHON3_API Prefab : public BasePrefab
void reinit();
virtual void doReInit() ;

void addPrefabParameter(const std::string& name, const std::string& help, const std::string& type, pybind11::object defaultValue = py::none());
void addPrefabParameter(const std::string& name, const std::string& help, const std::string& type, pybind11::object defaultValue = pybind11::none());
void setSourceTracking(const std::string& filename);
void breakPrefab();

Expand Down
4 changes: 3 additions & 1 deletion Plugin/src/SofaPython3/PythonEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ using sofa::simulation::SceneLoaderFactory;

#include <pybind11/embed.h>
#include <pybind11/eval.h>
namespace py = pybind11;

/// Makes an alias for the pybind11 namespace to increase readability.
namespace py { using namespace pybind11; }

#include "SceneLoaderPY3.h"
using sofapython3::SceneLoaderPY3;
Expand Down
5 changes: 2 additions & 3 deletions Plugin/src/SofaPython3/PythonEnvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ along with sofaqtquick. If not, see <http://www.gnu.org/licenses/>.

namespace sofapython3
{
namespace py { using namespace pybind11; }
using sofa::simulation::SceneLoader ;

/// Forward definition
Expand All @@ -82,9 +81,9 @@ class SOFAPYTHON3_API PythonEnvironment
static void Init();
static void Release();

static py::module importFromFile(const std::string& module,
static pybind11::module importFromFile(const std::string& module,
const std::string& path,
py::object* globals = nullptr);
pybind11::object* globals = nullptr);

/// Add a path to sys.path, the list of search path for Python modules.
static void addPythonModulePath(const std::string& path);
Expand Down
9 changes: 7 additions & 2 deletions Plugin/src/SofaPython3/PythonFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ along with sofaqtquick. If not, see <http://www.gnu.org/licenses/>.
********************************************************************/

#include <functional>
#include "PythonFactory.h"
#include "DataHelper.h"
#include <SofaPython3/PythonFactory.h>
#include <SofaPython3/DataHelper.h>

#include <SofaSimulationGraph/DAGNode.h>

Expand Down Expand Up @@ -60,6 +60,11 @@ using sofa::core::objectmodel::Event;

#include <sofa/defaulttype/DataTypeInfo.h>

/// Bind the python's attribute error
namespace pybind11 { PYBIND11_RUNTIME_EXCEPTION(attribute_error, PyExc_AttributeError) }
/// Makes an alias for the pybind11 namespace to increase readability.
namespace py { using namespace pybind11; }

namespace sofapython3
{
using namespace pybind11::literals;
Expand Down
21 changes: 9 additions & 12 deletions Plugin/src/SofaPython3/PythonFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,22 @@ along with sofaqtquick. If not, see <http://www.gnu.org/licenses/>.
/////////////////////////////// DECLARATION //////////////////////////////
namespace sofapython3
{
/// Makes an alias for the pybind11 namespace to increase readability.
namespace py { using namespace pybind11; }

typedef std::function<py::object(sofa::core::objectmodel::Base*)> componentDowncastingFunction;
typedef std::function<py::object(sofa::core::objectmodel::BaseData*)> dataDowncastingFunction;
typedef std::function<py::dict(sofa::core::objectmodel::Event*)> eventDowncastingFunction;
typedef std::function<pybind11::object(sofa::core::objectmodel::Base*)> componentDowncastingFunction;
typedef std::function<pybind11::object(sofa::core::objectmodel::BaseData*)> dataDowncastingFunction;
typedef std::function<pybind11::dict(sofa::core::objectmodel::Event*)> eventDowncastingFunction;
typedef std::function<sofa::core::objectmodel::BaseData*()> dataCreatorFunction;

typedef sofa::helper::Factory< std::string, sofa::core::objectmodel::BaseData> DataFactory;

class SOFAPYTHON3_API PythonFactory
{
public:
static py::object toPython(sofa::core::objectmodel::Base* object);
static py::object toPython(const sofa::core::objectmodel::BaseData* data);
static py::object toPython(sofa::core::objectmodel::BaseData* data);
static py::object valueToPython_ro(sofa::core::objectmodel::BaseData* data);
static void fromPython(sofa::core::objectmodel::BaseData* data, const py::object& value);
static py::object toPython(sofa::core::objectmodel::Event* event);
static pybind11::object toPython(sofa::core::objectmodel::Base* object);
static pybind11::object toPython(const sofa::core::objectmodel::BaseData* data);
static pybind11::object toPython(sofa::core::objectmodel::BaseData* data);
static pybind11::object valueToPython_ro(sofa::core::objectmodel::BaseData* data);
static void fromPython(sofa::core::objectmodel::BaseData* data, const pybind11::object& value);
static pybind11::object toPython(sofa::core::objectmodel::Event* event);
static sofa::core::objectmodel::BaseData* createInstance(const std::string& typeName);

static void registerType(const std::string& typeName, componentDowncastingFunction fct);
Expand Down
2 changes: 1 addition & 1 deletion Plugin/src/SofaPython3/PythonTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ along with sofaqtquick. If not, see <http://www.gnu.org/licenses/>.
#pragma once

#include <string>
#include "config.h"
#include <SofaPython3/config.h>
#include <sofa/helper/testing/BaseTest.h>

#include <boost/filesystem/path.hpp>
Expand Down
Loading