Forum Replies Created
-
AuthorPosts
-
7 December 2020 at 18:51 in reply to: SofaPython3: Troubles starting Sofa GUI from Python script #17970AOBlocked
Hello,
As @ScheicklP told me a few days ago you might want to type :
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:Mypath/sofa/build/lib
and retry.(with the correct path)
If it works you can add this line to your .bashrc file.Alban.
AOBlockedAlright, I managed to find a solution to the problem but it’s clearly not what should be done in the long term. I hope at some point we will be able to get the original sender as a data from the value argument.
An event class is often declared as such :
class SOFA_SIMULATION_CORE_API MyEvent : public sofa::core::objectmodel::Event { public: SOFA_EVENT_H( MyEvent ) MyEvent( SReal dt ); MyEvent() {} void setDt(SReal sdt){dt = sdt;} ~MyEvent() override; SReal getDt() const { return dt; } inline static const char* GetClassName() { return "MyEvent"; } protected: SReal dt; };
The point of interest here is the macro
SOFA_EVENT_H( MyEvent )
which defines a bunch of functions. One of themgetClassName()
defines the name of the event sent to each desired components.
The only way (that I found atleast) to transfer some information along the event propagation is to add it to actual the event name.In my case I only want to give the id of the component that sent the signal but it can easily be adapted to suit your demand.
We can explicit the macro in order to only change the
getClassName
function (and not theGetClassName
one):class SOFA_SIMULATION_CORE_API MyEvent : public sofa::core::objectmodel::Event { public: virtual size_t getEventTypeIndex() const override { return MyEvent::s_eventTypeIndex; } static bool checkEventType( const Event* event ) { return event->getEventTypeIndex() == MyEvent::s_eventTypeIndex; } virtual const char* getClassName() const override {return name.c_str();} MyEvent(SReal dt, uint id=0): sofa::core::objectmodel::Event(), dt(dt) { name = std::string("MyEvent_").append(std::to_string(id)).c_str(); } MyEvent() {} void setDt(SReal sdt){dt = sdt;} ~MyEvent() override; SReal getDt() const { return dt; } inline static const char* GetClassName() { return "MyEvent"; } protected: SReal dt; static const size_t s_eventTypeIndex; std::string name; };
Now from the python script we can catch any event from
def onEvent(self, value)
.
Previously when printing value you’ll get something like this:
{‘type’: ‘MyEvent’, ‘isHandled’: False}But with the modification presented above the display will look a tiny bit different:
{‘type’: ‘MyEvent_0’, ‘isHandled’: False}From this in your python script you can write:
def onEvent(self, value): [event, id] = value['type'].split('_') if event == "the name of the event": doStuff(int(id))
or you can specify each
def onMyEvent_id(self, value)
if you need to.TLDR : Remove the macro, change the name of the event and catch it with onEvent
Alban.
AOBlockedThanks, it’s very clear now 🙂
-
AuthorPosts