Skip to content
Merged
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: 13 additions & 4 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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<bool>(result);
}
return false;
}

void Controller_Trampoline::handleEvent(Event* event)
Expand All @@ -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();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
Loading