Skip to content

Commit 9d2a9df

Browse files
hugtalbotbakpaul
authored andcommitted
[examples] Update examples using imgui as default (#521)
* [examples] Update examples using imgui as default * Add example to keep the Qt GUI example
1 parent c7c4eb9 commit 9d2a9df

File tree

9 files changed

+90
-27
lines changed

9 files changed

+90
-27
lines changed

examples/basic-addGUI.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
# Required import for python
2-
import Sofa
3-
4-
51
# Choose in your script to activate or not the GUI
62
USE_GUI = True
73

84
def main():
5+
# Required import for python
6+
import Sofa
97
import SofaRuntime
10-
import Sofa.Gui
11-
# Make sure to load all SOFA libraries
128

139
#Create the root node
1410
root = Sofa.Core.Node("root")
@@ -20,12 +16,13 @@ def main():
2016
for iteration in range(10):
2117
Sofa.Simulation.animate(root, root.dt.value)
2218
else:
23-
import SofaQt
19+
import Sofa.Gui
20+
SofaRuntime.importPlugin("SofaImGui")
2421

2522
# Find out the supported GUIs
2623
print ("Supported GUIs are: " + Sofa.Gui.GUIManager.ListSupportedGUI(","))
27-
# Launch the GUI (qt or qglviewer)
28-
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
24+
# Launch the GUI (imgui is now by default, to use Qt please refer to the example "basic-useQtGui.py")
25+
Sofa.Gui.GUIManager.Init("myscene", "imgui")
2926
Sofa.Gui.GUIManager.createGUI(root, __file__)
3027
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
3128
# Initialization of the scene will be done here

examples/basic-useQtGUI.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Choose in your script to activate or not the GUI
2+
USE_GUI = True
3+
4+
def main():
5+
# Required import for python
6+
import Sofa
7+
import SofaRuntime
8+
9+
# Make sure to load all SOFA libraries
10+
11+
#Create the root node
12+
root = Sofa.Core.Node("root")
13+
# Call the below 'createScene' function to create the scene graph
14+
createScene(root)
15+
Sofa.Simulation.initRoot(root)
16+
17+
if not USE_GUI:
18+
for iteration in range(10):
19+
Sofa.Simulation.animate(root, root.dt.value)
20+
else:
21+
import Sofa.Gui
22+
import SofaQt
23+
24+
# Find out the supported GUIs
25+
print ("Supported GUIs are: " + Sofa.Gui.GUIManager.ListSupportedGUI(","))
26+
# Launch the GUI (qt or qglviewer)
27+
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
28+
Sofa.Gui.GUIManager.createGUI(root, __file__)
29+
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
30+
# Initialization of the scene will be done here
31+
Sofa.Gui.GUIManager.MainLoop(root)
32+
Sofa.Gui.GUIManager.closeGUI()
33+
print("GUI was closed")
34+
35+
print("Simulation is done.")
36+
37+
38+
# Function called when the scene graph is being created
39+
def createScene(root):
40+
41+
root.addObject('RequiredPlugin', name='Sofa.Component.StateContainer')
42+
43+
# Scene must now include a AnimationLoop
44+
root.addObject('DefaultAnimationLoop')
45+
46+
# Add new nodes and objects in the scene
47+
node1 = root.addChild("Node1")
48+
node2 = root.addChild("Node2")
49+
50+
node1.addObject("MechanicalObject", template="Rigid3d", position="0 0 0 0 0 0 1", showObject="1")
51+
52+
node2.addObject("MechanicalObject", template="Rigid3d", position="1 1 1 0 0 0 1", showObject="1")
53+
54+
return root
55+
56+
57+
# Function used only if this script is called from a python environment
58+
if __name__ == '__main__':
59+
main()

examples/emptyController.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def onKeypressedEvent(self, event):
3939

4040
if ord(key) == 20: # right
4141
print("You pressed the Right key")
42+
43+
if key == 'M': # M key of the keyboard
44+
print("You pressed the M key")
45+
4246

4347
def onKeyreleasedEvent(self, event):
4448
key = event['key']
@@ -54,6 +58,10 @@ def onKeyreleasedEvent(self, event):
5458
if ord(key) == 20: # right
5559
print("You released the Right key")
5660

61+
if key == 'M': # M key of the keyboard
62+
print("You released the M key")
63+
64+
5765
def onMouseEvent(self, event):
5866
if (event['State']== 0): # mouse moving
5967
print("Mouse is moving (x,y) = "+str(event['mouseX'])+" , "+str(event['mouseY']))
@@ -96,13 +104,13 @@ def createScene(root):
96104
def main():
97105
import SofaRuntime
98106
import Sofa.Gui
99-
import SofaQt
107+
SofaRuntime.importPlugin("SofaImGui")
100108

101109
root=Sofa.Core.Node("root")
102110
createScene(root)
103111
Sofa.Simulation.initRoot(root)
104112

105-
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
113+
Sofa.Gui.GUIManager.Init("myscene", "imgui")
106114
Sofa.Gui.GUIManager.createGUI(root, __file__)
107115
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
108116
Sofa.Gui.GUIManager.MainLoop(root)

examples/emptyDataEngine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ def createScene(root):
3333
def main():
3434
import Sofa.Gui
3535
import SofaRuntime
36-
import SofaQt
36+
SofaRuntime.importPlugin("SofaImGui")
3737

3838
root=Sofa.Core.Node("root")
3939
createScene(root)
4040
Sofa.Simulation.initRoot(root)
4141

42-
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
42+
Sofa.Gui.GUIManager.Init("myscene", "imgui")
4343
Sofa.Gui.GUIManager.createGUI(root, __file__)
4444
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
4545
Sofa.Gui.GUIManager.MainLoop(root)

examples/emptyForceField.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ def createScene(root):
5858
def main():
5959
import SofaRuntime
6060
import Sofa.Gui
61-
import SofaQt
61+
SofaRuntime.importPlugin("SofaImGui")
6262

6363
root=Sofa.Core.Node("root")
6464
createScene(root)
6565
Sofa.Simulation.initRoot(root)
6666

67-
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
67+
Sofa.Gui.GUIManager.Init("myscene", "imgui")
6868
Sofa.Gui.GUIManager.createGUI(root, __file__)
6969
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
7070
Sofa.Gui.GUIManager.MainLoop(root)

examples/example-forcefield.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ def createScene(root):
6060
def main():
6161
import SofaRuntime
6262
import Sofa.Gui
63-
import SofaQt
63+
SofaRuntime.importPlugin("SofaImGui")
6464

6565
root=Sofa.Core.Node("root")
6666
createScene(root)
6767
Sofa.Simulation.initRoot(root)
6868

69-
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
69+
Sofa.Gui.GUIManager.Init("myscene", "imgui")
7070
Sofa.Gui.GUIManager.createGUI(root, __file__)
7171
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
7272
Sofa.Gui.GUIManager.MainLoop(root)

examples/liver-scriptcontroller.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ def main():
1515
Sofa.Simulation.initRoot(root)
1616

1717
if not USE_GUI:
18-
import SofaQt
19-
2018
for iteration in range(10):
2119
Sofa.Simulation.animate(root, root.dt.value)
2220
else:
23-
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
21+
SofaRuntime.importPlugin("SofaImGui")
22+
Sofa.Gui.GUIManager.Init("myscene", "imgui")
2423
Sofa.Gui.GUIManager.createGUI(root, __file__)
2524
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
2625
Sofa.Gui.GUIManager.MainLoop(root)
@@ -43,12 +42,13 @@ def createScene(root):
4342
'Sofa.Component.ODESolver.Backward',
4443
'Sofa.Component.SolidMechanics.FEM.Elastic',
4544
'Sofa.Component.StateContainer',
45+
'Sofa.Component.MechanicalLoad',
4646
'Sofa.Component.Topology.Container.Dynamic',
4747
'Sofa.Component.Visual',
4848
'Sofa.GL.Component.Rendering3D'
4949
])
5050

51-
root.addObject('DefaultAnimationLoop')
51+
root.addObject('DefaultAnimationLoop', computeBoundingBox=False)
5252

5353
root.addObject('VisualStyle', displayFlags="showCollisionModels hideVisualModels showForceFields")
5454
root.addObject('CollisionPipeline', name="CollisionPipeline")

examples/liver.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ def main():
1818
for iteration in range(10):
1919
Sofa.Simulation.animate(root, root.dt.value)
2020
else:
21-
import SofaQt
22-
23-
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
21+
SofaRuntime.importPlugin("SofaImGui")
22+
Sofa.Gui.GUIManager.Init("myscene", "imgui")
2423
Sofa.Gui.GUIManager.createGUI(root, __file__)
2524
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
2625
Sofa.Gui.GUIManager.MainLoop(root)

examples/loadXMLfromPython.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ def createScene(root):
3131
def main():
3232
import SofaRuntime
3333
import Sofa.Gui
34-
import SofaQt
3534

3635
root = Sofa.Core.Node("root")
3736
createScene(root)
3837
Sofa.Simulation.initRoot(root)
3938

4039
# Find out the supported GUIs
4140
print ("Supported GUIs are: " + Sofa.Gui.GUIManager.ListSupportedGUI(","))
42-
# Launch the GUI (qt or qglviewer)
43-
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
41+
# Launch the GUI (imgui is now by default, to use Qt please refer to the example "basic-useQtGui.py")
42+
SofaRuntime.importPlugin("SofaImGui")
43+
Sofa.Gui.GUIManager.Init("myscene", "imgui")
4444
Sofa.Gui.GUIManager.createGUI(root, __file__)
4545
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
4646
# Initialization of the scene will be done here

0 commit comments

Comments
 (0)