Skip to content

Commit 48f11a9

Browse files
committed
Add proper return value to event overriden in python
Currently the controllers implemented in python cannot report the the event processing system in sofa that there is no need anymore to process the event. The PR allows to do. Every onXXXXXX() that implement an event processor can no return a value. If it is None or False then processing of event use this to continue the propagation If it is True (aka: has been processed/no more further), then the SOFA system will not proposed to other component to process the same event
1 parent 9b244d4 commit 48f11a9

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void Controller_Trampoline::reinit()
6868

6969
/// If a method named "methodName" exists in the python controller,
7070
/// methodName is called, with the Event's dict as argument
71-
void Controller_Trampoline::callScriptMethod(
71+
bool Controller_Trampoline::callScriptMethod(
7272
const py::object& self, Event* event, const std::string & methodName)
7373
{
7474
if(f_printLog.getValue())
@@ -81,8 +81,13 @@ void Controller_Trampoline::callScriptMethod(
8181
if( py::hasattr(self, methodName.c_str()) )
8282
{
8383
py::object fct = self.attr(methodName.c_str());
84-
fct(PythonFactory::toPython(event));
84+
py::object result = fct(PythonFactory::toPython(event));
85+
if(result.is_none())
86+
return false;
87+
88+
return py::cast<bool>(result);
8589
}
90+
return false;
8691
}
8792

8893
void Controller_Trampoline::handleEvent(Event* event)
@@ -95,13 +100,17 @@ void Controller_Trampoline::handleEvent(Event* event)
95100
{
96101
py::object fct = self.attr(name.c_str());
97102
if (PyCallable_Check(fct.ptr())) {
98-
callScriptMethod(self, event, name);
103+
bool isHandled = callScriptMethod(self, event, name);
104+
if(isHandled)
105+
event->setHandled();
99106
return;
100107
}
101108
}
102109

103110
/// Is the fallback method available.
104-
callScriptMethod(self, event, "onEvent");
111+
bool isHandled = callScriptMethod(self, event, "onEvent");
112+
if(isHandled)
113+
event->setHandled();
105114
});
106115
}
107116

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Controller_Trampoline : public Controller
5050
std::string getClassName() const override;
5151

5252
private:
53-
void callScriptMethod(const pybind11::object& self, sofa::core::objectmodel::Event* event,
53+
bool callScriptMethod(const pybind11::object& self, sofa::core::objectmodel::Event* event,
5454
const std::string& methodName);
5555
};
5656

0 commit comments

Comments
 (0)