Home › Forum › SOFA › Programming with SOFA › [SOLVED] Applying force to an arbitrary node
Tagged: 64_bits, Linux_ubuntu, Plugin_SoftRobots, SOFA_other
- This topic has 4 replies, 3 voices, and was last updated 5 years, 7 months ago by Hugo.
-
AuthorPosts
-
10 May 2019 at 06:12 #13469minjBlocked
Hello All!
I would like to apply force to a node and obtain the position of it.
I used pythonController to print the position of node, but I have no idea how to apply force on the mesh.
I guess it is possible because it is possible with mouse input, but I want to make a program that applies force as a function of time.I am using the v19.06.99_custom_Linux_v5.1, which I downloaded from the SoftRobotics workshop page.
Any ideas of how to do it?
I’d be grateful if you could help me.
Thanks a lot!10 May 2019 at 15:28 #13470NouraBlockedHello minj,
You can apply a constant force field to a certain node in your mesh (let say on the node with index 5), you supply the force direction and value (for instance 7 N. in the positive direction of Y axis), I included the “arrowSizeCoef” parameter which is optional just to help visualizing the force direction.
<ConstantForceField indices="5" totalForce="0 7 0" arrowSizeCoef="0.1"/>
Hope this helps,
Noura
10 May 2019 at 16:04 #13471HugoKeymasterHi @minj Hi Noura !
Since I was already preparing some files for you Minj I am posting them.
Find here an example with a deformable liver on which we apply a constant force and this constant force is applied on a random point of the mesh.The scene :
<Node name="root" dt="0.05" gravity="0 -9.81 0"> <!-- Constant force for a deformable --> <VisualStyle displayFlags="showBehaviorModels showForceFields" /> <PythonScriptController name="randomForce" filename="randomForce.py" classname="randomize" listening="1"/> <MeshObjLoader name="VisualLoader" filename="mesh/liver-smooth.obj" /> <MeshGmshLoader name="MecaLoader" filename="mesh/liver.msh" /> <Node name="Liver" > <EulerImplicitSolver name="cg_odesolver" rayleighStiffness="0.1" rayleighMass="0.1" /> <CGLinearSolver name="linear solver" iterations="25" tolerance="1e-09" threshold="1e-09" /> <TetrahedronSetTopologyContainer name="topo" src="@../MecaLoader" /> <MechanicalObject name="dofs" src="@../MecaLoader" /> <TetrahedronSetGeometryAlgorithms template="Vec3d" name="GeomAlgo" /> <DiagonalMass name="computed using mass density" massDensity="1" /> <TetrahedralCorotationalFEMForceField template="Vec3d" name="FEM" method="large" poissonRatio="0.3" youngModulus="3000" computeGlobalMatrix="0" /> <FixedConstraint name="FixedConstraint" indices="3 39 64" /> <ConstantForceField name="CFF" indices="14" forces="0 -10 0" /> <Node name="Visu" tags="Visual" gravity="0 -9.81 0"> <OglModel name="VisualModel" src="@../../VisualLoader" /> <BarycentricMapping name="visual mapping" input="@../dofs" output="@VisualModel" /> </Node> </Node> </Node>
The python script (randomForce.py):
import Sofa import sys import random class randomize(Sofa.PythonScriptController): # optionnally, script can create a graph... def initGraph(self,node): self.nodeLiver = node.getChild('Liver') self.activeRandomize=0 #save a pointer to the ConstantForceField self.RandomForce = self.nodeLiver.getObject('CFF') return 0 def bwdInitGraph(self,node): #count the number of points self.nbPoints = self.nodeLiver.getObject('dofs').findData("size").value print "nbPoints = "+str(self.nbPoints) return 0 def onBeginAnimationStep(self,dt): # if active, randomly change the indice on which the force is applied if self.activeRandomize==1 : newIndice = random.randint(1,self.nbPoints) self.nodeLiver.getObject('CFF').findData("indices").value = newIndice print "Randomizing force on point = "+str(newIndice) return 0 def onKeyPressed(self, c): if c=="L" : if self.activeRandomize==1 : self.activeRandomize=0 print "Randomizing inactive" else: self.activeRandomize=1 print "Randomizing active" return 0;
I hope this helps.
BestHugo
14 May 2019 at 07:34 #13475minjBlockedHi Hugo Hi Noura!
Thanks y’all for your replies.
It helped me so much.Best,
Minji14 May 2019 at 09:45 #13476 -
AuthorPosts
- You must be logged in to reply to this topic.