6. Manual compilation

The compilation of the SofaPython3 can be done when building SOFA, or it can be done independently using an already compiled/installed SOFA:

  • the former is called an In-tree build, which is, the compilation of SofaPython3 is included inside the build tree of SOFA

  • the latter is called an Out-of-tree build, hence the compilation of SofaPython3 is done in its own build tree, usually placed somewhere else than SOFA.

6.1. Prerequisites

The compilation of SofaPython3 requires the installation of some dependencies. The following table lists all of those prerequisites.

Package

Type

Installation

Description

Python 3 libs

Required

sudo apt install python3-dev

Used to link against the C-Python API

pybind11

Required

sudo apt install pybind11-dev

Heavily used to automatically create the binding code between SOFA and python

SOFA Framework

Required

You can either download it, or compile it from sources

6.2. In-tree build

We use here the term in-tree build to indicate that we wish to compile SofaPython3 in the same build tree as SOFA, which is, compiling SOFA will automatically compile SofaPython3 as an internal subproject. In this case, the compilation of the plugin should be straightforward, given that you installed correctly all the dependencies of SofaPython3.

Follow the same steps as the one needed to compile SOFA : https://www.sofa-framework.org/community/doc/getting-started/build/linux. When you get to the step Generate a Makefile with CMake, activate the CMake option SOFA_FETCH_SOFAPYTHON3:

Activate the cmake option SOFA_FETCH_SOFAPYTHON3

and configure CMake. This will pull the source code of SofaPython3 into the application/plugins directory of SOFA’s source directory.

Note

The CMake option SOFA_FETCH_SOFAPYTHON3 should disable itself automatically once CMake has been configure one time. This is to prevent pulling again the SofaPython3 source code again the next time you run cmake. If you need to update the plugin, you can manually turn the option ON again to pull the latest changes made in SofaPython3.

Once cmake has pulled the source code of SofaPython3 into the application/plugins directory, a new CMake option should now be available : PLUGIN_SOFAPYTHON3. Make sure you activate it and re-run CMake configure and generate:

Activate the cmake option PLUGIN_SOFAPYTHON3

If everything went fine during the cmake configuration stage, you should be able to find some information on the version of python and pybind11 used for the compilation of the plugin:

$ cmake ..
(...)
Adding plugin SofaPython3
-- Python:
    Version: 3.9.1
    Executable: /usr/bin/python3.9
    Headers: /usr/include/python3.9
    Libraries: /usr/lib64/libpython3.9.so
    User site: /home/jnbrunet/.local/lib/python3.9/site-packages
-- pybind11:
    Version: 2.6.1
    Config: /usr/share/cmake/pybind11/pybind11Config.cmake
(...)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jnbrunet/sources/sofa/build

At this point, you are ready to start the compilation of SOFA and the SofaPython3 plugin.

6.3. Out-of-tree build

6.3.1. Compilation

As detailed above in the Prerequisites section, you must have an installed version of SOFA in order to compile the SofaPython3 plugin out-of-tree. Not only must the sources be compiled following the SOFA documentation, but you have to make sure to have an install directory, i.e. to execute the installation (e.g. by runnin the command cmake --install . from the SOFA build directory)

Once done, export the installation path of SOFA inside the SOFA_ROOT environment variable. For example,

$ export SOFA_ROOT="/home/user/sofa/build/master/install"

Note

To make sure your SOFA_ROOT is well defined, you can verify that the following file path exists:

$SOFA_ROOT/lib/cmake/SofaFramework/SofaFrameworkTargets.cmake

We are now ready to compile SofaPython3. Let’s first create three new environment variables: SP3_SRC, SP3_BUILD and SP3_ROOT that will be use to set the location to the source code, the built files and the installed files, respectively. For example:

$ export SP3_SRC=/opt/SofaPython3/src
$ export SP3_BUILD=/opt/SofaPython3/build
$ export SP3_ROOT=/opt/SofaPython3/build/install

Let’s now pull the source code of the plugin, configure the build and start the compilation and installation.

$ git clone https://github.com/sofa-framework/SofaPython3.git $SP3_SRC
$ cmake -DCMAKE_PREFIX_PATH=$SOFA_ROOT/lib/cmake \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_INSTALL_PREFIX=$SP3_ROOT\
        -S $SP3_SRC \
        -B $SP3_BUILD
$ cmake --build $SP3_BUILD
$ cmake --install $SP3_BUILD

Keep the environment variable SOFA_ROOT and SP3_ROOT close by for the upcoming sections.

6.3.2. Setup using runSofa

Using SofaPython3 built out-of-tree (as explained just previously), you can load the SofaPython3 plugin in runSofa following one of this step:

  • use the “-l” of runSofa: runSofa -l /path/to/SofaPython3_build/lib/libSofaPython3.so <scene>

  • or use the environment variable SOFA_PLUGIN_PATH=/path/to/SofaPython3_build/

  • or add the component <AddPluginRepository path="/path/to/SofaPython3_build/"/> in your scene

  • or start runSofa, manually load the SofaPython3 library using Edit->Plugin Manager->Add , then restart runSofa: the plugin should load automatically

6.3.3. Setup using python3

See the Setup your environment using python3 section