- This topic has 1 reply, 2 voices, and was last updated 2 years, 10 months ago by .
Viewing 2 posts - 1 through 2 (of 2 total)
Viewing 2 posts - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.
Home › Forum › SofaPython3 › Using SofaPython3 › Access object data on the fly, sofaPython3
Tagged: Linux_ubuntu, Programming with SOFA, SOFA_2106
Dear all,
I want to propose a question of how to access/modify object data on the fly, for example index positions.
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 node
tissue = 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
Hi @ericcui
My apologies for this late reply.
Have you tried to first click in the view of SOFA? To properly listen and take into account the events like keyboard interactions, we must first click in the view of the runSofa application. Let me know if this helps.
Else, everything looks correct to me. No other errors in the command prompt?
Best wishes,
Hugo
WARNING
The forum has been moved to GitHub Discussions.
Old topics and replies are kept here as an archive.