Skip to content

Commit e496cb1

Browse files
authored
Update examples 202101 (#86)
* Add example to load XML file from python environment * Update scenes with pluginization * Missing plugin SofaOpenglVisual in scenes
1 parent 1b675c8 commit e496cb1

8 files changed

Lines changed: 95 additions & 16 deletions

File tree

examples/basic-addGUI.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
def main():
1212
# Make sure to load all SOFA libraries
13+
SofaRuntime.importPlugin("SofaBaseMechanics")
1314
SofaRuntime.importPlugin("SofaOpenglVisual")
1415

1516
#Create the root node
@@ -48,11 +49,9 @@ def createScene(root):
4849
node1 = root.addChild("Node1")
4950
node2 = root.addChild("Node2")
5051

51-
node1.addObject("MechanicalObject")
52-
node1.addObject("OglModel")
52+
node1.addObject("MechanicalObject", template="Rigid3d", position="0 0 0 0 0 0 1", showObject="1")
5353

54-
node2.addObject("MechanicalObject")
55-
node2.addObject("OglModel")
54+
node2.addObject("MechanicalObject", template="Rigid3d", position="1 1 1 0 0 0 1", showObject="1")
5655

5756
return root
5857

examples/basic.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
def main():
77
# Make sure to load all SOFA libraries
8-
SofaRuntime.importPlugin("SofaOpenglVisual")
9-
8+
SofaRuntime.importPlugin("SofaBaseMechanics")
9+
1010
# Call the above function to create the scene graph
1111
root = Sofa.Core.Node("root")
1212
createScene(root)
@@ -29,11 +29,9 @@ def createScene(root):
2929
node1 = root.addChild("Node1")
3030
node2 = root.addChild("Node2")
3131

32-
node1.addObject("MechanicalObject", position="0 0 0")
33-
node1.addObject("OglModel")
32+
node1.addObject("MechanicalObject", template="Rigid3d", position="0 0 0 0 0 0 1", showObject="1")
3433

35-
node2.addObject("MechanicalObject", position="1 1 1")
36-
node2.addObject("OglModel")
34+
node2.addObject("MechanicalObject", template="Rigid3d", position="1 1 1 0 0 0 1", showObject="1")
3735

3836
return root
3937

examples/emptyController.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def createScene(root):
9494

9595

9696
def main():
97+
SofaRuntime.importPlugin("SofaOpenglVisual")
9798
root=Sofa.Core.Node("root")
9899
createScene(root)
99100

examples/emptyDataEngine.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def createScene(root):
3030

3131

3232
def main():
33+
SofaRuntime.importPlugin("SofaOpenglVisual")
3334
root=Sofa.Core.Node("root")
3435
createScene(root)
3536
Sofa.Simulation.init(root)

examples/emptyForceField.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Sofa
2+
import SofaRuntime
23
import Sofa.Gui
34
from Sofa.Helper import msg_info
45
import numpy as np
@@ -52,6 +53,9 @@ def createScene(root):
5253

5354

5455
def main():
56+
SofaRuntime.importPlugin("SofaOpenglVisual")
57+
SofaRuntime.importPlugin("SofaImplicitOdeSolver")
58+
5559
root=Sofa.Core.Node("root")
5660
createScene(root)
5761
Sofa.Simulation.init(root)

examples/example-forcefield.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
"""Implementation of a RestShapeForceField in python"""
22
# coding: utf8
33
import Sofa
4+
import SofaRuntime
5+
import Sofa.Gui
46
import numpy as np
57

68
class RestShapeForceField(Sofa.Core.ForceField):
79
"""Implementation of a RestShapeForceField in python"""
810
def __init__(self, ks=1.0, kd=1.0, *args, **kwargs):
911
Sofa.Core.ForceField.__init__(self, *args, **kwargs)
1012
self.addData("ks", type="float", value=ks, help="The stiffness spring", group="Spring's Properties")
11-
self.addData("kd", type="float", value=kd, help="The stiffness spring", group="Spring's Properties")
13+
self.addData("kd", type="float", value=kd, help="The damping spring", group="Spring's Properties")
1214

1315
def init(self):
14-
self.initpos = self.mstate.position.array().copy()
16+
mstate = self.getContext().mechanical
17+
self.initpos = mstate.position.array().copy()
1518
self.k = np.zeros((1,1))
1619
self.f = []
1720
self.d = 0.5
1821

1922
def addForce(self, m, out_force, pos, vel):
20-
out_force.value += ( (self.initpos-pos.value) * self.ks.value )
23+
with out_force.writeableArray() as wa:
24+
wa[:] += ( (self.initpos-pos.value) * self.ks.value )
2125

2226
def addDForce(self, df, dx, params):
2327
pass
@@ -40,8 +44,28 @@ def createScene(node):
4044
c.showColor = [1.0,0.0,0.0,1.0]
4145
c.drawMode = 1
4246

43-
o.addObject("UniformMass", name="mass", vertexMass=0.1)
44-
o.addObject( RestShapeForceField(name="CPPObject", kd=2.0))
45-
47+
o.addObject("UniformMass", name="mass", totalMass=[0.1])
48+
o.addObject( RestShapeForceField(name="CPPObject", ks=2.0, kd=0.1))
49+
4650
return node
4751

52+
def main():
53+
SofaRuntime.importPlugin("SofaOpenglVisual")
54+
SofaRuntime.importPlugin("SofaBaseMechanics")
55+
SofaRuntime.importPlugin("SofaImplicitOdeSolver")
56+
57+
root=Sofa.Core.Node("root")
58+
createScene(root)
59+
Sofa.Simulation.init(root)
60+
61+
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
62+
Sofa.Gui.GUIManager.createGUI(root, __file__)
63+
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
64+
Sofa.Gui.GUIManager.MainLoop(root)
65+
Sofa.Gui.GUIManager.closeGUI()
66+
67+
print("End of simulation.")
68+
69+
70+
if __name__ == '__main__':
71+
main()

examples/liver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
def main():
1212
SofaRuntime.importPlugin("SofaOpenglVisual")
13+
SofaRuntime.importPlugin("SofaImplicitOdeSolver")
1314

1415
root = Sofa.Core.Node("root")
1516
createScene(root)

examples/loadXMLfromPython.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import Sofa
2+
import Sofa.Gui
3+
import SofaRuntime
4+
import tempfile
5+
import os
6+
7+
8+
9+
def main():
10+
# Make sure to load all SOFA libraries
11+
SofaRuntime.importPlugin("SofaOpenglVisual")
12+
13+
root = Sofa.Core.Node("root")
14+
15+
# Call the above function to create the scene graph
16+
scene="""
17+
<Node name="rootNode" dt="0.005" gravity="0 0 0">
18+
19+
<DefaultVisualManagerLoop/>
20+
<DefaultAnimationLoop/>
21+
22+
<Node name="child1">
23+
<MechanicalObject template="Rigid3d" position="0 0 0 0 0 0 1" showObject="1"/>
24+
</Node>
25+
26+
<Node name="child2">
27+
<MechanicalObject template="Rigid3d" position="1 1 1 0 0 0 1" showObject="1"/>
28+
</Node>
29+
</Node>
30+
"""
31+
tf = tempfile.NamedTemporaryFile(mode="w+t", suffix=".scn", delete=False)
32+
tf.write(scene)
33+
tf.flush()
34+
node = Sofa.Simulation.load(tf.name)
35+
36+
# Find out the supported GUIs
37+
print ("Supported GUIs are: " + Sofa.Gui.GUIManager.ListSupportedGUI(","))
38+
# Launch the GUI (qt or qglviewer)
39+
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
40+
Sofa.Gui.GUIManager.createGUI(node, __file__)
41+
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
42+
# Initialization of the scene will be done here
43+
Sofa.Gui.GUIManager.MainLoop(node)
44+
Sofa.Gui.GUIManager.closeGUI()
45+
print("GUI was closed")
46+
print("Simulation is done.")
47+
os.remove(tf.name)
48+
49+
# Function used only if this script is called from a python environment
50+
if __name__ == '__main__':
51+
main()

0 commit comments

Comments
 (0)