Skip to content

Commit e39feb5

Browse files
committed
Add binding for Controller::draw() and drawTools()
So it become possible to draw from python.
1 parent e42fc90 commit e39feb5

File tree

9 files changed

+139
-1
lines changed

9 files changed

+139
-1
lines changed

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_BaseObject.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
// Imports for getCategories
3030
#include <sofa/core/objectmodel/ContextObject.h>
3131
#include <sofa/core/visual/VisualModel.h>
32+
#include <sofa/core/visual/VisualParams.h>
3233
#include <sofa/core/BaseMapping.h>
3334
#include <sofa/core/BehaviorModel.h>
3435
#include <sofa/core/CollisionModel.h>

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_BaseObject_doc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ static auto Class =
4545
root.obj.position.value # Access the position of the object
4646
4747
)";
48+
static auto draw =
49+
R"(
50+
Implement this function to draw an object in the 3D view.
51+
)";
52+
4853
static auto init =
4954
R"(
5055
Initialization method called at graph creation and modification, during top-down traversal.Initialize data.

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Controller.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#include <pybind11/pybind11.h>
2222
#include <pybind11/cast.h>
23-
23+
#include <sofa/core/visual/VisualParams.h>
2424
#include <SofaPython3/Sofa/Core/Binding_Base.h>
2525
#include <SofaPython3/Sofa/Core/Binding_Controller.h>
2626
#include <SofaPython3/Sofa/Core/Binding_Controller_doc.h>
@@ -45,6 +45,13 @@ std::string Controller_Trampoline::getClassName() const
4545
return py::str(py::cast(this).get_type().attr("__name__"));
4646
}
4747

48+
void Controller_Trampoline::draw(const sofa::core::visual::VisualParams* params)
49+
{
50+
PythonEnvironment::executePython(this, [this, params](){
51+
PYBIND11_OVERLOAD(void, Controller, draw, params);
52+
});
53+
}
54+
4855
void Controller_Trampoline::init()
4956
{
5057
PythonEnvironment::executePython(this, [this](){
@@ -131,6 +138,9 @@ void moduleAddController(py::module &m) {
131138

132139
f.def("init", &Controller::init);
133140
f.def("reinit", &Controller::reinit);
141+
f.def("draw", [](Controller& self, sofa::core::visual::VisualParams* params){
142+
self.draw(params);
143+
});
134144
}
135145

136146

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Controller.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class Controller_Trampoline : public Controller
4343

4444
void init() override;
4545
void reinit() override;
46+
void draw(const sofa::core::visual::VisualParams* params) override;
47+
4648
void handleEvent(sofa::core::objectmodel::Event* event) override;
4749

4850
std::string getClassName() const override;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/******************************************************************************
2+
* SOFA, Simulation Open-Framework Architecture *
3+
* (c) 2021 INRIA, USTL, UJF, CNRS, MGH *
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 <sofa/type/Quat.h>
22+
#include <pybind11/pybind11.h>
23+
#include <pybind11/pytypes.h>
24+
#include <pybind11/stl.h>
25+
26+
#include <SofaPython3/Sofa/Core/Binding_Base.h>
27+
#include <sofa/core/visual/VisualParams.h>
28+
29+
#include <SofaPython3/Sofa/Core/Binding_VisualParams.h>
30+
#include <SofaPython3/Sofa/Core/Binding_VisualParams_doc.h>
31+
32+
#include <SofaPython3/PythonFactory.h>
33+
34+
#include <sofa/type/RGBAColor.h>;
35+
36+
namespace py { using namespace pybind11; }
37+
using sofa::core::objectmodel::BaseObject;
38+
using sofa::core::visual::VisualParams;
39+
using sofa::core::visual::DrawTool;
40+
41+
namespace sofapython3 {
42+
43+
void moduleAddVisualParams(py::module &m)
44+
{
45+
py::class_<VisualParams> vp(m, "VisualParams", sofapython3::doc::visualParams::baseVisualParamsClass);
46+
vp.def("getDrawTool", [](VisualParams *self){ return self->drawTool() ;});
47+
48+
py::class_<DrawTool> dt(m, "DrawTool", sofapython3::doc::visualParams::baseVisualParamsClass);
49+
dt.def("drawPoints", [](DrawTool *self, const std::vector<sofa::type::Vec3> &points, float size ){
50+
self->drawPoints(points, size, sofa::type::RGBAColor::white());
51+
});
52+
}
53+
54+
} /// namespace sofapython3
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/******************************************************************************
2+
* SOFA, Simulation Open-Framework Architecture *
3+
* (c) 2021 INRIA, USTL, UJF, CNRS, MGH *
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+
#pragma once
22+
23+
#include <pybind11/pybind11.h>
24+
25+
namespace sofapython3 {
26+
27+
void moduleAddVisualParams(pybind11::module &m);
28+
29+
} /// namespace sofapython3
30+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/******************************************************************************
2+
* SOFA, Simulation Open-Framework Architecture *
3+
* (c) 2021 INRIA, USTL, UJF, CNRS, MGH *
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+
#pragma once
22+
23+
namespace sofapython3::doc::visualParams {
24+
25+
static auto baseVisualParamsClass =
26+
R"(
27+
TBD
28+
)";
29+
30+
} // namespace sofapython3::doc::visualParams

bindings/Sofa/src/SofaPython3/Sofa/Core/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ set(HEADER_FILES
5151
${CMAKE_CURRENT_SOURCE_DIR}/Binding_BaseMeshTopology.h
5252
${CMAKE_CURRENT_SOURCE_DIR}/Binding_TaskScheduler.h
5353
${CMAKE_CURRENT_SOURCE_DIR}/Binding_TaskScheduler_doc.h
54+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_VisualParams.h
55+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_VisualParams_doc.h
5456
)
5557

5658
set(SOURCE_FILES
@@ -83,6 +85,7 @@ set(SOURCE_FILES
8385
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Topology.cpp
8486
${CMAKE_CURRENT_SOURCE_DIR}/Binding_BaseMeshTopology.cpp
8587
${CMAKE_CURRENT_SOURCE_DIR}/Binding_TaskScheduler.cpp
88+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_VisualParams.cpp
8689
${CMAKE_CURRENT_SOURCE_DIR}/Submodule_Core.cpp
8790
)
8891

bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ using sofa::helper::logging::Message;
4848
#include <SofaPython3/Sofa/Core/Binding_BaseMeshTopology.h>
4949
#include <SofaPython3/Sofa/Core/Binding_Topology.h>
5050
#include <SofaPython3/Sofa/Core/Binding_TaskScheduler.h>
51+
#include <SofaPython3/Sofa/Core/Binding_VisualParams.h>
5152

5253
#include <SofaPython3/Sofa/Core/Data/Binding_DataString.h>
5354
#include <SofaPython3/Sofa/Core/Data/Binding_DataLink.h>
5455
#include <SofaPython3/Sofa/Core/Data/Binding_DataVectorString.h>
5556
#include <SofaPython3/Sofa/Core/Data/Binding_DataContainer.h>
5657

58+
5759
#include <sofa/core/init.h>
5860

5961
namespace sofapython3
@@ -153,6 +155,7 @@ PYBIND11_MODULE(Core, core)
153155
moduleAddBaseMeshTopology(core);
154156
moduleAddPointSetTopologyModifier(core);
155157
moduleAddTaskScheduler(core);
158+
moduleAddVisualParams(core);
156159

157160
// called when the module is unloaded
158161
auto atexit = py::module_::import("atexit");

0 commit comments

Comments
 (0)