-
Notifications
You must be signed in to change notification settings - Fork 55
Update documentation: ImGui, Qt, and Python env #522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
99389d4
8d288c6
e5f203d
aca8f2a
492a9e0
79507e0
01a9693
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,29 +8,35 @@ Executing a simulation | |
| With runSofa | ||
| ^^^^^^^^^^^^ | ||
|
|
||
| You can load a python based simulation directly in runSofa by using the command | ||
| Making sure the SofaPython3 plugin is loaded, you can load a simulation described by a python script directly in runSofa. | ||
| Assuming you want to run a script named "example.py", you can run the following command: | ||
|
|
||
| .. code-block:: shell | ||
|
|
||
| runSofa examples/example1.py | ||
| runSofa example.py | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we mention at one point the necessity to load SofaPython3 here ?
|
||
|
|
||
| Let's now open ``examples/example1.py`` to have more insight. | ||
| The first important thin to notice is that the python script is importing modules called ``Sofa``, this module, and few other are containing | ||
| all SOFA specific component. Then the script is defining a ``createScene()`` function. This function is the entry point of your simulation and | ||
| is automatically called by the runSofa application when a python file is loaded. | ||
|
|
||
| We will look with more details in this ``createScene()`` but first let's how we can execute the same file without using ``runSofa``. | ||
| Let's now see how this script ``example.py`` should look like. | ||
| The first important thing to notice is that to be compatible with SOFA, a python script must define the ``createScene()`` function. This function is the entry point of your simulation and | ||
hugtalbot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
hugtalbot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| is automatically called by the runSofa application when a python file is loaded. It is responsible for describing and building the SOFA scene graph. | ||
hugtalbot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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()``. | ||
|
|
||
| With the python3 interpreter | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| 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 | ||
| so your python environment is properly configured and has access to the SOFA specific python modules. | ||
| SOFA simulation can also be executed from a python environment (including jupyter notebook). | ||
| To do so, the Python environment must be filled in for SOFA python modules. | ||
| Located in *site-packages/* repositories, the path to these libraries should be added to the ``PYTHONPATH``. | ||
| The section "`Setup your python3 environment <https://sofapython3.readthedocs.io/en/latest/content/Installation.html#using-python3>`_" section details how to configure it. | ||
|
|
||
| Once your python environment is properly configured, you will be able to import SOFA python modules (e.g. ``import Sofa``). | ||
| By running your simulation from a python interpreter, you will be responsible for: | ||
| * creating the root node | ||
| * before calling the ``createScene()`` function | ||
| * and later initializing the graph | ||
|
|
||
| When working a python3 interpreter, your simulation requires more than only the ``createScene()`` function. | ||
| Indeed, the python environment does not pre-generate a root node as the runSofa executable is. | ||
| One must therefore create it and then call the ``createScene()`` function: | ||
| To be run from a python environment, any python script should therefore look like: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
|
|
@@ -67,10 +73,20 @@ One must therefore create it and then call the ``createScene()`` function: | |
| main() | ||
|
|
||
|
|
||
| By structuring your scripts this way, you get the advantage to have a script loadable from both runSofa and a python3 interpreter. | ||
| Note that the ``main()`` function runs 10 time steps without any graphical user interface and the script ends. | ||
| The above script can be run as follows: | ||
| .. code-block:: shell | ||
|
|
||
| python3 example.py | ||
|
|
||
| It can be noted that: | ||
| * by structuring your scripts this way, you get the advantage to have a script loadable from both runSofa and a python3 interpreter. | ||
| * in the above example, the ``main()`` function runs 10 time steps without any graphical user interface and the script ends. | ||
|
|
||
|
|
||
| Using the SOFA GUI from a python environment | ||
| """""""""""""""""""""""""""""""""""""""""""" | ||
|
|
||
| In case you want to manage the simulation from the runSofa GUI, you can simply change the ``main()`` function as follows: | ||
| In case you want to manage the simulation from the runSofa GUI, you can call the GUI from the ``main()`` function, as follows: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
|
|
@@ -85,10 +101,12 @@ In case you want to manage the simulation from the runSofa GUI, you can simply c | |
| Sofa.Simulation.initRoot(root) | ||
|
|
||
| # Import the GUI package | ||
| import SofaRuntime | ||
| import Sofa.Gui | ||
|
|
||
| # Launch the GUI (qt or qglviewer) | ||
| Sofa.Gui.GUIManager.Init("myscene", "qglviewer") | ||
| # Launch the GUI (imgui is now by default, to use Qt please refer to the example "basic-useQtGui.py") | ||
| SofaRuntime.importPlugin("SofaImGui") | ||
hugtalbot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Sofa.Gui.GUIManager.Init("myscene", "imgui") | ||
| Sofa.Gui.GUIManager.createGUI(root, __file__) | ||
| Sofa.Gui.GUIManager.SetDimension(1080, 800) | ||
|
|
||
|
|
@@ -102,6 +120,19 @@ So far, you can load this python scene, but it doesn't do much. Let's enrich thi | |
| 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/>`_ | ||
|
|
||
|
|
||
| Using the old Qt GUI | ||
| """""""""""""""""""" | ||
|
|
||
| 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: | ||
|
|
||
| * add the *lib/* repository in the SOFA binaries to your ``LD_LIBRARY_PATH`` | ||
| * add the *lib/python3/site-packages/* repository to your ``PYTHONPATH`` | ||
| * make sure your SOFA install path does not include any special character | ||
|
|
||
| An example using the Qt GUI is available: `basic-useQtGUI.py <https://github.com/sofa-framework/SofaPython3/blob/master/examples/basic-useQtGUI.py>`_ | ||
|
|
||
|
|
||
|
|
||
| Create a new simulation | ||
| *********************** | ||
|
|
||
|
|
@@ -345,6 +376,7 @@ Here is the entire code of the scene : | |
| .. code-block:: python | ||
|
|
||
| import Sofa | ||
| import SofaRuntime | ||
| import Sofa.Gui | ||
|
|
||
|
|
||
|
|
@@ -358,8 +390,9 @@ Here is the entire code of the scene : | |
| # Once defined, initialization of the scene graph | ||
| Sofa.Simulation.initRoot(root) | ||
|
|
||
| # Launch the GUI (qt or qglviewer) | ||
| Sofa.Gui.GUIManager.Init("myscene", "qglviewer") | ||
| # Launch the GUI (imgui is now by default, to use Qt please refer to the example "basic-useQtGui.py") | ||
| SofaRuntime.importPlugin("SofaImGui") | ||
| Sofa.Gui.GUIManager.Init("myscene", "imgui") | ||
| Sofa.Gui.GUIManager.createGUI(root, __file__) | ||
| Sofa.Gui.GUIManager.SetDimension(1080, 800) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.