Skip to content
Closed
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
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()
10 changes: 4 additions & 6 deletions Plugin/src/SofaPython3/DataHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,10 @@ class SOFAPYTHON3_API PythonTrampoline
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) ) ;
}
using Base = typename sofa::core::sptr<T>;
using Base::Base;
py_shared_ptr(const py_shared_ptr& other, T* ptr) : sofa::core::sptr<T>(ptr) {}
py_shared_ptr(T* ptr) : sofa::core::sptr<T>(ptr) {}
};

SOFAPYTHON3_API void setItem2D(py::array a, py::slice slice, py::object o);
Expand Down
77 changes: 21 additions & 56 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ along with sofaqtquick. If not, see <http://www.gnu.org/licenses/>.
********************************************************************/

#include <pybind11/pybind11.h>
#include <pybind11/cast.h>

#include "Binding_Controller.h"
#include "Binding_Controller_doc.h"
Expand All @@ -42,39 +43,6 @@ namespace sofapython3
{
using sofa::core::objectmodel::Event;

void Controller::init() {
}

void Controller::reinit() {
}

Controller::Controller() {
}

Controller::~Controller() {
}

class Controller_Trampoline : public Controller, public PythonTrampoline
{
public:
Controller_Trampoline() = default;

~Controller_Trampoline() override = default;

std::string getClassName() const override
{
return pyobject->ob_type->tp_name;
}

void init() override ;
void reinit() override ;
void handleEvent(Event* event) override ;

private:
void callScriptMethod(const py::object& self, Event* event,
const std::string & methodName);
};

void Controller_Trampoline::init()
{
PythonEnvironment::gil acquire {"Controller_init"};
Expand Down Expand Up @@ -120,39 +88,36 @@ namespace sofapython3
callScriptMethod(self, event, "onEvent");
}



void moduleAddController(py::module &m) {
py::class_<Controller,
Controller_Trampoline,
BaseObject,
py_shared_ptr<Controller>> f(m, "Controller",
py::dynamic_attr(),
py::multiple_inheritance(),
sofapython3::doc::controller::Controller);

f.def(py::init([](py::args& /*args*/, py::kwargs& kwargs)
{
auto c = new Controller_Trampoline();
c->f_listening.setValue(true);

for(auto kv : kwargs)
{
std::string key = py::cast<std::string>(kv.first);
py::object value = py::reinterpret_borrow<py::object>(kv.second);

if( key == "name")
c->setName(py::cast<std::string>(kv.second));
try {
BindingBase::SetAttr(*c, key, value);
} catch (py::attribute_error& /*e*/) {
/// kwargs are used to set datafields to their initial values,
/// but they can also be used as simple python attributes, unrelated to SOFA.
/// thus we catch & ignore the py::attribute_error thrown by SetAttr
}
}
return c;
}));
auto c = new Controller_Trampoline();
c->f_listening.setValue(true);

for(auto kv : kwargs)
{
std::string key = py::cast<std::string>(kv.first);
py::object value = py::reinterpret_borrow<py::object>(kv.second);

if( key == "name")
c->setName(py::cast<std::string>(kv.second));
try {
BindingBase::SetAttr(*c, key, value);
} catch (py::attribute_error& /*e*/) {
/// kwargs are used to set datafields to their initial values,
/// but they can also be used as simple python attributes, unrelated to SOFA.
/// thus we catch & ignore the py::attribute_error thrown by SetAttr
}
}
return c;
}));

f.def("init", &Controller::init);
f.def("reinit", &Controller::reinit);
Expand Down
31 changes: 21 additions & 10 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,11 @@ along with sofaqtquick. If not, see <http://www.gnu.org/licenses/>.
********************************************************************/

#pragma once

#include "Binding_BaseObject.h"
#include <SofaPython3/DataHelper.h>

#include <sofa/core/behavior/BaseController.h>

template class pybind11::class_<sofa::core::behavior::BaseController,
sofa::core::objectmodel::BaseObject,
sofa::core::sptr<sofa::core::behavior::BaseController>>;


namespace sofapython3
{
using sofa::core::behavior::BaseController;
Expand All @@ -44,14 +39,30 @@ class Controller : public BaseController
{
public:
SOFA_CLASS(Controller, BaseController);
void init() override ;
void reinit() override;
void init() override {};
void reinit() override {};

Controller();
~Controller() override;
Controller() {};
~Controller() override {};
};

void moduleAddController(py::module &m);

class Controller_Trampoline : public Controller
{
public:
Controller_Trampoline() = default;

~Controller_Trampoline() override = default;

void init() override;
void reinit() override;
void handleEvent(sofa::core::objectmodel::Event* event) override;

private:
void callScriptMethod(const py::object& self, sofa::core::objectmodel::Event* event,
const std::string& methodName);
};

} /// namespace sofapython3