Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions examples/basic-addGUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

def main():
# Make sure to load all SOFA libraries
SofaRuntime.importPlugin("SofaBaseMechanics")
SofaRuntime.importPlugin("SofaOpenglVisual")

#Create the root node
Expand Down Expand Up @@ -48,11 +49,9 @@ def createScene(root):
node1 = root.addChild("Node1")
node2 = root.addChild("Node2")

node1.addObject("MechanicalObject")
node1.addObject("OglModel")
node1.addObject("MechanicalObject", template="Rigid3d", position="0 0 0 0 0 0 1", showObject="1")

node2.addObject("MechanicalObject")
node2.addObject("OglModel")
node2.addObject("MechanicalObject", template="Rigid3d", position="1 1 1 0 0 0 1", showObject="1")

return root

Expand Down
10 changes: 4 additions & 6 deletions examples/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

def main():
# Make sure to load all SOFA libraries
SofaRuntime.importPlugin("SofaOpenglVisual")
SofaRuntime.importPlugin("SofaBaseMechanics")

# Call the above function to create the scene graph
root = Sofa.Core.Node("root")
createScene(root)
Expand All @@ -29,11 +29,9 @@ def createScene(root):
node1 = root.addChild("Node1")
node2 = root.addChild("Node2")

node1.addObject("MechanicalObject", position="0 0 0")
node1.addObject("OglModel")
node1.addObject("MechanicalObject", template="Rigid3d", position="0 0 0 0 0 0 1", showObject="1")

node2.addObject("MechanicalObject", position="1 1 1")
node2.addObject("OglModel")
node2.addObject("MechanicalObject", template="Rigid3d", position="1 1 1 0 0 0 1", showObject="1")

return root

Expand Down
1 change: 1 addition & 0 deletions examples/emptyController.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def createScene(root):


def main():
SofaRuntime.importPlugin("SofaOpenglVisual")
root=Sofa.Core.Node("root")
createScene(root)

Expand Down
1 change: 1 addition & 0 deletions examples/emptyDataEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def createScene(root):


def main():
SofaRuntime.importPlugin("SofaOpenglVisual")
root=Sofa.Core.Node("root")
createScene(root)
Sofa.Simulation.init(root)
Expand Down
4 changes: 4 additions & 0 deletions examples/emptyForceField.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Sofa
import SofaRuntime
import Sofa.Gui
from Sofa.Helper import msg_info
import numpy as np
Expand Down Expand Up @@ -52,6 +53,9 @@ def createScene(root):


def main():
SofaRuntime.importPlugin("SofaOpenglVisual")
SofaRuntime.importPlugin("SofaImplicitOdeSolver")

root=Sofa.Core.Node("root")
createScene(root)
Sofa.Simulation.init(root)
Expand Down
36 changes: 30 additions & 6 deletions examples/example-forcefield.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
"""Implementation of a RestShapeForceField in python"""
# coding: utf8
import Sofa
import SofaRuntime
import Sofa.Gui
import numpy as np

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

def init(self):
self.initpos = self.mstate.position.array().copy()
mstate = self.getContext().mechanical
self.initpos = mstate.position.array().copy()
self.k = np.zeros((1,1))
self.f = []
self.d = 0.5

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

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

o.addObject("UniformMass", name="mass", vertexMass=0.1)
o.addObject( RestShapeForceField(name="CPPObject", kd=2.0))
o.addObject("UniformMass", name="mass", totalMass=[0.1])
o.addObject( RestShapeForceField(name="CPPObject", ks=2.0, kd=0.1))

return node

def main():
SofaRuntime.importPlugin("SofaOpenglVisual")
SofaRuntime.importPlugin("SofaBaseMechanics")
SofaRuntime.importPlugin("SofaImplicitOdeSolver")

root=Sofa.Core.Node("root")
createScene(root)
Sofa.Simulation.init(root)

Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
Sofa.Gui.GUIManager.createGUI(root, __file__)
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
Sofa.Gui.GUIManager.MainLoop(root)
Sofa.Gui.GUIManager.closeGUI()

print("End of simulation.")


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions examples/liver.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

def main():
SofaRuntime.importPlugin("SofaOpenglVisual")
SofaRuntime.importPlugin("SofaImplicitOdeSolver")

root = Sofa.Core.Node("root")
createScene(root)
Expand Down
51 changes: 51 additions & 0 deletions examples/loadXMLfromPython.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Sofa
import Sofa.Gui
import SofaRuntime
import tempfile
import os



def main():
# Make sure to load all SOFA libraries
SofaRuntime.importPlugin("SofaOpenglVisual")

root = Sofa.Core.Node("root")

# Call the above function to create the scene graph
scene="""
<Node name="rootNode" dt="0.005" gravity="0 0 0">

<DefaultVisualManagerLoop/>
<DefaultAnimationLoop/>

<Node name="child1">
<MechanicalObject template="Rigid3d" position="0 0 0 0 0 0 1" showObject="1"/>
</Node>

<Node name="child2">
<MechanicalObject template="Rigid3d" position="1 1 1 0 0 0 1" showObject="1"/>
</Node>
</Node>
"""
tf = tempfile.NamedTemporaryFile(mode="w+t", suffix=".scn", delete=False)
tf.write(scene)
tf.flush()
node = Sofa.Simulation.load(tf.name)

# Find out the supported GUIs
print ("Supported GUIs are: " + Sofa.Gui.GUIManager.ListSupportedGUI(","))
# Launch the GUI (qt or qglviewer)
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
Sofa.Gui.GUIManager.createGUI(node, __file__)
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
# Initialization of the scene will be done here
Sofa.Gui.GUIManager.MainLoop(node)
Sofa.Gui.GUIManager.closeGUI()
print("GUI was closed")
print("Simulation is done.")
os.remove(tf.name)

# Function used only if this script is called from a python environment
if __name__ == '__main__':
main()