You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update documentation: ImGui, Qt, and Python env (#522)
* Update documentation to explicit the use of ImGui and SOFA in Python
* add description to use SofaQt
* clarify and clean the installation section
* Rename example1 as example
* simplify writing for ImGui use
* Apply suggestions from code review
Copy file name to clipboardExpand all lines: docs/sphinx/source/content/FirstSteps.rst
+55-19Lines changed: 55 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,29 +8,40 @@ Executing a simulation
8
8
With runSofa
9
9
^^^^^^^^^^^^
10
10
11
-
You can load a python based simulation directly in runSofa by using the command
11
+
💡 To make sure runSofa is able to load a scene described by a python, you can either:
12
+
* open runSofa without any argument once and then add libSofaPython3 in the plugin manager
13
+
* or add -l SofaPython3 in this command line.
12
14
13
-
.. code-block:: shell
15
+
For more information, please refer to the documentation: `What is a plugin > Plugin loading<https://sofa-framework.github.io/doc/plugins/what-is-a-plugin/#plugin_loading>`_
16
+
17
+
Once the SofaPython3 plugin is loaded, you can load a simulation from a python script directly in runSofa.
18
+
Assuming you want to run a script named "example.py", you can run the following command:
14
19
15
-
runSofa examples/example1.py
20
+
.. code-block:: shell
16
21
17
-
Let's now open ``examples/example1.py`` to have more insight.
18
-
The first important thin to notice is that the python script is importing modules called ``Sofa``, this module, and few other are containing
19
-
all SOFA specific component. Then the script is defining a ``createScene()`` function. This function is the entry point of your simulation and
20
-
is automatically called by the runSofa application when a python file is loaded.
22
+
runSofa example.py
21
23
22
-
We will look with more details in this ``createScene()`` but first let's how we can execute the same file without using ``runSofa``.
24
+
Let's now see how this script ``example.py`` should look like.
25
+
The first important thing to notice is that to be compatible with SOFA, a python script must define the ``createScene(root : Sofa.Core.Node)`` function. This function is the entry point of your simulation and it is automatically called by the runSofa application when a python file is loaded. It is responsible for describing and building the SOFA scene graph.
23
26
27
+
In the section "`Create a new simulation <https://sofapython3.readthedocs.io/en/latest/content/FirstSteps.html#create-a-new-simulation>`_" below, we will detail how to implement this ``createScene()``.
24
28
25
29
With the python3 interpreter
26
30
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27
31
28
-
Before being able to execute a simulation from a python interpreter (including jupyter notebook) be sure you read the "`Setup your python3 environment <https://sofapython3.readthedocs.io/en/latest/content/Installation.html#using-python3>`_" section
29
-
so your python environment is properly configured and has access to the SOFA specific python modules.
32
+
SOFA simulation can also be executed from a python environment (including jupyter notebook).
33
+
To do so, the Python environment must be filled in for SOFA python modules.
34
+
Located in *site-packages/* repositories, the path to these libraries should be added to the ``PYTHONPATH``.
35
+
The section "`Setup your python3 environment <https://sofapython3.readthedocs.io/en/latest/content/Installation.html#using-python3>`_" section details how to configure it.
36
+
37
+
Once your python environment is properly configured, you will be able to import SOFA python modules (e.g. ``import Sofa``).
38
+
By running your simulation from a python interpreter, you will be responsible for:
39
+
* creating the root node
40
+
* before calling the ``createScene()`` function
41
+
* and later initializing the graph
30
42
31
-
When working a python3 interpreter, your simulation requires more than only the ``createScene()`` function.
32
43
Indeed, the python environment does not pre-generate a root node as the runSofa executable is.
33
-
One must therefore create it and then call the ``createScene()`` function:
44
+
To be run from a python environment, any python script should therefore look like:
34
45
35
46
.. code-block:: python
36
47
@@ -67,10 +78,20 @@ One must therefore create it and then call the ``createScene()`` function:
67
78
main()
68
79
69
80
70
-
By structuring your scripts this way, you get the advantage to have a script loadable from both runSofa and a python3 interpreter.
71
-
Note that the ``main()`` function runs 10 time steps without any graphical user interface and the script ends.
81
+
The above script can be run as follows:
82
+
.. code-block:: shell
83
+
84
+
python3 example.py
85
+
86
+
It can be noted that:
87
+
* by structuring your scripts this way, you get the advantage to have a script loadable from both runSofa and a python3 interpreter.
88
+
* in the above example, the ``main()`` function runs 10 time steps without any graphical user interface and the script ends.
89
+
72
90
73
-
In case you want to manage the simulation from the runSofa GUI, you can simply change the ``main()`` function as follows:
91
+
Using the SOFA GUI from a python environment
92
+
""""""""""""""""""""""""""""""""""""""""""""
93
+
94
+
In case you want to manage the simulation from the runSofa GUI, you can call the GUI from the ``main()`` function, as follows:
74
95
75
96
.. code-block:: python
76
97
@@ -85,10 +106,11 @@ In case you want to manage the simulation from the runSofa GUI, you can simply c
85
106
Sofa.Simulation.initRoot(root)
86
107
87
108
# Import the GUI package
109
+
import SofaImGui
88
110
import Sofa.Gui
89
111
90
-
# Launch the GUI (qt or qglviewer)
91
-
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
112
+
# Launch the GUI (imgui is now by default, to use Qt please refer to the example "basic-useQtGui.py")
113
+
Sofa.Gui.GUIManager.Init("myscene", "imgui")
92
114
Sofa.Gui.GUIManager.createGUI(root, __file__)
93
115
Sofa.Gui.GUIManager.SetDimension(1080, 800)
94
116
@@ -102,6 +124,19 @@ So far, you can load this python scene, but it doesn't do much. Let's enrich thi
102
124
A scene in SOFA is an ordered tree of nodes representing objects (example of node: hand), with parent/child relationship (example of hand's child: finger). Each node has one or more components. Every node and component has a name and a few features. The main node at the top of the tree is usually called "rootNode" or "root". More about how to create a simulation scene can be found in the `SOFA online documentation <https://www.sofa-framework.org/community/doc/using-sofa/lexicography/>`_
103
125
104
126
127
+
Using the old Qt GUI
128
+
""""""""""""""""""""
129
+
130
+
Since SOFA v25.06, SOFA GUI relies on the ImGui library. The previous Qt-based GUI is still available. To use it, make sure to:
131
+
132
+
* add the *lib/* repository in the SOFA binaries to your ``LD_LIBRARY_PATH``
133
+
* add the *lib/python3/site-packages/* repository to your ``PYTHONPATH``
134
+
* make sure your SOFA install path does not include any special character
135
+
136
+
An example using the Qt GUI is available: `basic-useQtGUI.py <https://github.com/sofa-framework/SofaPython3/blob/master/examples/basic-useQtGUI.py>`_
137
+
138
+
139
+
105
140
Create a new simulation
106
141
***********************
107
142
@@ -345,6 +380,7 @@ Here is the entire code of the scene :
345
380
.. code-block:: python
346
381
347
382
import Sofa
383
+
import SofaImGui
348
384
import Sofa.Gui
349
385
350
386
@@ -358,8 +394,8 @@ Here is the entire code of the scene :
358
394
# Once defined, initialization of the scene graph
359
395
Sofa.Simulation.initRoot(root)
360
396
361
-
# Launch the GUI (qt or qglviewer)
362
-
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
397
+
# Launch the GUI (imgui is now by default, to use Qt please refer to the example "basic-useQtGui.py")
Copy file name to clipboardExpand all lines: docs/sphinx/source/content/Installation.rst
+3-13Lines changed: 3 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -130,20 +130,10 @@ Before running your simulations, you must make sure to define the following envi
130
130
After that, all you need to do is open a Console (cmd.exe) and run: ``runSofa -lSofaPython3``
131
131
132
132
133
+
It must be noted that depending on the plugins you use, you might have to add the *site-packages/* paths associated to these plugins to your ``PYTHONPATH``.
134
+
These are usually located in */path_to_plugin/lib/python3/site-packages*.
133
135
134
-
It is possible to use SOFA in any python3 interpreter.
135
-
The following code should cover most basic SOFA elements:
136
-
137
-
.. code-block:: python
138
-
139
-
# to be able to create SOFA objects you need to first load the plugins that implement them.
140
-
# For simplicity you can load the plugin "Sofa.Component" that will load all most
141
-
# common sofa objects.
142
-
import SofaRuntime
143
-
SofaRuntime.importPlugin("Sofa.Component")
144
-
145
-
# to create elements like Node or objects
146
-
import Sofa.Core
136
+
To discover, how to use SOFA in any python3 interpreter, please refer to the associated `FirstSteps section <https://sofapython3.readthedocs.io/en/latest/content/FirstSteps.html#with-the-python3-interpreter>`_
Copy file name to clipboardExpand all lines: docs/sphinx/source/content/UsingThePlugin.rst
+8-5Lines changed: 8 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -221,8 +221,10 @@ By structuring your scripts this way, you get the advantage to have a script loa
221
221
# Once defined, initialization of the scene graph
222
222
Sofa.Simulation.init(root)
223
223
224
-
# Launch the GUI (qt or qglviewer)
225
-
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
224
+
# Launch the GUI (imgui is now by default, to use Qt please refer to the example "basic-useQtGui.py")
225
+
import SofaImGui
226
+
import Sofa.Gui
227
+
Sofa.Gui.GUIManager.Init("myscene", "imgui")
226
228
Sofa.Gui.GUIManager.createGUI(root, __file__)
227
229
Sofa.Gui.GUIManager.SetDimension(1080, 800)
228
230
@@ -415,6 +417,7 @@ Here is the entire code of the scene :
415
417
.. code-block:: python
416
418
417
419
import Sofa
420
+
import SofaImGui
418
421
import Sofa.Gui
419
422
420
423
@@ -428,8 +431,8 @@ Here is the entire code of the scene :
428
431
# Once defined, initialization of the scene graph
429
432
Sofa.Simulation.init(root)
430
433
431
-
# Launch the GUI (qt or qglviewer)
432
-
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
434
+
# Launch the GUI (imgui is now by default, to use Qt please refer to the example "basic-useQtGui.py")
435
+
Sofa.Gui.GUIManager.Init("myscene", "imgui")
433
436
Sofa.Gui.GUIManager.createGUI(root, __file__)
434
437
Sofa.Gui.GUIManager.SetDimension(1080, 800)
435
438
@@ -644,6 +647,6 @@ Do not hesitate to take a look and get inspiration!
644
647
Get support
645
648
***********
646
649
647
-
To freely get technical assistance from the community, please do not hesitate to join the `SofaPython3 GitHub forum <https://github.com/sofa-framework/sofapython3/discussions>`_ and post there any question related to SofaPython3.
650
+
To freely get technical assistance from the community, please do not hesitate to join the `SofaPython3 GitHub forum <https://github.com/sofa-framework/sofa/discussions/categories/sofapython3>`_ and post there any question related to SofaPython3.
648
651
649
652
To quickly level up on SOFA, never hesitate to request `SOFA training sessions <https://www.sofa-framework.org/sofa-events/sofa-training-sessions/>`_.
0 commit comments