Home › Forum › SOFA › Programming with SOFA › Stop Animation function in sofa
Tagged: 64_bits, Qt, scene file, SOFA, SOFA_1706, Windows_10
- This topic has 16 replies, 4 voices, and was last updated 5 years, 8 months ago by timp.
-
AuthorPosts
-
30 January 2019 at 08:34 #12957rubab123Blocked
Hello everyone,
I am working with visual studio , I want some code to be executed once the animation is stop, for this I think i have to use stop animation function, but how can I inherit it in my plugin component which is totally separated from other sofa files. This plugin component is self created, how can i use stop animation function to use this utility that a certain chunk of code will be displayed once animation is completed.
Any help will highly be appreciated, i truly get help from this forum and thank you all once again.30 January 2019 at 10:15 #12958Bruno MarquesBlockedHi @rubab123,
I’m not sure if I understood correctly, but if what you want is stop / pause the simulation from within a component, you can do this by calling setAnimate(false) on the root context:
this->getContext()->getRootContext()->setAnimate(false);
You can also do this from PythonScriptControllers, if that’s more convenient to you:
node.getRootContext().animate = False
30 January 2019 at 13:44 #12963rubab123BlockedThis stops the animation but what i want is when user clicks on animate and animation is stopped, then due to some method i get to know that now animation is stopped and the code should be displayed now.
is there any way i can get this?
@Bruno30 January 2019 at 14:11 #12966HugoKeymasterHi @rubab
Does this new topic : https://www.sofa-framework.org/community/forum/topic/get-signal-when-scene-file-is-closed-and-display-a-graph-qtablewidget/ closes this one ?
I would also be curious about your project @rubab? Could you tell us more about it?
Cheers,Hugo
31 January 2019 at 07:39 #12967rubab123Blocked@Hugo yes you may close this topic, may be I will get desired responses on other question.
Yes, sure. We are developing a training simulator for Cell injection procedures (i.e ICSI etc), users will get training on this simulator at how to inject the substance into the cell. Once they will get training, they can easily work on real icsi procedures.
We are using SOFA for making small scene based exercises, which train them from basic (pipette moving towards cell) modules to advanced modules (full icsi procedure).
Now I also want to display a graph which contains user’s accuracy (how much time he has taken to complete the exercise, distance he covered towards cell etc) at the end of each module.
I am facing some issues in that, which is why I am here on SOFA forum 🙂 I hope you got an idea about our work.? @Hugo31 January 2019 at 09:52 #12968Bruno MarquesBlocked@rubab123, after reading your last post on the other topic you opened, I’m quite confident that my initial reply answers your problem:
What you want, if I understood correctly, is to wait for a user to execute a simulation in SOFA that would gather some data on the user’s inputs (accuracy, speed…), and, once the simulation is performed, stop the animation and display a plot using the gathered data.
The way I would do it is as follows:
If the scene is made in python (using SofaPython) I would create my scene with a PythonScriptController, and in the onEndAnimationStep method, I would manually stop the scene once the simulation is done (
node.getRootContext().animate = False
), then plot the given data.
If it’s in c++, in a component inheriting BaseObject for instance, I would implement BaseObject’s handleEvent method to check for theAnimateEndEvent
, and if the simulation is complete, stop the simulation (this->getContext()->getRootContext()->animate(false);
), and then plot the graph.I *think* that you want to use SOFA’s simulation loop to handle the interactions with the plot (which would imply keeping the animation running while the plot is being displayed, which would also imply keeping the simulation running…), but that’s not the way to go IMHO.
If instead you keep the SOFA simulation stopped, and handle your plot in a dedicated QApp, you would probably reach the same result with much less troubble.Does this answer your question?
Also, please keep your questions within the same topic on the forum, opening multiple topics for a same question makes things confusing for other users that will be looking for a similar issue.
31 January 2019 at 11:43 #12969rubab123Blocked@Bruno yes thats what I wanted, thank you so much.
I did thisif (sofa::simulation::AnimateEndEvent::checkEventType(event))
{
this->getContext()->getRootContext()->setAnimate(false);}In header file: sofa::core::objectmodel::Event* event;
Now when I open modeler and execute the scn file it gives this:FROM SOFA [ERR] >> ########## SIG 11 – SIGSEGV: segfault ##########
FROM SOFA [ERR] >> 42
FROM SOFA [ERR] >> : sofa::helper::BackTrace::dump – 0x7ff821b94fc041: sofa::helper::BackTrace::sig – 0x7ff821b9514040: seh_filter_exe – 0x7ff850521b4039: seh_filter_exe – 0x7ff850521b4038: _C_specific_handler – 0x7ff834afbff037: _chkstk – 0x7ff854039f6036: RtlLookupFunctionEntry – 0x7ff853fa91a035: KiUserExceptionDispatcher – 0x7ff8540390d034: sofa::simulation::AnimateEndEvent::checkEventType – 0x7ff821d13cb0
FROM SOFA [ERR] >> 33: getModuleComponentList – 0x7ff8321237e032: sofa::simulation::Visitor::treeTraversal – 0x7ff821d30af031: sofa::simulation::BehaviorUpdatePositionVisitor::processNodeTopDown – 0x7ff821d30ef030: sofa::simulation::graph::DAGNode::executeVisitorTopDown – 0x7ff82b1d99d029: sofa::simulation::graph::DAGNode::doExecuteVisitor – 0x7ff82b1d851028: sofa::simulation::Node::executeVisitor – 0x7ff821d5d46027: sofa::simulation::DefaultAnimationLoop::step – 0x7ff821d36ef026: sofa::simulation::Simulation::animate – 0x7ff821d799e025: sofa::gui::qt::RealGUI::step – 0x7ff821e7677024: QMetaObject::activate – 0x5b3dbf3023: QTimer::timeout – 0x5b4541f022: QObject::event – 0x5b3e0ec021: QApplicationPrivate::notify_helper – 0x5b847e7020: QApplication::notify – 0x5b84564019: QCoreApplication::notifyInternal2 – 0x5b3bb92018: QEventDispatcherWin32::event – 0x5b402cc017: QApplicationPrivate::notify_helper – 0x5b847e7016: QApplication::notify – 0x5b84564015: QCoreApplication::notifyInternal2 – 0x5b3bb92014: QCoreApplicationPrivate::sendPostedEvents – 0x5b3bd4b013: qt_plugin_query_metadata – 0x7ff8041cfeb012: QEventDispatcherWin32::processEvents – 0x5b40326011: CallWindowProcW – 0x7ff853e4b78010: DispatchMessageW – 0x7ff853e4b4209: QEventDispatcherWin32::processEvents – 0x5b4032608: qt_plugin_query_metadata – 0x7ff8041cfeb07: QEventLoop::exec – 0x5b3b7e206: QCoreApplication::exec – 0x5b3ba4d05: sofa::gui::qt::RealGUI::mainLoop – 0x7ff821e749504: sofa::gui::GUIManager::MainLoop – 0x7ff83d5791603: sofa::gui::GUIManager::MainLoop – 0x7ff83d5791602: sofa::gui::GUIManager::MainLoop – 0x7ff83d5791601: BaseThreadInitThunk – 0x7ff853d727600: RtlUserThreadStart – 0x7ff854000d3031 January 2019 at 11:50 #12970rubab123BlockedMay be I am not able to inherit it properly @Bruno can you access my code through teamviewer or what, I am not very much experienced in SOFA and it is taking a lot of time to get me resolve this.
1 February 2019 at 08:39 #12972rubab123Blocked@Bruno I got this, after checking if the AnimateEndEvent has occurred then I stopped animation
FROM SOFA [ERR] >> ########## SIG 11 – SIGSEGV: segfault ##########
FROM SOFA [ERR] >> 42
FROM SOFA [ERR] >> : sofa::helper::BackTrace::dump – 0x7ff833244fc041: sofa::helper::BackTrace::sig – 0x7ff83324514040: seh_filter_exe – 0x7ff850521b4039: seh_filter_exe – 0x7ff850521b4038: _C_specific_handler – 0x7ff834afbff037: _chkstk – 0x7ff854039f6036: RtlLookupFunctionEntry – 0x7ff853fa91a035: KiUserExceptionDispatcher – 0x7ff8540390d034: sofa::simulation::AnimateBeginEvent::checkEventType – 0x7ff8333c3b00
FROM SOFA [ERR] >> 33: initExternalModule – 0x7ff8350c23b032: sofa::simulation::Visitor::treeTraversal – 0x7ff8333e0af031: sofa::simulation::BehaviorUpdatePositionVisitor::processNodeTopDown – 0x7ff8333e0ef030: sofa::simulation::graph::DAGNode::executeVisitorTopDown – 0x7ff82b6399d029: sofa::simulation::graph::DAGNode::doExecuteVisitor – 0x7ff82b63851028: sofa::simulation::Node::executeVisitor – 0x7ff83340d46027: sofa::simulation::DefaultAnimationLoop::step – 0x7ff8333e6ef026: sofa::simulation::Simulation::animate – 0x7ff8334299e025: sofa::gui::qt::RealGUI::step – 0x7ff8261b677024: QMetaObject::activate – 0x5b49bf3023: QTimer::timeout – 0x5b5141f022: QObject::event – 0x5b4a0ec021: QApplicationPrivate::notify_helper – 0x5b847e7020: QApplication::notify – 0x5b84564019: QCoreApplication::notifyInternal2 – 0x5b47b92018: QEventDispatcherWin32::event – 0x5b4c2cc017: QApplicationPrivate::notify_helper – 0x5b847e7016: QApplication::notify – 0x5b84564015: QCoreApplication::notifyInternal2 – 0x5b47b92014: QCoreApplicationPrivate::sendPostedEvents – 0x5b47d4b013: qt_plugin_query_metadata – 0x7ff813b2feb012: QEventDispatcherWin32::processEvents – 0x5b4c326011: CallWindowProcW – 0x7ff853e4b78010: DispatchMessageW – 0x7ff853e4b4209: QEventDispatcherWin32::processEvents – 0x5b4c32608: qt_plugin_query_metadata – 0x7ff813b2feb07: QEventLoop::exec – 0x5b477e206: QCoreApplication::exec – 0x5b47a4d05: sofa::gui::qt::RealGUI::mainLoop – 0x7ff8261b49504: sofa::gui::GUIManager::MainLoop – 0x7ff83d7891603: sofa::gui::GUIManager::MainLoop – 0x7ff83d7891602: sofa::gui::GUIManager::MainLoop – 0x7ff83d7891601: BaseThreadInitThunk – 0x7ff853d727600: RtlUserThreadStart – 0x7ff854000d30
FROM SOFA [OUT] >> [WARNING] [SofaSimulationTree] the library has not been cleaned up (sofa::simulation::tree::cleanup() has never been called, see sofa/helper/init.h)The changes I made in my code are: sofa::core::objectmodel::Event* event; in header file
and if (simulation::AnimateEndEvent::checkEventType(event))
{this->getContext()->getRootContext()->setAnimate(false);
std::cout << “it is stopped”;}
But it gave me above errors, my sofa crashed. any suggestions @Bruno Marques1 February 2019 at 10:13 #12974Bruno MarquesBlocked@rubab123 Your mistake I suppose is that you declared an event pointer in your class declaration.
The prototype of the handleEvent function tells you that it takes a sofa::core::objectmodel::Event pointer as an argument, so that’s the one you have to work with. if you declare an Event* in your class, this one will of course be uninitialized hence unallocated, which means that you’re accessing an undefined space in memory, causing the crash.
Just remove the line you added in your header file, it should work.1 February 2019 at 11:28 #12976rubab123Blocked@Bruno I removed it and now I have this
if (simulation::AnimateEndEvent::checkEventType)
{this->getContext()->getRootContext()->setAnimate(false);
}
Now it doesn’t wait for the animation to stop, I open my scn file and start animate after a second It gets stopped (animation is stopped) itself. and nothing happens. Why doesnt it check if condition and wait for Animation End event? why it closes animation itself?1 February 2019 at 13:21 #12977Bruno MarquesBlockedSorry, I replied a bit too fast the first time:
The results you’re getting means it works:What your code is doing is catching every AnimateEndEvent signals, and stopping the simulation each time.
The AnimateEndEvent’s name is misleading, I admit: it should actually be called “SimulationStepEndEvent”. This signal is sent at the end of each time step.
What you need is defining a condition to stop your simulation (for instance, when the user reaches a target with the surgical tool, or by using a timer.There is no event sent when the simulation is manually stopped by pressing the animate button in the user interface. What I was suggesting was something like this:
void handleEvent(objectmodel::Event* e) { if (simulation::AnimateEndEvent::checkEventType && someInterruptionCondition == true) { this->getContext()->getRootContext()->setAnimate(false); openSomeOtherQApplication(simulationResults) // Open the Widget in a dedicated interface & display the results of the simulation } }
Hope this helps
11 February 2019 at 15:13 #13026timpBlockedHi all,
I have a follow-up question. In my Python scene I would like to be able stop and start the animation upon pressing a key. While I can stop the animation using:
rootNode.getRootContext().animate = False
starting it again by writing:
rootNode.getRootContext().animate = True
does not work.
Is it not possible to restart the animation in this way?
I appreciate your help!13 February 2019 at 09:47 #13030Bruno MarquesBlockedHi @Tim,
That’s a good question. To my knowledge there’s no such mechanism.
I looked it up a little bit and it seems that the way the animation works in runSofa is that clicking on the animate button:
– triggers the loop that will call theRealGUI::step()
method.
– This method calls theSimulation::animate()
method that triggers the execution of 1 step of the simulation on the root node for a given dt.
– performs a check on thegetAnimate()
method, and:
– if it returns false, and simulates a click on the animate button, which in turn it stops the loop.
– if it returns true, the next step is called.The thing is, that when the loop is stopped, the program does not enter the loop anymore until the animate button is clicked again, so setting the animate flag to True in python will not actually re-run the loop.
I don’t know if that’s clear…
Anyway, one way to restart the simulation would be to emulate a click on the animate button of the GUI. It’s not really clean, but it could be done by simulating a click on the animate button, or a key press on the spacebar… This is quite a hack, but it would work.
You can simulate keyboard presses usingpyautogui
in python for instance. Your runSofa window must have the focus though.If you go for simulating the keyboard press, there’s still an issue, which is that the spacebar key also toggles the “fly mode” in runSofa’s camera controller, and I have no idea how to prevent that behavior without digging in the code..
The current runSofa gui is quite old, and work is being done to create a new GUI, rare are the bug fixes and new features arriving in runSofa. Sadly, it’s a work in progress and for now we have to bare with runSofa, or develop our own..
Hope this helps… good luck!14 February 2019 at 10:23 #13035timpBlockedHi @Bruno,
thanks for your reply and the detailed explanation. I will have a look atpyautogui
then or try to find a workaround.19 March 2019 at 17:48 #1321820 March 2019 at 09:11 #13233 -
AuthorPosts
- You must be logged in to reply to this topic.