11 June 2021 at 12:30 #19681
Hi, I am working with sofapython3 and I would like to implement the following:
As soon as a contact between body1 and body2 is detected, a spring should be added between the two bodies. Then, when the bodies move apart again, after a certain threshold distance, the spring should be removed.
Right now I am trying to create the first step: detecting the contact. I am using the ContactListener as:
Listener=name.addObject('ContactListener', name="Listener”, collsionModel1=Skin.SkinColl.TriangleCollisionSkin.getLinkPath(), collisionModel2 = Scalpel.InstrumentColl.SphereCollisionInstrument.getLinkPath())
And the listener is created. However when I try using the ContactListener method getNumberOfContacts() I receive this error:
[ERROR] [SofaPython3::SceneLoader] KeyError: 'getNumberOfContacts'
Should I do something differently?
Any help on this step or any of the other steps would be very appreciated!11 June 2021 at 20:51 #1968714 June 2021 at 11:18 #19689
Hi First of all thank you for your answer! But unfortunately I uncommented it and recompiled but I get the same error14 June 2021 at 16:50 #1969615 June 2021 at 10:59 #19700
It worked perfectly, thanks a lot! Now it checks for collisions but it only does it once when I load the project on sofa. Do you know how I can activate it only during animation time?15 June 2021 at 11:17 #19701
glad I could help!
I would suggest using a
Sofa.Core.Controllerto call the function on a specified event, such as AnimateEndEvent.
There is a nice example in the SofaPython3 repo.
Just delete all the functions that you do not need. 🙂
class PrintContactController(Sofa.Core.Controller): def __init__(self, *args, **kwargs): Sofa.Core.Controller.__init__(self, *args, **kwargs) def init(self, contact_listener): self.contact_listener = contact_listener def onAnimateBeginEvent(self, event): # called at each begin of animation step print(self.contact_listener.getNumberOfContacts())
Paul16 June 2021 at 10:58 #19707
Thank you very much! I will mark this as resolved.
Have a nice day,
Chiara16 June 2021 at 11:45 #19709
You’re very welcome! 🙂
Thanks, you too!
- You must be logged in to reply to this topic.