Home › Forum › SOFA › Using SOFA › [SOLVED] SofaPython3 getObjects
Tagged: Linux_ubuntu, SOFA_2106, using sofapython3
- This topic has 4 replies, 2 voices, and was last updated 2 years, 11 months ago by Hugo.
-
AuthorPosts
-
9 December 2021 at 16:33 #21108EricBlocked
Hi All,
I’ve started creating my surgical scene by imitating liver.py, and now I’m wondering if there is any documentation available that records sofaPython3 functions.
For example, I am trying to add fixed constraints to my tissue meshes nodes whose x coordinate is smaller than 0.01, which requires me to retrieve mesh nodes positions. However, I don’t know which function is responsible for “getobject” and “getPosX” in python. Could you help with it?
Cheers
9 December 2021 at 19:11 #21109HugoKeymasterhey @ericcui
Definitely, please find the readthedoc for SofaPython3!
I advice you to take a look this example which gives a good example of a so-called “python controller” : a python class that allows to define actions while the simulation is running.
However, in your case, all you need to do is to define a BoxROI with a FixedConstraint more or less as follows:
node.addObject('BoxROI', name="BoxSelectingPointToBeFixed", box="-999 -999 -999 0.01 999 999" ) node.addObject('FixedConstraint', name="FixedConstraint", indices="@BoxSelectingPointToBeFixed.indices", listening="true")
Best wishes,
Hugo
13 December 2021 at 11:19 #21136EricBlockedHi Hugo,
Thanks for your reply and suggestion.
I’ve followed the example of “python controller” as https://github.com/sofa-framework/SofaPython3/blob/master/examples/liver-scriptcontroller.py, but still fail to stream position data when I trigger the event.
My code is attached as below, aiming to print out the position of the first tissue index on pressing “ctrl + “+””
In the createScene(root) function, I create the scene with root and tissue nodetissue = root.addChild('tissue') tissue.addObject('MechanicalObject', name = tissueDofs, src=@tissueMeshLoader) root.addObject( KeyPressedController(name=tissueController )
In the trigger function, I define
class KeyPressedController(Sofa.Core.Controller): def __init__(self, *args, **kwargs): Sofa.Core.Controller.__init__(self, *args, **kwargs) self.iteration = 0 def onKeypressEvent(self, event): if event['key']== "+": root_now = self.getContext() tissue_now = root_now.tissue.tissueDofs with tissue_now.position.writeableArray() as wa: print(wa[0])
However, after I start the simulation and press “ctrl + “+””, I only received
[INFO] [SofaViewer] QtViewer::keyPressEvent, CONTROL pressed
but no position data was printed out. Could you help point out what might be the reason?In addition, is there other way to trigger an event than pressing a key? For example, in a tissue cutting simulation in which only when the distance between scalpel and tissue surface is closer enough could a Topology modification event be triggered.
Cheers
3 January 2022 at 17:35 #21135EricBlockedHi Hugo,
Thanks for your reply and suggestion.
I’ve followed the example of “python controller” as https://github.com/sofa-framework/SofaPython3/blob/master/examples/liver-scriptcontroller.py, but still fail to stream position data when I trigger the event.
My code is attached as below, aiming to print out the position of the first tissue index on pressing “ctrl + “+””
In the createScene(root) function, I create the scene with root and tissue nodetissue = root.addChild('tissue') tissue.addObject('MechanicalObject', name = "tissueDofs", src="@tissueMeshLoader") root.addObject( KeyPressedController(name="tissueController") )
In the trigger function, I define
class KeyPressedController(Sofa.Core.Controller): def __init__(self, *args, **kwargs): Sofa.Core.Controller.__init__(self, *args, **kwargs) self.iteration = 0 def onKeypressEvent(self, event): if event['key']=="+": root_now = self.getContext() tissue_now = root_now.tissue.tissueDofs with tissue_now.position.writeableArray() as wa: print(wa[0])
However, after I start the simulation and press “ctrl + “+””, I only received
[INFO] [SofaViewer] QtViewer::keyPressEvent, CONTROL pressed
but no position data was printed out. Could you help point out what might be the reason?In addition, is there other way to trigger an event than pressing a key? For example, in a tissue cutting simulation in which only when the distance between scalpel and tissue surface is closer enough could a Topology modification event be triggered.
Cheers
10 January 2022 at 16:03 #21340HugoKeymasterHi @ericcui
I close this topic since the question got a reply there: https://www.sofa-framework.org/community/forum/topic/access-object-data-on-the-fly-sofapython3/
Best
Hugo
-
AuthorPosts
- You must be logged in to reply to this topic.