Skip to content

Commit 8b4b20b

Browse files
committed
Take comments into account
1 parent 0719834 commit 8b4b20b

6 files changed

Lines changed: 40 additions & 32 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ using sofa::core::objectmodel::BaseComponent;
3535
// ---------------------------------------------------------------------------
3636

3737
Component_Trampoline::Component_Trampoline()
38-
: TrampolineBase(this) // pass this as BaseComponent* — no CRTP needed
38+
: BasetTrampoline(this) // pass this as BaseComponent* — no CRTP needed
3939
{
4040
}
4141

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
namespace sofapython3 {
2929

3030

31-
class TrampolineBase
31+
class BasetTrampoline
3232
{
3333
public:
34-
explicit TrampolineBase(sofa::core::objectmodel::BaseComponent* self);
35-
~TrampolineBase();
34+
explicit BasetTrampoline(sofa::core::objectmodel::BaseComponent* self);
35+
~BasetTrampoline();
3636

3737
void trampoline_handleEvent(sofa::core::objectmodel::Event* event);
3838
std::string trampoline_getClassName() const;
@@ -46,7 +46,7 @@ class TrampolineBase
4646
const std::string& methodName);
4747

4848
/// Raw non-owning pointer to the concrete trampoline as BaseComponent.
49-
/// Safe because TrampolineBase is always embedded in the same object.
49+
/// Safe because BasetTrampoline is always embedded in the same object.
5050
sofa::core::objectmodel::BaseComponent* m_componentSelf { nullptr };
5151

5252
pybind11::object m_pySelf;
@@ -70,7 +70,7 @@ class Component : public sofa::core::objectmodel::BaseComponent {
7070
void reinit() override {}
7171
};
7272

73-
class Component_Trampoline : public Component, public TrampolineBase
73+
class Component_Trampoline : public Component, public BasetTrampoline
7474
{
7575
public:
7676
SOFA_CLASS(Component_Trampoline, Component);

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Component.inl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,22 @@ using sofa::core::objectmodel::Event;
3737
using sofa::core::objectmodel::BaseComponent;
3838

3939

40-
inline TrampolineBase::TrampolineBase(BaseComponent* self)
40+
inline BasetTrampoline::BasetTrampoline(BaseComponent* self)
4141
: m_componentSelf(self)
4242
{
4343
}
4444

45-
inline TrampolineBase::~TrampolineBase()
45+
inline BasetTrampoline::~BasetTrampoline()
4646
{
4747
if (m_cacheInitialized)
4848
{
49-
PythonEnvironment::gil acquire {"~TrampolineBase"};
49+
PythonEnvironment::gil acquire {"~BasetTrampoline"};
5050
m_methodCache.clear();
5151
m_pySelf = py::object();
5252
}
5353
}
5454

55-
inline void TrampolineBase::initializePythonCache()
55+
inline void BasetTrampoline::initializePythonCache()
5656
{
5757
if (m_cacheInitialized)
5858
return;
@@ -66,7 +66,7 @@ inline void TrampolineBase::initializePythonCache()
6666
m_cacheInitialized = true;
6767
}
6868

69-
inline py::object TrampolineBase::getCachedMethod(const std::string& methodName)
69+
inline py::object BasetTrampoline::getCachedMethod(const std::string& methodName)
7070
{
7171
auto it = m_methodCache.find(methodName);
7272
if (it != m_methodCache.end())
@@ -84,7 +84,7 @@ inline py::object TrampolineBase::getCachedMethod(const std::string& methodName)
8484
return method;
8585
}
8686

87-
inline bool TrampolineBase::callCachedMethod(const py::object& method, Event* event)
87+
inline bool BasetTrampoline::callCachedMethod(const py::object& method, Event* event)
8888
{
8989
if (m_componentSelf->f_printLog.getValue())
9090
{
@@ -99,14 +99,14 @@ inline bool TrampolineBase::callCachedMethod(const py::object& method, Event* ev
9999
return py::cast<bool>(result);
100100
}
101101

102-
inline void TrampolineBase::invalidateMethodCache(const std::string& methodName)
102+
inline void BasetTrampoline::invalidateMethodCache(const std::string& methodName)
103103
{
104104
if (!m_cacheInitialized)
105105
return;
106106
m_methodCache.erase(methodName);
107107
}
108108

109-
inline std::string TrampolineBase::trampoline_getClassName() const
109+
inline std::string BasetTrampoline::trampoline_getClassName() const
110110
{
111111
PythonEnvironment::gil acquire {"getClassName"};
112112

@@ -117,7 +117,7 @@ inline std::string TrampolineBase::trampoline_getClassName() const
117117
return py::str(py::type::of(py::cast(m_componentSelf)).attr("__name__"));
118118
}
119119

120-
inline bool TrampolineBase::callScriptMethod(
120+
inline bool BasetTrampoline::callScriptMethod(
121121
const py::object& self, Event* event, const std::string& methodName)
122122
{
123123
if (m_componentSelf->f_printLog.getValue())
@@ -138,7 +138,7 @@ inline bool TrampolineBase::callScriptMethod(
138138
return false;
139139
}
140140

141-
inline void TrampolineBase::trampoline_handleEvent(Event* event)
141+
inline void BasetTrampoline::trampoline_handleEvent(Event* event)
142142
{
143143
PythonEnvironment::executePython(m_componentSelf, [this, event](){
144144
if (!m_cacheInitialized)

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@ namespace sofapython3::doc::component
2424
{
2525
static auto componentClass =
2626
R"(
27-
Overridable class for user interaction on SOFA Components
27+
Overridable class for SOFA Components
2828
2929
It can catch events to trigger actions, such as onAnimateBeginEvent, onAnimateEndEvent or onPythonScriptEvent.
30-
A new custom controller class needs to be defined to use a controller in a script,
30+
A new custom Components class needs to be defined to use a Components in a script,
3131
and that class needs to reimplement the __init__ method.
3232
3333
:example of use:
3434
35-
In the following example, we redefine the controller class, and reimplement the __init__ method.
36-
We also implement the onAnimateBeginEvent, that will be activted everytime an animation step ends
35+
In the following example, we redefine the Components class, and reimplement the __init__ method.
36+
We also implement the init and onAnimateBeginEvent methods, the last one will be activted everytime an animation step ends
3737
and that will simply print a message in the command line. In the createScene function, we initialize
38-
the controller and add it to the rootNode.
39-
If you run this with runSofa, it will simply endlessly print `onAnimateBeginEvent`
38+
the Components and add it to the rootNode.
39+
If you run this with runSofa, it will first print "Init component" before you start the simulation, then
40+
it will simply endlessly print `onAnimateBeginEvent`
41+
4042
when you click the Animate button.
4143
4244
.. code-block:: python
@@ -45,18 +47,24 @@ static auto componentClass =
4547
4648
class MyComponent(Sofa.Core.Component):
4749
def __init__(self, *args, **kwargs):
48-
## These are needed (and the normal way to override from a python class)
49-
Sofa.Core.Component.__init__(self, *args, **kwargs)
50-
print(" Python::__init__::"+str(self.name))
50+
## These are needed (and the normal way to override from a python class)
51+
Sofa.Core.Component.__init__(self, *args, **kwargs)
52+
print(" Python::__init__::"+str(self.name))
53+
54+
def init():
55+
"""This function is called at the initialization stage of the component.
56+
Right after the call to 'createScene' when the root node is initialized.
57+
"""
58+
print("Init component")
5159
5260
def onEvent(self, event):
53-
"""This function is the fallback one that is called if the XXXX event is
54-
received but there is not overriden onXXXX() method.
55-
"""
56-
print("generic event handler catched ", event)
61+
"""This function is the fallback one that is called if the XXXX event is
62+
received but there is not overriden onXXXX() method.
63+
"""
64+
print("generic event handler catched ", event)
5765
5866
def onAnimateBeginEvent(self, event):
59-
print("onAnimateBeginEvent")
67+
print("onAnimateBeginEvent")
6068
6169
def createScene(rootNode):
6270
controller = MyComponent(name="MyC")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ using sofa::core::behavior::BaseController;
4343
// ---------------------------------------------------------------------------
4444

4545
Controller_Trampoline::Controller_Trampoline()
46-
: TrampolineBase(this) // pass this as BaseComponent* — no CRTP needed
46+
: BasetTrampoline(this) // pass this as BaseComponent* — no CRTP needed
4747
{
4848
}
4949

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Controller : public sofa::core::behavior::BaseController {
3838
void reinit() override {}
3939
};
4040

41-
class Controller_Trampoline : public Controller, public TrampolineBase
41+
class Controller_Trampoline : public Controller, public BasetTrampoline
4242
{
4343
public:
4444
SOFA_CLASS(Controller_Trampoline, Controller);

0 commit comments

Comments
 (0)