Home › Forum › SofaPython3 › Programming with SofaPython3 › [SOLVED] ContactListener error on SofaPython3
Tagged: contactlistener, SOFA_2012, SofaPython3, Windows_10
- This topic has 7 replies, 2 voices, and was last updated 3 years, 6 months ago by Scheikl.
-
AuthorPosts
-
11 June 2021 at 12:30 #19681chiarasapoBlocked
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 #19687ScheiklBlockedHi @chiarasapo,
as you can see in the bindings for ContactListener, the method getNumberOfContacts is commented out. Could you try uncommenting and recompile it?Cheers,
Paul14 June 2021 at 11:18 #19689chiarasapoBlockedHi First of all thank you for your answer! But unfortunately I uncommented it and recompiled but I get the same error
14 June 2021 at 16:50 #19696ScheiklBlockedHi @chiarasapo,
are you using SofaPython3 out-of-tree?Also try removing the build folder to start clean.
What may also help is doing a separate install step after building.
cmake --install <sofa target folder>/external_directories/sofapython3/bindings
Cheers,
Paul15 June 2021 at 10:59 #19700chiarasapoBlockedIt 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 #19701ScheiklBlockedHi @chiarasapo,
glad I could help!I would suggest using a
Sofa.Core.Controller
to 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. 🙂
For exampleclass 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())
Cheers,
Paul16 June 2021 at 10:58 #19707chiarasapoBlockedThank you very much! I will mark this as resolved.
Have a nice day,
Chiara16 June 2021 at 11:45 #19709ScheiklBlockedYou’re very welcome! 🙂
Thanks, you too!
-
AuthorPosts
- You must be logged in to reply to this topic.