Home › Forum › SOFA › Using SOFA › [SOLVED] Using Python to move some points or parts of an object
Tagged: 64_bits, Linux_ubuntu, Plugin_other, SOFA_1806, SofaPython interactive
- This topic has 4 replies, 2 voices, and was last updated 5 years, 5 months ago by baymin.
-
AuthorPosts
-
27 May 2019 at 07:01 #13529bayminBlocked
Hi,
I want to use python to move some points of an object, which function should I use?
I have tried applyTranslation(x,y,z) of Sofa.BaseMechanicalState but it did not work. I have also tried setting value of Sofa.Data but it did not work too.
If there are something wrong in the setting?
def createGraph(self,rootNode): gravity = '0 0 0' # rootNode rootNode.createObject('VisualStyle', displayFlags='showForceFields') rootNode.createObject('DefaultPipeline', name='CollisionPipeline', verbose='0') rootNode.createObject('BruteForceDetection', name='N2') rootNode.createObject('DefaultContactManager', name='collision response', response='default') rootNode.createObject('DiscreteIntersection') # rootNode/LiverFEM LiverFEM = rootNode.createChild('LiverFEM') self.LiverFEM = LiverFEM LiverFEM.gravity = gravity LiverFEM.createObject('EulerImplicitSolver', printLog='0', rayleighStiffness='0.1', name='cg_odesolver', rayleighMass='0.1') LiverFEM.createObject('CGLinearSolver', threshold='1e-09', tolerance='1e-09', name='linear solver', iterations='25') LiverFEM.createObject('MeshTopology', name='mesh', fileTopology='mesh/liver.msh') LiverFEM.createObject('MechanicalObject', name='dofs', template='Vec3d') LiverFEM.createObject('TetrahedronFEMForceField', youngModulus='50', name='FEM', poissonRatio='0.45', template='Vec3d') LiverFEM.createObject('UniformMass', name='mass', totalmass='1') LiverFEM.createObject('FixedConstraint', indices='3 39 64', name='FixedConstraint') # rootNode/LiverFEM/Visu Visu = LiverFEM.createChild('Visu') self.Visu = Visu Visu.gravity = gravity Visu.createObject('OglModel', name='VisualModel', fileMesh='mesh/liver-smooth.obj') Visu.createObject('BarycentricMapping', input='@../dofs', name='visual mapping', output='@VisualModel') # rootNode/LiverFEM/Surf Surf = LiverFEM.createChild('Surf') self.Surf = Surf Surf.gravity = gravity Surf.createObject('SphereLoader', name='SphereLoader', filename='mesh/liver.sph') Surf.createObject('MechanicalObject', position='@[-1].position', name='mappedMS') Surf.createObject('SphereModel', listRadius='@[-2].listRadius', name='CollisionModel') Surf.createObject('BarycentricMapping', input='@../dofs', name='sphere mapping', output='@mappedMS')
best,
Baymin
27 May 2019 at 18:57 #13535HugoKeymasterHi @baymin
There is actually several ways of enforcing displacement:
– You could first use the component LinearMovementConstraint (see example scene in examples/Components/constraint/LinearMovementConstraint.scn): with this component you can impose a sequential linear movement between target positions
– another option is to create another MechanicalObject with your moving target positions and define springs between these nodes and your object
There is maybe other ways that I am forgetting, but the above-mentioned ones can be easily tested. I hope this helps.
Hugo
28 May 2019 at 15:38 #13547bayminBlockedDear Hugo,
Thank you very much for your help.
Both using ‘PartialLinearMovementConstraint’ and adding springs between nodes are work well.Best,
Baymin
29 May 2019 at 07:03 #13552bayminBlockedHi, @Hugo
How can I change the position of some points of an object in real-time.
I tried to use ‘PartialLinearMovementConstraint’ and set the properties: ‘movements’ and ‘keyTimes’:self.LiverFEM.getObject('PartialLinearMovementConstraint').findData('keyTimes').value = [[0.0], [5.0], [10.0]] if self.count == t1: self.LiverFEM.getObject('PartialLinearMovementConstraint').findData('movements').value = [[0, 0, 0], [0.1, -0.1, -0.1], [0.1, -0.1, -0.1]] elif self.count == t2: self.LiverFEM.getObject('PartialLinearMovementConstraint').findData('movements').value = [[0, 0, 0], [0.1, -0.1, -0.1], [-0.1, 0.1, 0.1]]
but the object in simulation changed suddenly and did not move smoothly as set it in the createGraph function/method:
LiverFEM.createObject('PartialLinearMovementConstraint', indices='100 105 110', keyTimes='0 5 10', template='Vec3d', movements='0 0 0 0.1 -0.1 -0.1 -0.1 0.1 0.1')
So how can I change the position of one point of an object such as one blue point on the surface in each AnimationStep?
https://drive.google.com/file/d/13wC1e5j2eSSdgBaMD8ywH4tSdkJlbSSY/view?usp=sharingBest,
Baymin
31 May 2019 at 05:23 #13558bayminBlockedHi,
I have moved some points of an object in real-time success, by adding the below code into the function ‘onBeginAnimationStep’:
cT = self.LiverFEM.getTime() self.LiverFEM.getObject('PartialLinearMovementConstraint').findData('keyTimes').value = [[cT], [cT + 100]] self.LiverFEM.getObject('PartialLinearMovementConstraint').findData('movements').value = [0.5*math.sin(cT), 0.5*math.cos(cT), 0]
But what is the meaning of the keyTimes? If I set it too small, the simulation will illness. On the other hand, if I set it biger, it works well, but 1s, 10s and 100s has nothing different.
Best,
Baymin
-
AuthorPosts
- You must be logged in to reply to this topic.