Skip to content

Commit d02c833

Browse files
authored
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
1 parent 4cb31d0 commit d02c833

File tree

3 files changed

+66
-37
lines changed

3 files changed

+66
-37
lines changed

docs/sphinx/source/content/FirstSteps.rst

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,40 @@ Executing a simulation
88
With runSofa
99
^^^^^^^^^^^^
1010

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.
1214

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:
1419

15-
runSofa examples/example1.py
20+
.. code-block:: shell
1621
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
2123
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.
2326

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()``.
2428

2529
With the python3 interpreter
2630
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2731

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
3042

31-
When working a python3 interpreter, your simulation requires more than only the ``createScene()`` function.
3243
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:
3445

3546
.. code-block:: python
3647
@@ -67,10 +78,20 @@ One must therefore create it and then call the ``createScene()`` function:
6778
main()
6879
6980
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+
7290

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:
7495

7596
.. code-block:: python
7697
@@ -85,10 +106,11 @@ In case you want to manage the simulation from the runSofa GUI, you can simply c
85106
Sofa.Simulation.initRoot(root)
86107
87108
# Import the GUI package
109+
import SofaImGui
88110
import Sofa.Gui
89111
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")
92114
Sofa.Gui.GUIManager.createGUI(root, __file__)
93115
Sofa.Gui.GUIManager.SetDimension(1080, 800)
94116
@@ -102,6 +124,19 @@ So far, you can load this python scene, but it doesn't do much. Let's enrich thi
102124
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/>`_
103125

104126

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+
105140
Create a new simulation
106141
***********************
107142

@@ -345,6 +380,7 @@ Here is the entire code of the scene :
345380
.. code-block:: python
346381
347382
import Sofa
383+
import SofaImGui
348384
import Sofa.Gui
349385
350386
@@ -358,8 +394,8 @@ Here is the entire code of the scene :
358394
# Once defined, initialization of the scene graph
359395
Sofa.Simulation.initRoot(root)
360396
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")
398+
Sofa.Gui.GUIManager.Init("myscene", "imgui")
363399
Sofa.Gui.GUIManager.createGUI(root, __file__)
364400
Sofa.Gui.GUIManager.SetDimension(1080, 800)
365401

docs/sphinx/source/content/Installation.rst

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,10 @@ Before running your simulations, you must make sure to define the following envi
130130
After that, all you need to do is open a Console (cmd.exe) and run: ``runSofa -lSofaPython3``
131131

132132

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*.
133135

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>`_
147137

148138

149139
Get support

docs/sphinx/source/content/UsingThePlugin.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,10 @@ By structuring your scripts this way, you get the advantage to have a script loa
221221
# Once defined, initialization of the scene graph
222222
Sofa.Simulation.init(root)
223223
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")
226228
Sofa.Gui.GUIManager.createGUI(root, __file__)
227229
Sofa.Gui.GUIManager.SetDimension(1080, 800)
228230
@@ -415,6 +417,7 @@ Here is the entire code of the scene :
415417
.. code-block:: python
416418
417419
import Sofa
420+
import SofaImGui
418421
import Sofa.Gui
419422
420423
@@ -428,8 +431,8 @@ Here is the entire code of the scene :
428431
# Once defined, initialization of the scene graph
429432
Sofa.Simulation.init(root)
430433
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")
433436
Sofa.Gui.GUIManager.createGUI(root, __file__)
434437
Sofa.Gui.GUIManager.SetDimension(1080, 800)
435438
@@ -644,6 +647,6 @@ Do not hesitate to take a look and get inspiration!
644647
Get support
645648
***********
646649

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.
648651

649652
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

Comments
 (0)