Home › Forum › SOFA › Using SOFA › [SOLVED] Moving a model with key controls
- This topic has 5 replies, 3 voices, and was last updated 5 years, 8 months ago by
Damien Marchal.
-
AuthorPosts
-
13 December 2018 at 18:46 #12595
Anonymous
InactiveI have a model (surgical scissors) which is made up of multiple meshes (base, connector, left & right scissors). I want to move the model as a whole at once using the keyboard, but I am not sure how to achieve this. I am trying to do it through python for now, by moving each mesh independently. Is there a better way of doing this?
17 December 2018 at 14:38 #12609Hugo
KeymasterHi Fayad,
What you can actually do is to create yourself an animation : using WriteState. WriteState saves all positions into a file. Once you register the animation, you could replay it using ReadState.
To create rotation/translation/scaling of an object, you can always use the TransformEngine.
Let me know if it helps.
Hugo
19 December 2018 at 11:19 #12620Anonymous
InactiveCan I get an example of a scene with a Rigid model and using TransformEngine.
Thanks,
Fayad19 December 2018 at 15:46 #12623Hugo
KeymasterYes, take a look in examples/Components/engine/TransformEngine.scn
Hugo
19 December 2018 at 22:21 #12624Anonymous
InactiveThanks Hugo, I know about this example but it uses vec3d for the template, and I want to use a rigid one, when I do that it does not seem to work. So can you show me how I can do the TransformEngine with a rigid object that has a collision model.
Thanks,
Fayad19 December 2018 at 23:14 #12625Damien Marchal
BlockedHi Fayad,
I tried in a small test to see how I would do a very simplified logic for a cissor.
Basically it is a “cissor” reference frame connected to two “child” frames to hold the local frames for each part of the cissor. The connection between the parent and child is done using a RigidRigidMapping. To any of these frame you can attach visual or collision models.Then I made a python controller which actually connect the keypressed to actual transform of either the reference frame or the two sides when we open/close it.
It is quickly done and I have no idea if it is bullet proof… but here is the result, hoping you will find it useful.
# coding: utf-8 import Sofa def translate(x, t): p = x[0:3] q = x[3:] return [p[0]+t[0], p[1]+t[1], p[2]+t[2]]+q class CissorController(Sofa.PythonScriptController): '''Controller that handle open/close and movement of a cissor''' def __init__(self, node, cissor): self.cissor = cissor def onKeyPressed(self, c): o = None p = None if c == '+': o=0.1 elif c == '-': o=-0.1 elif c=='4': p = 0.1 elif c=='6': p = -0.1 if o is not None: # Open/Close the cissor. q = self.cissor.right.transform.rest_position[0] self.cissor.right.transform.rest_position = translate(q,[o,0.0,0.0]) q = self.cissor.left.transform.rest_position[0] self.cissor.left.transform.rest_position = translate(q,[-o,0.0,0.0]) if p is not None: ## Move along X the cissor q = self.cissor.transform.position[0] self.cissor.transform.position = translate(q,[p,0.0,0.0]) class MyCissor(): '''A Sofa Prefab for a cissor.''' def __init__(self, node, name="MyCissor"): n = node.createChild(name) ## The reference transform to manipulate the whole cissor m = n.createObject("MechanicalObject", template="Rigid3", name="transform") ## The right side of the cissor with its own position/orientation. r = n.createChild("right") r.createObject("MechanicalObject", name="transform", template="Rigid3", position=[1.0,0.5,0.0,0,0,0,1]) r.createObject("RigidRigidMapping", name="map", initialPoints=r.transform.findData("rest_position").getLinkPath()) ## The left side of the cissor with its own position/orientation. l = n.createChild("left") l.createObject("MechanicalObject", name="transform", template="Rigid3", position=[-1.0,0.5,0.0,0,0,0,1]) l.createObject("RigidRigidMapping", name="map", initialPoints=l.transform.findData("rest_position").getLinkPath()) self.node = n def createScene(root): root.createObject("EulerImplicit") root.createObject("CGLinearSolver") c = MyCissor(root).node CissorController(c,c) c.transform.showObject=True c.right.transform.showObject=True c.left.transform.showObject=True
-
AuthorPosts
- You must be logged in to reply to this topic.