From b3637ada0588fbcb97821287bc34253296ce9871 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Sat, 16 Jan 2021 09:21:17 +0100 Subject: [PATCH 1/3] Add example to load XML file from python environment --- examples/loadXMLfromPython.py | 53 +++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 examples/loadXMLfromPython.py diff --git a/examples/loadXMLfromPython.py b/examples/loadXMLfromPython.py new file mode 100644 index 00000000..4938c2e6 --- /dev/null +++ b/examples/loadXMLfromPython.py @@ -0,0 +1,53 @@ +import Sofa +import Sofa.Gui +import SofaRuntime +import tempfile +import os + + + +def main(): + # Make sure to load all SOFA libraries + # SofaRuntime.importPlugin("SofaBaseMechanics") + + root = Sofa.Core.Node("root") + + # Call the above function to create the scene graph + scene=""" + + + + + + + + + + + + + + """ + tf = tempfile.NamedTemporaryFile(mode="w+t", suffix=".scn", delete=False) + tf.write(scene) + tf.flush() + node = Sofa.Simulation.load(tf.name) + + # Once defined, initialization of the scene graph + + # 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() \ No newline at end of file From 082e226a33800811a06e147e322fdf5146137c75 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Sat, 16 Jan 2021 09:21:48 +0100 Subject: [PATCH 2/3] Update scenes with pluginization --- examples/basic-addGUI.py | 8 +++----- examples/basic.py | 10 ++++------ examples/emptyForceField.py | 3 +++ examples/example-forcefield.py | 35 ++++++++++++++++++++++++++++------ examples/liver.py | 1 + 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/examples/basic-addGUI.py b/examples/basic-addGUI.py index b7df9bb5..57ae2139 100644 --- a/examples/basic-addGUI.py +++ b/examples/basic-addGUI.py @@ -10,7 +10,7 @@ def main(): # Make sure to load all SOFA libraries - SofaRuntime.importPlugin("SofaOpenglVisual") + SofaRuntime.importPlugin("SofaBaseMechanics") #Create the root node root = Sofa.Core.Node("root") @@ -48,11 +48,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 diff --git a/examples/basic.py b/examples/basic.py index f27ad13e..63fc9192 100644 --- a/examples/basic.py +++ b/examples/basic.py @@ -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) @@ -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 diff --git a/examples/emptyForceField.py b/examples/emptyForceField.py index f033fcc2..c53ba1b3 100644 --- a/examples/emptyForceField.py +++ b/examples/emptyForceField.py @@ -1,4 +1,5 @@ import Sofa +import SofaRuntime import Sofa.Gui from Sofa.Helper import msg_info import numpy as np @@ -52,6 +53,8 @@ def createScene(root): def main(): + SofaRuntime.importPlugin("SofaImplicitOdeSolver") + root=Sofa.Core.Node("root") createScene(root) Sofa.Simulation.init(root) diff --git a/examples/example-forcefield.py b/examples/example-forcefield.py index 381f2bac..0ed4d89a 100644 --- a/examples/example-forcefield.py +++ b/examples/example-forcefield.py @@ -1,6 +1,8 @@ """Implementation of a RestShapeForceField in python""" # coding: utf8 import Sofa +import SofaRuntime +import Sofa.Gui import numpy as np class RestShapeForceField(Sofa.Core.ForceField): @@ -8,16 +10,18 @@ class RestShapeForceField(Sofa.Core.ForceField): 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 @@ -40,8 +44,27 @@ 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("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() diff --git a/examples/liver.py b/examples/liver.py index 97ac43fe..38cb45cc 100644 --- a/examples/liver.py +++ b/examples/liver.py @@ -10,6 +10,7 @@ def main(): SofaRuntime.importPlugin("SofaOpenglVisual") + SofaRuntime.importPlugin("SofaImplicitOdeSolver") root = Sofa.Core.Node("root") createScene(root) From 9c481493959935f1ef958fd938923bb3c35c121b Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Sat, 16 Jan 2021 11:28:08 +0100 Subject: [PATCH 3/3] Missing plugin SofaOpenglVisual in scenes --- examples/basic-addGUI.py | 1 + examples/emptyController.py | 1 + examples/emptyDataEngine.py | 1 + examples/emptyForceField.py | 1 + examples/example-forcefield.py | 1 + examples/loadXMLfromPython.py | 6 ++---- 6 files changed, 7 insertions(+), 4 deletions(-) diff --git a/examples/basic-addGUI.py b/examples/basic-addGUI.py index 57ae2139..d53ac9be 100644 --- a/examples/basic-addGUI.py +++ b/examples/basic-addGUI.py @@ -11,6 +11,7 @@ def main(): # Make sure to load all SOFA libraries SofaRuntime.importPlugin("SofaBaseMechanics") + SofaRuntime.importPlugin("SofaOpenglVisual") #Create the root node root = Sofa.Core.Node("root") diff --git a/examples/emptyController.py b/examples/emptyController.py index e3dcd1c4..488edc2c 100644 --- a/examples/emptyController.py +++ b/examples/emptyController.py @@ -94,6 +94,7 @@ def createScene(root): def main(): + SofaRuntime.importPlugin("SofaOpenglVisual") root=Sofa.Core.Node("root") createScene(root) diff --git a/examples/emptyDataEngine.py b/examples/emptyDataEngine.py index 5b43f0fe..bc492e90 100644 --- a/examples/emptyDataEngine.py +++ b/examples/emptyDataEngine.py @@ -30,6 +30,7 @@ def createScene(root): def main(): + SofaRuntime.importPlugin("SofaOpenglVisual") root=Sofa.Core.Node("root") createScene(root) Sofa.Simulation.init(root) diff --git a/examples/emptyForceField.py b/examples/emptyForceField.py index c53ba1b3..7174041f 100644 --- a/examples/emptyForceField.py +++ b/examples/emptyForceField.py @@ -53,6 +53,7 @@ def createScene(root): def main(): + SofaRuntime.importPlugin("SofaOpenglVisual") SofaRuntime.importPlugin("SofaImplicitOdeSolver") root=Sofa.Core.Node("root") diff --git a/examples/example-forcefield.py b/examples/example-forcefield.py index 0ed4d89a..af953a0e 100644 --- a/examples/example-forcefield.py +++ b/examples/example-forcefield.py @@ -50,6 +50,7 @@ def createScene(node): return node def main(): + SofaRuntime.importPlugin("SofaOpenglVisual") SofaRuntime.importPlugin("SofaBaseMechanics") SofaRuntime.importPlugin("SofaImplicitOdeSolver") diff --git a/examples/loadXMLfromPython.py b/examples/loadXMLfromPython.py index 4938c2e6..5b3ef22f 100644 --- a/examples/loadXMLfromPython.py +++ b/examples/loadXMLfromPython.py @@ -8,8 +8,8 @@ def main(): # Make sure to load all SOFA libraries - # SofaRuntime.importPlugin("SofaBaseMechanics") - + SofaRuntime.importPlugin("SofaOpenglVisual") + root = Sofa.Core.Node("root") # Call the above function to create the scene graph @@ -33,8 +33,6 @@ def main(): tf.flush() node = Sofa.Simulation.load(tf.name) - # Once defined, initialization of the scene graph - # Find out the supported GUIs print ("Supported GUIs are: " + Sofa.Gui.GUIManager.ListSupportedGUI(",")) # Launch the GUI (qt or qglviewer)