From 48f11a998cfefcc2f6d58ad42abb5b3fbc8fcd1f Mon Sep 17 00:00:00 2001 From: Damien Marchal Date: Mon, 27 Oct 2025 13:20:38 +0100 Subject: [PATCH] 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 --- .../Sofa/Core/Binding_Controller.cpp | 17 +++++++++++++---- .../SofaPython3/Sofa/Core/Binding_Controller.h | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Controller.cpp b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Controller.cpp index 8e87c3bf..0f26c765 100644 --- a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Controller.cpp +++ b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Controller.cpp @@ -68,7 +68,7 @@ void Controller_Trampoline::reinit() /// If a method named "methodName" exists in the python controller, /// methodName is called, with the Event's dict as argument -void Controller_Trampoline::callScriptMethod( +bool Controller_Trampoline::callScriptMethod( const py::object& self, Event* event, const std::string & methodName) { if(f_printLog.getValue()) @@ -81,8 +81,13 @@ void Controller_Trampoline::callScriptMethod( if( py::hasattr(self, methodName.c_str()) ) { py::object fct = self.attr(methodName.c_str()); - fct(PythonFactory::toPython(event)); + py::object result = fct(PythonFactory::toPython(event)); + if(result.is_none()) + return false; + + return py::cast(result); } + return false; } void Controller_Trampoline::handleEvent(Event* event) @@ -95,13 +100,17 @@ void Controller_Trampoline::handleEvent(Event* event) { py::object fct = self.attr(name.c_str()); if (PyCallable_Check(fct.ptr())) { - callScriptMethod(self, event, name); + bool isHandled = callScriptMethod(self, event, name); + if(isHandled) + event->setHandled(); return; } } /// Is the fallback method available. - callScriptMethod(self, event, "onEvent"); + bool isHandled = callScriptMethod(self, event, "onEvent"); + if(isHandled) + event->setHandled(); }); } diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Controller.h b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Controller.h index dee81152..d1bd9166 100644 --- a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Controller.h +++ b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Controller.h @@ -50,7 +50,7 @@ class Controller_Trampoline : public Controller std::string getClassName() const override; private: - void callScriptMethod(const pybind11::object& self, sofa::core::objectmodel::Event* event, + bool callScriptMethod(const pybind11::object& self, sofa::core::objectmodel::Event* event, const std::string& methodName); };