-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathliver-scriptcontroller.py
More file actions
117 lines (93 loc) · 4.96 KB
/
liver-scriptcontroller.py
File metadata and controls
117 lines (93 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Required import for python
import Sofa
# Choose in your script to activate or not the GUI
USE_GUI = True
def main():
import SofaRuntime
import Sofa.Gui
root = Sofa.Core.Node("root")
createScene(root)
Sofa.Simulation.initRoot(root)
if not USE_GUI:
for iteration in range(10):
Sofa.Simulation.animate(root, root.dt.value)
else:
SofaRuntime.importPlugin("SofaImGui")
Sofa.Gui.GUIManager.Init("myscene", "imgui")
Sofa.Gui.GUIManager.createGUI(root, __file__)
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
Sofa.Gui.GUIManager.MainLoop(root)
Sofa.Gui.GUIManager.closeGUI()
def createScene(root):
root.gravity=[0, -9.81, 0]
root.dt=0.02
root.addObject("RequiredPlugin", pluginName=[ 'Sofa.Component.Collision.Detection.Algorithm',
'Sofa.Component.Collision.Detection.Intersection',
'Sofa.Component.Collision.Geometry',
'Sofa.Component.Collision.Response.Contact',
'Sofa.Component.Constraint.Projective',
'Sofa.Component.IO.Mesh',
'Sofa.Component.LinearSolver.Iterative',
'Sofa.Component.Mapping.Linear',
'Sofa.Component.Mass',
'Sofa.Component.ODESolver.Backward',
'Sofa.Component.SolidMechanics.FEM.Elastic',
'Sofa.Component.StateContainer',
'Sofa.Component.MechanicalLoad',
'Sofa.Component.Topology.Container.Dynamic',
'Sofa.Component.Visual',
'Sofa.GL.Component.Rendering3D'
])
root.addObject('DefaultAnimationLoop', computeBoundingBox=False)
root.addObject('VisualStyle', displayFlags="showCollisionModels hideVisualModels showForceFields")
root.addObject('CollisionPipeline', name="CollisionPipeline")
root.addObject('BruteForceBroadPhase', name="BroadPhase")
root.addObject('BVHNarrowPhase', name="NarrowPhase")
root.addObject('CollisionResponse', name="CollisionResponse", response="PenalityContactForceField")
root.addObject('DiscreteIntersection')
root.addObject('MeshOBJLoader', name="LiverSurface", filename="mesh/liver-smooth.obj")
liver = root.addChild('Liver')
liver.addObject('EulerImplicitSolver', name="cg_odesolver", rayleighStiffness=0.1, rayleighMass=0.1)
liver.addObject('CGLinearSolver', name="linear_solver", iterations=25, tolerance=1e-09, threshold=1e-09)
liver.addObject('MeshGmshLoader', name="meshLoader", filename="mesh/liver.msh")
liver.addObject('TetrahedronSetTopologyContainer', name="topo", src="@meshLoader")
liver.addObject('MechanicalObject', name="dofs", src="@meshLoader")
liver.addObject('TetrahedronSetGeometryAlgorithms', template="Vec3d", name="GeomAlgo")
liver.addObject('DiagonalMass', name="Mass", massDensity=1.0)
liver.addObject('TetrahedralCorotationalFEMForceField', template="Vec3d", name="FEM", method="large", poissonRatio=0.3, youngModulus=3000, computeGlobalMatrix=False)
liver.addObject('FixedProjectiveConstraint', name="FixedConstraint", indices="3 39 64")
visu = liver.addChild('Visu')
visu.addObject('OglModel', name="VisualModel", src="@../../LiverSurface")
visu.addObject('BarycentricMapping', name="VisualMapping", input="@../dofs", output="@VisualModel")
surf = liver.addChild('Surf')
surf.addObject('SphereLoader', name="sphereLoader", filename="mesh/liver.sph")
surf.addObject('MechanicalObject', name="spheres", position="@sphereLoader.position")
surf.addObject('SphereCollisionModel', name="CollisionModel", listRadius="@sphereLoader.listRadius")
surf.addObject('BarycentricMapping', name="CollisionMapping", input="@../dofs", output="@spheres")
root.addObject(KeyPressedController(name = "SphereCreator"))
return root
class KeyPressedController(Sofa.Core.Controller):
""" This controller monitors new sphere objects.
Press ctrl and the L key to make spheres falling!
"""
def __init__(self, *args, **kwargs):
Sofa.Core.Controller.__init__(self, *args, **kwargs)
self.iteration = 0
def onKeypressedEvent(self, event):
# Press L key triggers the creation of new objects in the scene
if event['key']=='L':
self.createNewSphere()
def createNewSphere(self):
root = self.getContext()
newSphere = root.addChild('FallingSphere-'+str(self.iteration))
newSphere.addObject('EulerImplicitSolver')
newSphere.addObject('CGLinearSolver', threshold='1e-09', tolerance='1e-09', iterations='200')
MO = newSphere.addObject('MechanicalObject', showObject=True, position=[-2, 10+self.iteration, 0, 0, 0, 0, 1], name=f'Particle-{self.iteration}', template='Rigid3d')
Mass = newSphere.addObject('UniformMass', totalMass=1)
Force = newSphere.addObject('ConstantForceField', name="CFF", totalForce=[0, -1, 0, 0, 0, 0] )
Sphere = newSphere.addObject('SphereCollisionModel', name="SCM", radius=1.0 )
newSphere.init()
self.iteration = self.iteration+1
# Function used only if this script is called from a python environment
if __name__ == '__main__':
main()