Adjust object positions in SOFA at runtime #5143
Unanswered
nikhunziker
asked this question in
Build / Config / Environment
Replies: 1 comment
-
Hi @nikhunziker, I think for Unity related question @epernod is the one that has the knowledge. If python is possible in Unity, then you should use what is called a Controller. """Demonstrate how to change a sofa component variables using a custom controller"""
import Sofa.Core
import math
class MyController(Sofa.Core.Controller):
"""A controlling accepting a mechanical object and changing its state"""
def __init__(self, *args, **kwargs):
Sofa.Core.Controller.__init__(self, *args, **kwargs)
self.target = kwargs.get("target")
self.currenttime = 0
# This method is called automatically at the end of each simulation step.
# Have a look at example the following example for other possible callback method:
# https://github.com/sofa-framework/SofaPython3/blob/master/examples/emptyController.py
def onAnimateEndEvent(self, event):
# Update internal "timing" to make cos/sin happy
self.currenttime += event["dt"]
# Here we change the position of the "target" object.
self.target.position.value = [[math.cos(self.currenttime),math.sin(self.currenttime),0
,0,0,0,1]]
def createScene(root : Sofa.Core.Node):
# Adds the mechanical object with a single 3d frame
root.addObject("MechanicalObject", name = "mo", template="Rigid3", position=[[0,0,0,0,0,0,1]])
# Make the mechanical object visible.
root.mo.showObject.value = True
root.mo.showObjectScale.value = 1.0
# add the controller and pass the mechanical object as target
root.addObject(MyController(target=root.mo)) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi everyone,
I'm working on a project where I aim to manipulate the position of objects during runtime in SOFA inside Unity. The idea is that these changes are also reflected in real-time in Unity while running the simulation and I have a few questions in this regard:
Any advice, examples, or documentation references would be greatly appreciated!
Beta Was this translation helpful? Give feedback.
All reactions