-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathelasticbody.py
More file actions
75 lines (64 loc) · 2.86 KB
/
elasticbody.py
File metadata and controls
75 lines (64 loc) · 2.86 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
# -*- coding: utf-8 -*-
"""
Step 3: Move the content of the ElasticBody in a separated file for reusability
"""
import os
import pathlib
import sys
import Sofa
sys.path.insert(0, str(pathlib.Path(__file__).parent.absolute())+"/../")
sys.path.insert(0, str(pathlib.Path(__file__).parent.absolute()))
mesh_path = os.path.dirname(os.path.abspath(__file__))+'/mesh/'
def ElasticBody(name="ElasticBody", rotation=[0, 0, 0], translation=[0, 0, 0], color=[1.0, 1.0, 1.0, 1.0]):
# To simulate an elastic object, we need:
# - a deformation law (here linear elasticity)
# - a solving method (here FEM)
# - as we are using FEM we need a space discretization (here tetrahedron)
self = Sofa.Core.Node(name)
mechanicalmodel = self.addChild("MechanicalModel")
mechanicalmodel.addObject('GIDMeshLoader',
name='loader',
rotation=rotation,
translation=translation,
filename=mesh_path+'tripod_low.gidmsh')
mechanicalmodel.addObject('MeshTopology',
src='@loader',
name='container')
mechanicalmodel.addObject('MechanicalObject',
name='dofs',
position=mechanicalmodel.loader.position.getLinkPath(),
showObject=False,
showObjectScale=5.0)
mechanicalmodel.addObject('UniformMass',
name="mass",
totalMass=0.032)
# ForceField components
mechanicalmodel.addObject('TetrahedronFEMForceField',
name="linearElasticBehavior",
youngModulus=250,
poissonRatio=0.45)
# Visual model
visualmodel = Sofa.Core.Node("VisualModel")
# Specific loader for the visual model
visualmodel.addObject('MeshSTLLoader',
name='loader',
filename=mesh_path+'tripod_mid.stl',
rotation=rotation,
translation=translation)
visualmodel.addObject('OglModel',
src=visualmodel.loader.getLinkPath(),
name='renderer',
color=color)
self.addChild(visualmodel)
visualmodel.addObject('BarycentricMapping',
input=mechanicalmodel.dofs.getLinkPath(),
output=visualmodel.renderer.getLinkPath())
return self
def createScene(rootNode):
from stlib3.scene import Scene
scene = Scene(rootNode, gravity=[0.0, -9810, 0.0],
iterative=False)
scene.addMainHeader()
scene.addObject('DefaultAnimationLoop')
scene.addObject('DefaultVisualManagerLoop')
scene.Simulation.addChild(ElasticBody())