1. Install

The SofaPython3 plugins allows you to embed a python3 interpreter into an existing SOFA application (eg: runSofa) and to create/launch SOFA simulations from a python environment. Depending on your use case you may have different step to follow to get a fully working SofaPython3 environnement.

1.1. Get SofaPython3

Latest version of the SofaPython3 plugins are now shipped within the binary SOFA release supported by the SOFA Constortium. All you need is therefore to download these SOFA binaries from the SOFA website.

1.2. Get python installed

First, make sure you have the same version of python installed on your computer as the one that is used in the binary version.

Run in a terminal:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install libpython3.8 python3.8 python3-pip
python3.8 -m pip install numpy

if you want to launch the runSofa:

sudo apt install libopengl0

1.3. Setup your environment

1.3.1. using runSofa

Using SofaPython3 in runSofa requires loading the SofaPython3 plugin in your runSofa environment. If you downloaded and installed SOFA from the SOFA website (as explained above, see Get SofaPython3), you can load the SofaPython3 plugin using the PluginManager (in the GUI) or by auto-loading the plugin in runSofa: simply copy the file plugin_list.conf.default in <SOFA_build>/lib, and rename it plugin_list.conf, then add the line:

SofaPython3 NO_VERSION

Having the SofaPython3 plugin active will allow you to open scene files using the “.py, .py3, .pyscn, .pyscn3” file extension in runSofa, with the command :

<SOFA_build>/bin/runSofa <your_python_file>

1.3.2. using python3

Before running your simulations, you must make sure to define the following environment variables:

Run in a terminal:

export SOFA_ROOT=/path/to/SOFA_install
export PYTHONPATH=/path/to/SofaPython3/lib/python3/site-packages:$PYTHONPATH

It is possible to use SOFA in any python3 interpreter. The following code should cover most basic SOFA elements:

# to be able to create SOFA objects you need to first load the plugins that implement them.
# For simplicity you can load the plugin "Sofa.Component" that will load all most
# common sofa objects.
import SofaRuntime
SofaRuntime.importPlugin("Sofa.Component")

# to create elements like Node or objects
import Sofa.Core

1.3.3. Get support

To freely get technical assistance from the community, please do not hesitate to join the SofaPython3 GitHub forum and post there any question related to SofaPython3.

To quickly level up on SOFA, never hesitate to request SOFA training sessions.