### Installing OpenGL Library for runSofa on Ubuntu Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Installation.rst This command installs the `libopengl0` package on Ubuntu, which is required to launch the `runSofa` application, ensuring necessary graphical libraries are present. ```bash sudo apt install libopengl0 ``` -------------------------------- ### Verifying Python and Installing Libraries on Windows Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Installation.rst This command, run in a Windows console after setting system variables, verifies the Python version and installs the NumPy and SciPy libraries, which are essential dependencies for SofaPython3. ```bash python -V && python -m pip install numpy scipy ``` -------------------------------- ### Complete SOFA Scene Setup with Collision and GUI in Python Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This comprehensive example demonstrates a full SOFA scene setup. It includes the main function for GUI initialization and simulation loop, and a `createScene` function that defines the root node, sets gravity and timestep, loads necessary SOFA plugins, and configures the collision pipeline with broad phase and constraint solvers. ```python import Sofa import Sofa.Gui def main(): # Call the SOFA function to create the root node root = Sofa.Core.Node("root") # Call the createScene function, as runSofa does createScene(root) # Once defined, initialization of the scene graph Sofa.Simulation.init(root) # Launch the GUI (qt or qglviewer) Sofa.Gui.GUIManager.Init("myscene", "qglviewer") Sofa.Gui.GUIManager.createGUI(root, __file__) Sofa.Gui.GUIManager.SetDimension(1080, 800) # Initialization of the scene will be done here Sofa.Gui.GUIManager.MainLoop(root) Sofa.Gui.GUIManager.closeGUI() def createScene(rootNode): rootNode.addObject("VisualGrid", nbSubdiv=10, size=1000) # Define the root node properties rootNode.gravity=[0.0,-9.81,0.0] rootNode.dt=0.01 # Loading all required SOFA modules confignode = rootNode.addChild("Config") confignode.addObject('RequiredPlugin', name="Sofa.Component.AnimationLoop", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Collision.Detection.Algorithm", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Collision.Detection.Intersection", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Collision.Geometry", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Collision.Response.Contact", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Constraint.Lagrangian.Correction", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Constraint.Lagrangian.Solver", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.IO.Mesh", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.LinearSolver.Iterative", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Mapping.NonLinear", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Mass", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.ODESolver.Backward", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.StateContainer", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Topology.Container.Constant", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Visual", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.GL.Component.Rendering3D", printLog=False) confignode.addObject('OglSceneFrame', style="Arrows", alignment="TopRight") # Collision pipeline rootNode.addObject('DefaultPipeline') rootNode.addObject('FreeMotionAnimationLoop') rootNode.addObject('GenericConstraintSolver', tolerance="1e-6", maxIterations="1000") rootNode.addObject('BruteForceBroadPhase') ``` -------------------------------- ### Basic SOFA Object Creation in Python Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Installation.rst This Python snippet demonstrates how to initialize SofaPython3 by importing `SofaRuntime` and loading the `Sofa.Component` plugin, which provides common SOFA objects. It also shows how to import `Sofa.Core` for creating fundamental SOFA elements like Nodes. ```python # to be able to create SOFA objects you need to first load the plugins that implement them.\n# For simplicity you can load the plugin "Sofa.Component" that will load all most\n# common sofa objects.\nimport SofaRuntime\nSofaRuntime.importPlugin("Sofa.Component")\n\n# to create elements like Node or objects\nimport Sofa.Core ``` -------------------------------- ### Launching runSofa with a Python Scene File Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Installation.rst This command demonstrates how to launch the runSofa application and load a SOFA scene defined in a Python file, using the specified build directory and the Python file path. ```bash /bin/runSofa ``` -------------------------------- ### Installing SofaPython3 Plugin and Bindings (CMake) Source: https://github.com/sofa-framework/sofapython3/blob/master/README.md This command, executed from within the SofaPython3 build directory, initiates the CMake installation process. It installs the SofaPython3 plugin and its Python bindings into the designated install directory, and then creates symbolic links to the bindings in the Python user site-packages directory. ```Shell cmake --install . ``` -------------------------------- ### Configuring SofaPython3 Plugin for runSofa Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Installation.rst This line is added to the `plugin_list.conf` file to enable the SofaPython3 plugin within the runSofa environment, allowing it to load Python-based SOFA scene files. ```text SofaPython3 NO_VERSION ``` -------------------------------- ### Installing Python 3.12 and NumPy on Ubuntu Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Installation.rst This snippet provides commands to install Python 3.12, pip, and NumPy on Ubuntu using the deadsnakes PPA. It ensures the correct Python version and essential libraries are available for SofaPython3. ```bash sudo add-apt-repository ppa:deadsnakes/ppa sudo apt install libpython3.12 python3.12 python3-pip python3.12 -m pip install numpy ``` -------------------------------- ### Installing NumPy on MacOS Catalina Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Installation.rst This command installs the NumPy library for macOS Catalina users, providing essential numerical computing capabilities required by SofaPython3. ```bash pip3 install numpy ``` -------------------------------- ### Installing OpenGL Library for runSofa on Ubuntu Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This command installs the `libopengl0` package on Ubuntu, which is a required dependency to successfully launch the `runSofa` application after setting up SofaPython3. ```bash sudo apt install libopengl0 ``` -------------------------------- ### Installing SofaPython3 Documentation Requirements (Shell) Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/README.md Navigates into the `SofaPython3/docs/sphinx` directory and installs project-specific Python dependencies listed in `source/requirements.txt` using pip. This ensures all necessary packages for the SofaPython3 documentation build are available. ```shell pip3 install --user -r source/requirements.txt ``` -------------------------------- ### Installing Core Sphinx and Python Dependencies (Shell) Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/README.md Installs essential build tools, Python 3, pip, and Sphinx, followed by the Sphinx Read the Docs theme for documentation styling. These are system-wide dependencies required for building Sphinx documentation. ```shell sudo apt install build-essential python3 python3-pip python3-sphinx pip3 install --user sphinx_rtd_theme ``` -------------------------------- ### Installing Python 3.10 and NumPy on Ubuntu Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This snippet provides commands to install Python 3.10, pip, and the NumPy library on Ubuntu, which are essential prerequisites for using SofaPython3. It first adds a PPA to access newer Python versions. ```bash sudo add-apt-repository ppa:deadsnakes/ppa sudo apt install libpython3.10 python3.10 python3-pip python3.10 -m pip install numpy ``` -------------------------------- ### Setting Environment Variables for SofaPython3 on Ubuntu Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Installation.rst These commands set the `SOFA_ROOT` and `PYTHONPATH` environment variables on Ubuntu, which are crucial for the Python interpreter to locate the SOFA installation and SofaPython3 libraries when running simulations directly from Python. ```bash export SOFA_ROOT=/path/to/SOFA_install export PYTHONPATH=/path/to/SofaPython3/lib/python3/site-packages:$PYTHONPATH ``` -------------------------------- ### Compiling and Installing SofaPython3 Out-of-Tree (Bash) Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Compilation.rst This sequence of commands performs the complete out-of-tree compilation and installation of SofaPython3. It involves cloning the source, configuring CMake with SOFA's prefix path, building the project, and finally installing the compiled plugin to the specified `SP3_ROOT`. ```bash git clone https://github.com/sofa-framework/SofaPython3.git $SP3_SRC cmake -DCMAKE_PREFIX_PATH=$SOFA_ROOT/lib/cmake \n -DCMAKE_BUILD_TYPE=Release \n -DCMAKE_INSTALL_PREFIX=$SP3_ROOT\n -S $SP3_SRC \n -B $SP3_BUILD cmake --build $SP3_BUILD cmake --install $SP3_BUILD ``` -------------------------------- ### Python Script Entry Point for SOFA Scene Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This standard Python snippet ensures that the `main()` function is called only when the script is executed directly, not when imported as a module. It serves as the primary entry point for running the SOFA simulation setup. ```Python if __name__ == '__main__': main() ``` -------------------------------- ### Installing NumPy on MacOS BigSur Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Installation.rst For macOS BigSur users, this snippet upgrades pip and then installs the NumPy library using the Python 3.12 specific pip, which is essential for scientific computing with SofaPython3. ```bash pip3 install --upgrade pip python3.12 -m pip install numpy ``` -------------------------------- ### Installing Python 3.12 on MacOS via Homebrew Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Installation.rst This snippet uses Homebrew to install Python 3.12 on macOS and updates the system's PATH environment variable to prioritize the newly installed Python version, ensuring it's accessible from the terminal. ```bash brew install python@3.12 export PATH="/usr/local/opt/python@3.12/bin/:$PATH" ``` -------------------------------- ### Installing pybind11-stubgen (Python) Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/README.md Installs `pybind11-stubgen` directly from its GitHub repository using pip. This tool is crucial for generating Python stub files (.pyi) from pybind11-bound C++ modules, which are necessary for Sphinx to document the SofaPython3 bindings. ```python python -m pip install git+https://github.com/sizmailov/pybind11-stubgen.git ``` -------------------------------- ### Setting Environment Variables for SofaPython3 on MacOS Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Installation.rst These commands configure `SOFA_ROOT`, `PYTHONPATH`, and update the `PATH` environment variable on macOS. This setup ensures that the Python interpreter can find the SOFA installation, SofaPython3 libraries, and the correct Python 3.12 executable when running SOFA simulations. ```bash export SOFA_ROOT=/path/to/SOFA_install export PYTHONPATH=/path/to/SofaPython3/lib/python3/site-packages:$PYTHONPATH export PATH="/usr/local/opt/python@3.12/bin/:$PATH" ``` -------------------------------- ### Creating a Basic SOFA Scene with GUI in Python Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/FirstSteps.rst This comprehensive Python script defines the `main` function to initialize and run a SOFA simulation, including GUI setup, and the `createScene` function to configure the scene graph, load essential SOFA plugins, define collision pipelines, and add a deformable sphere object with collision and visualization properties. It demonstrates a complete workflow for a simple SOFA simulation. ```Python import Sofa import Sofa.Gui def main(): # Call the SOFA function to create the root node root = Sofa.Core.Node("root") # Call the createScene function, as runSofa does createScene(root) # Once defined, initialization of the scene graph Sofa.Simulation.initRoot(root) # Launch the GUI (qt or qglviewer) Sofa.Gui.GUIManager.Init("myscene", "qglviewer") Sofa.Gui.GUIManager.createGUI(root, __file__) Sofa.Gui.GUIManager.SetDimension(1080, 800) # Initialization of the scene will be done here Sofa.Gui.GUIManager.MainLoop(root) Sofa.Gui.GUIManager.closeGUI() def createScene(rootNode): # Define the root node properties rootNode.gravity=[0.0,-9.81,0.0] rootNode.dt=0.01 # Loading all required SOFA modules confignode = rootNode.addChild("Config") confignode.addObject('RequiredPlugin', name="Sofa.Component.AnimationLoop", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Collision.Detection.Algorithm", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Collision.Detection.Intersection", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Collision.Geometry", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Collision.Response.Contact", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Constraint.Lagrangian.Correction", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Constraint.Lagrangian.Solver", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.IO.Mesh", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.LinearSolver.Iterative", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Mapping.NonLinear", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Mass", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.ODESolver.Backward", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.StateContainer", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Topology.Container.Constant", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.Component.Visual", printLog=False) confignode.addObject('RequiredPlugin', name="Sofa.GL.Component.Rendering3D", printLog=False) confignode.addObject('OglSceneFrame', style="Arrows", alignment="TopRight") confignode.addObject("VisualGrid", nbSubdiv=10, size=1000) # Collision pipeline rootNode.addObject('CollisionPipeline') rootNode.addObject('FreeMotionAnimationLoop') rootNode.addObject('GenericConstraintSolver', tolerance="1e-6", maxIterations="1000") rootNode.addObject('BruteForceBroadPhase') rootNode.addObject('BVHNarrowPhase') rootNode.addObject('RuleBasedContactManager', responseParams="mu="+str(0.0), name='Response', response='FrictionContactConstraint') rootNode.addObject('LocalMinDistance', alarmDistance=10, contactDistance=5, angleCone=0.01) totalMass = 1.0 volume = 1.0 inertiaMatrix=[1., 0., 0., 0., 1., 0., 0., 0., 1.] sphere = rootNode.addChild("sphere") sphere.addObject('EulerImplicitSolver', name='odesolver') sphere.addObject('CGLinearSolver', name='Solver', iterations=25, tolerance=1e-05, threshold=1e-05) sphere.addObject('MechanicalObject', name="mstate", template="Rigid3", translation2=[0., 0., 0.], rotation2=[0., 0., 0.], showObjectScale=50) sphere.addObject('UniformMass', name="mass", vertexMass=[totalMass, volume, inertiaMatrix[:]]) sphere.addObject('UncoupledConstraintCorrection') #### Collision subnode for the sphere collision = sphere.addChild('collision') collision.addObject('MeshOBJLoader', name="loader", filename="mesh/ball.obj", triangulate="true", scale=45.0) collision.addObject('MeshTopology', src="@loader") collision.addObject('MechanicalObject') collision.addObject('TriangleCollisionModel') collision.addObject('LineCollisionModel') collision.addObject('PointCollisionModel') collision.addObject('RigidMapping') #### Visualization subnode for the sphere ``` -------------------------------- ### Installing Python 3 Development Libraries on Ubuntu Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Compilation.rst This command installs the Python 3 development libraries (python3-dev) on Ubuntu. These libraries are essential for linking against the C-Python API, a prerequisite for compiling SofaPython3. ```Shell sudo apt install python3-dev ``` -------------------------------- ### Installing pybind11 Development Libraries on Ubuntu Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Compilation.rst This command installs the pybind11 development libraries (pybind11-dev) on Ubuntu. pybind11 is a critical dependency used for automatically generating the binding code between SOFA and Python. ```Shell sudo apt install pybind11-dev ``` -------------------------------- ### Installing Python 3 on MacOS with Homebrew Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Compilation.rst This command installs Python 3 on MacOS using Homebrew. It provides the necessary Python libraries required for linking against the C-Python API during SofaPython3 compilation. ```Shell brew install python3 ``` -------------------------------- ### Installing Python 3.10 on MacOS with Homebrew Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This snippet uses Homebrew to install Python 3.10 on MacOS and then updates the system's PATH environment variable to ensure the newly installed Python version is prioritized. ```bash brew install python@3.10 export PATH="/usr/local/opt/python@3.10/bin/:$PATH" ``` -------------------------------- ### Importing SofaPython3 Bindings in Python Source: https://github.com/sofa-framework/sofapython3/blob/master/README.md This Python interactive session demonstrates how to import the `SofaRuntime` and `Sofa` modules after successful installation of SofaPython3 bindings. It then shows the creation of a root node, confirming that the bindings are correctly accessible and functional within a Python environment. ```Python import SofaRuntime import Sofa root = Sofa.Core.Node("root") ``` -------------------------------- ### Installing NumPy on MacOS Catalina Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst For MacOS Catalina users, this command installs the NumPy library using pip3, which is a key dependency for SofaPython3 functionality. ```bash pip3 install numpy ``` -------------------------------- ### Installing pybind11 on MacOS with Homebrew Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Compilation.rst This command installs pybind11 on MacOS using Homebrew. pybind11 is a key dependency for SofaPython3, facilitating the automatic generation of binding code between SOFA and Python. ```Shell brew install pybind11 ``` -------------------------------- ### Setting SofaPython3 Build Environment Variables (Bash) Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Compilation.rst These commands define environment variables `SP3_SRC`, `SP3_BUILD`, and `SP3_ROOT` for SofaPython3. They specify the locations for the source code, build files, and installation directory, respectively, facilitating an organized out-of-tree build. ```bash export SP3_SRC=/opt/SofaPython3/src export SP3_BUILD=/opt/SofaPython3/build export SP3_ROOT=/opt/SofaPython3/build/install ``` -------------------------------- ### Installing NumPy on MacOS BigSur Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst For MacOS BigSur users, these commands first upgrade pip and then install the NumPy library using the Python 3.10 interpreter, fulfilling a necessary dependency for SofaPython3. ```bash pip3 install --upgrade pip python3.10 -m pip install numpy ``` -------------------------------- ### Specifying Python Package Link Directory (CMake) Source: https://github.com/sofa-framework/sofapython3/blob/master/README.md These commands configure and execute the installation of SofaPython3 bindings, explicitly setting the `SP3_PYTHON_PACKAGES_LINK_DIRECTORY` CMake variable. This ensures that symbolic links to the installed bindings are created in a specific system-wide Python package directory, making them available to all users. ```Shell cmake -DSP3_PYTHON_PACKAGES_LINK_DIRECTORY=/usr/lib/python3.8/dist-packages . cmake --install . ``` -------------------------------- ### Creating and Configuring a Floor Object in SOFA Python Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This snippet creates a 'floor' child node and adds core mechanical components. It includes a MechanicalObject for rigid body state with a specific translation, and UniformMass for mass properties, similar to the sphere setup. ```Python floor = rootNode.addChild("floor") floor.addObject('MechanicalObject', name="mstate", template="Rigid3", translation2=[0.0,-300.0,0.0], rotation2=[0., 0., 0.], showObjectScale=5.0) floor.addObject('UniformMass', name="mass", vertexMass=[totalMass, volume, inertiaMatrix[:]]) ``` -------------------------------- ### Creating SOFA Scene with Sphere Mechanical and Visual Models Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/FirstSteps.rst This `createScene` function extends the basic setup by adding a sphere object with both mechanical and visual representations. It loads additional plugins for state containers, mesh I/O, non-linear mapping, and animation. The sphere is defined with a `MechanicalObject` for its rigid body state and a `VisualModel` that loads an OBJ mesh and applies an `OglModel` for rendering, linked via `RigidMapping`. ```python def createScene(rootNode): import SofaRuntime SofaRuntime.importPlugin("Sofa.Component.Visual") SofaRuntime.importPlugin("Sofa.GL.Component.Rendering3D") SofaRuntime.importPlugin("Sofa.Component.StateContainer") SofaRuntime.importPlugin("Sofa.Component.IO.Mesh") SofaRuntime.importPlugin("Sofa.Component.Mapping.NonLinear") SofaRuntime.importPlugin("Sofa.Component.AnimationLoop") rootNode.addObject("VisualGrid", nbSubdiv=10, size=1000) rootNode.addObject("DefaultAnimationLoop") confignode = rootNode.addChild("Config") confignode.addObject('OglSceneFrame', style="Arrows", alignment="TopRight") sphere = rootNode.addChild("sphere") sphere.addObject('MechanicalObject', name="mstate", template="Rigid3", translation2=[0., 0., 0.], rotation2=[0., 0., 0.], showObjectScale=50) sphereVisu = sphere.addChild("VisualModel") sphereVisu.loader = sphereVisu.addObject('MeshOBJLoader', name="loader", filename="mesh/ball.obj") sphereVisu.addObject('OglModel', name="model", src="@loader", scale3d=[50]*3, color=[0., 1., 0.], updateNormals=False) sphereVisu.addObject('RigidMapping') ``` -------------------------------- ### Setting SOFA Environment Variables on Ubuntu Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst These commands set the `SOFA_ROOT` and `PYTHONPATH` environment variables on Ubuntu, which are crucial for a Python 3 interpreter to correctly locate SOFA installations and SofaPython3 libraries. ```bash export SOFA_ROOT=/path/to/SOFA_install export PYTHONPATH=/path/to/SofaPython3/lib/python3/site-packages:$PYTHONPATH ``` -------------------------------- ### Setting SOFA_ROOT Environment Variable (Shell) Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/README.md Sets the `SOFA_ROOT` environment variable to the installation path of SOFA. This variable is essential for locating SOFA build artifacts and plugins, which SofaPython3 depends on for its functionality and bindings. ```shell export SOFA_ROOT=/opt/sofa_v20.12/build/install ``` -------------------------------- ### Adding SofaPython3 Dependencies to Windows PATH (PowerShell) Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Compilation.rst This PowerShell command modifies the system's PATH environment variable by appending the installation directories for SOFA, pybind11, and Python. This ensures that the system can locate these dependencies during the SofaPython3 compilation process on Windows. ```PowerShell PS $env:Path += \"C:\\sofa;C:\\pybind11;C:\\python\" ``` -------------------------------- ### Exporting SOFA_ROOT Environment Variable (Bash) Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/Compilation.rst This command sets the `SOFA_ROOT` environment variable to the installation path of SOFA. This variable is crucial for the out-of-tree compilation of SofaPython3, allowing CMake to locate SOFA's build dependencies. ```bash export SOFA_ROOT="/home/user/sofa/build/master/install" ``` -------------------------------- ### Configuring Local Minimum Distance Collision Detection Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This line adds a 'LocalMinDistance' object to the root node, which is a collision detection algorithm. It sets parameters for alarm distance (when collision detection starts), contact distance (when objects are considered in contact), and angle cone for collision response. ```python rootNode.addObject('LocalMinDistance', alarmDistance=10, contactDistance=5, angleCone=0.01) ``` -------------------------------- ### Writing SOFA Data with .value Accessor (Python) Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/FirstSteps.rst This example demonstrates how to modify SOFA component data using the `.value` accessor. It initializes a scene, runs a simulation for 10 steps, prints the sphere's position, then sets the world gravity to zero, and runs another 10 steps, showing the effect of data modification on simulation behavior. This method is suitable for simple data types like scalars and vectors. ```Python def main(): # Call the SOFA function to create the root node root = Sofa.Core.Node("root") # Call the createScene function, as runSofa does createScene(root) # Once defined, initialization of the scene graph Sofa.Simulation.initRoot(root) # Run the simulation for 10 steps for iteration in range(10): Sofa.Simulation.animate(root, root.dt.value) # Print the position of the falling sphere print(root.sphere.mstate.position.value) # Increase the gravity root.gravity.value = [0, 0, 0] # Run the simulation for 10 steps MORE for iteration in range(10): Sofa.Simulation.animate(root, root.dt.value) # Print the position of the falling sphere print(root.sphere.mstate.position.value) ``` -------------------------------- ### Configuring Python Executable Path (CMake) Source: https://github.com/sofa-framework/sofapython3/blob/master/README.md This CMake command sets the `Python_EXECUTABLE` variable to a specific Python 3 interpreter path. This ensures that the SofaPython3 plugin and its bindings are compiled against the desired Python version, which is particularly useful when multiple Python installations are present on the system. ```Shell cmake -DPython_EXECUTABLE=/usr/local/bin/python3 .. ``` -------------------------------- ### Initializing and Simulating a Basic SOFA Scene in Python Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This snippet illustrates the fundamental steps for setting up and running a SOFA simulation. It covers creating a root node, initializing the scene, running animation steps, modifying scene properties like gravity, and accessing component data such as sphere position. ```python # Call the SOFA function to create the root node root = Sofa.Core.Node("root") # Call the createScene function, as runSofa does createScene(root) # Once defined, initialization of the scene graph Sofa.Simulation.init(root) # Run the simulation for 10 steps for iteration in range(10): Sofa.Simulation.animate(root, root.dt.value) # Print the position of the falling sphere print(root.sphere.mstate.position.value) # Increase the gravity root.gravity.value = [0, 0, 0] # Run the simulation for 10 steps MORE for iteration in range(10): Sofa.Simulation.animate(root, root.dt.value) # Print the position of the falling sphere print(root.sphere.mstate.position.value) ``` -------------------------------- ### Initializing SOFA Visual Components and Grid (Basic) Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/FirstSteps.rst This snippet initializes essential SOFA components by importing `SofaRuntime` and loading the `Sofa.Component.Visual` plugin. It then adds a `VisualGrid` object to the `rootNode`, configuring its subdivisions (`nbSubdiv`) and size. This sets up the basic visual environment for a SOFA scene. ```python import SofaRuntime SofaRuntime.importPlugin("Sofa.Component.Visual") rootNode.addObject("VisualGrid", nbSubdiv=10, size=1000) ``` -------------------------------- ### Setting Up SOFA Scene with Sphere and Floor (Python) Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/FirstSteps.rst This snippet demonstrates the creation and configuration of a 3D scene in SOFA, including a sphere and a floor. It defines their visual models, mechanical properties, and collision behaviors by adding various SOFA components like `MeshOBJLoader`, `OglModel`, `MechanicalObject`, and collision models. It also includes the standard Python entry point for script execution. ```Python sphereVisu = sphere.addChild("VisualModel") sphereVisu.loader = sphereVisu.addObject('MeshOBJLoader', name="loader", filename="mesh/ball.obj") sphereVisu.addObject('OglModel', name="model", src="@loader", scale3d=[50]*3, color=[0., 1., 0.], updateNormals=False) sphereVisu.addObject('RigidMapping') # Creating the floor object floor = rootNode.addChild("floor") floor.addObject('MechanicalObject', name="mstate", template="Rigid3", translation2=[0.0,-300.0,0.0], rotation2=[0., 0., 0.], showObjectScale=5.0) floor.addObject('UniformMass', name="mass", vertexMass=[totalMass, volume, inertiaMatrix[:]]) #### Collision subnode for the floor floorCollis = floor.addChild('collision') floorCollis.addObject('MeshOBJLoader', name="loader", filename="mesh/floor.obj", triangulate="true", scale=5.0) floorCollis.addObject('MeshTopology', src="@loader") floorCollis.addObject('MechanicalObject') floorCollis.addObject('TriangleCollisionModel', moving=False, simulated=False) floorCollis.addObject('LineCollisionModel', moving=False, simulated=False) floorCollis.addObject('PointCollisionModel', moving=False, simulated=False) floorCollis.addObject('RigidMapping') #### Visualization subnode for the floor floorVisu = floor.addChild("VisualModel") floorVisu.loader = floorVisu.addObject('MeshOBJLoader', name="loader", filename="mesh/floor.obj") floorVisu.addObject('OglModel', name="model", src="@loader", scale3d=[5.0]*3, color=[1., 1., 0.], updateNormals=False) floorVisu.addObject('RigidMapping') return rootNode # Function used only if this script is called from a python environment if __name__ == '__main__': main() ``` -------------------------------- ### Configuring Floor Visualization Model in SOFA Python Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This snippet sets up the visual representation for the floor. It loads the 'floor.obj' mesh, creates an OglModel for rendering with specified scale and color, and uses a RigidMapping to link the visual model to the floor's mechanical state. ```Python floorVisu = floor.addChild("VisualModel") floorVisu.loader = floorVisu.addObject('MeshOBJLoader', name="loader", filename="mesh/floor.obj") floorVisu.addObject('OglModel', name="model", src="@loader", scale3d=[5.0]*3, color=[1., 1., 0.], updateNormals=False) floorVisu.addObject('RigidMapping') ``` -------------------------------- ### Running SOFA Simulation in Python Interpreter (CLI) - Python Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This comprehensive snippet demonstrates how to run a SOFA simulation directly from a Python 3 interpreter. It includes importing `Sofa`, creating a root node, calling `createScene`, initializing the simulation graph, and running a fixed number of simulation steps without a GUI. It also re-defines the basic `createScene` function. ```python # Required import for SOFA within python import Sofa def main(): # Call the SOFA function to create the root node root = Sofa.Core.Node("root") # Call the createScene function, as runSofa does createScene(root) # Once defined, initialization of the scene graph Sofa.Simulation.init(root) # Run as many simulation steps (here 10 steps are computed) for iteration in range(10): Sofa.Simulation.animate(root, root.dt.value) # Same createScene function as in the previous case def createScene(rootNode): #Doesn't do anything yet return rootNode # Function used only if this script is called from a python environment if __name__ == '__main__': main() ``` -------------------------------- ### Running SOFA Simulation with GUI in Python Interpreter - Python Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This `main` function variant shows how to launch a SOFA simulation with a graphical user interface (GUI) when running from a Python interpreter. It initializes the GUI manager, creates the GUI window, sets its dimensions, and enters the main event loop to manage the simulation interactively. ```python def main(): # Call the SOFA function to create the root node root = Sofa.Core.Node("root") # Call the createScene function, as runSofa does createScene(root) # Once defined, initialization of the scene graph Sofa.Simulation.init(root) # Launch the GUI (qt or qglviewer) Sofa.Gui.GUIManager.Init("myscene", "qglviewer") Sofa.Gui.GUIManager.createGUI(root, __file__) Sofa.Gui.GUIManager.SetDimension(1080, 800) # Initialization of the scene will be done here Sofa.Gui.GUIManager.MainLoop(root) Sofa.Gui.GUIManager.closeGUI() ``` -------------------------------- ### Defining createScene for runSofa Execution - Python Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This snippet defines the `createScene` function, which serves as the entry point for SOFA simulations when loaded within the `runSofa` executable. It takes the `rootNode` as a parameter and returns it, initially performing no additional actions. ```python def createScene(rootNode): #Doesn't do anything yet return rootNode ``` -------------------------------- ### Importing Core SOFA Components in Python Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This Python snippet demonstrates how to initialize `SofaRuntime` and import the `Sofa.Component` plugin, which loads most common SOFA objects, along with `Sofa.Core` for creating fundamental SOFA elements like nodes. ```python # 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 ``` -------------------------------- ### Defining the SOFA Scene Entry Point (Python) Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/FirstSteps.rst This snippet defines the `createScene` function, which serves as the primary entry point for SOFA simulations loaded by `runSofa` or a Python interpreter. It takes a `Sofa.Core.Node` as its root parameter and is expected to return the configured root node, even if it initially does nothing. ```python def createScene(rootNode): #Doesn't do anything yet return rootNode ``` -------------------------------- ### Executing SOFA Simulation with Python Interpreter and GUI Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/FirstSteps.rst This Python script modifies the `main()` function to launch the SOFA simulation with a graphical user interface (GUI). It initializes the root node, calls `createScene()`, initializes the scene, and then uses `Sofa.Gui` to manage the GUI lifecycle, allowing interactive control of the simulation. ```python def main(): # Call the SOFA function to create the root node root = Sofa.Core.Node("root") # Call the createScene function, as runSofa does createScene(root) # Once defined, initialization of the scene graph Sofa.Simulation.initRoot(root) # Import the GUI package import Sofa.Gui # Launch the GUI (qt or qglviewer) Sofa.Gui.GUIManager.Init("myscene", "qglviewer") Sofa.Gui.GUIManager.createGUI(root, __file__) Sofa.Gui.GUIManager.SetDimension(1080, 800) # Initialization of the scene will be done here Sofa.Gui.GUIManager.MainLoop(root) Sofa.Gui.GUIManager.closeGUI() ``` -------------------------------- ### Configuring SOFA Scene with Visuals and Axes Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/FirstSteps.rst This `createScene` function sets up a basic SOFA environment. It imports necessary plugins like `Sofa.Component.Visual` and `Sofa.GL.Component.Rendering3D`, adds a `VisualGrid` for scene visualization, and creates a child node named "Config" to add an `OglSceneFrame` for displaying coordinate axes. ```python def createScene(rootNode): import SofaRuntime SofaRuntime.importPlugin("Sofa.Component.Visual") SofaRuntime.importPlugin("Sofa.GL.Component.Rendering3D") rootNode.addObject("VisualGrid", nbSubdiv=10, size=1000) confignode = rootNode.addChild("Config") confignode.addObject('OglSceneFrame', style="Arrows", alignment="TopRight") ``` -------------------------------- ### Opening Python Scene Files with runSofa Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This command demonstrates how to launch the `runSofa` application and load a SOFA scene file written in Python (e.g., with `.py`, `.py3`, `.pyscn`, or `.pyscn3` extensions) by specifying its path. ```bash /bin/runSofa ``` -------------------------------- ### Running a Sofa Scene with Python Source: https://github.com/sofa-framework/sofapython3/blob/master/README.md This command line snippet shows how to execute a minimal Sofa scene using the Python 3 interpreter. It illustrates the ability to run Sofa scenes as standard Python scripts. ```Python python3 minimalscene.py ``` -------------------------------- ### Configuring Sphere Visualization Model in SOFA Python Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This snippet sets up the visual representation for the sphere. It loads the 'ball.obj' mesh, creates an OglModel for rendering with specified scale and color, and uses a RigidMapping to link the visual model to the sphere's mechanical state. ```Python sphereVisu = sphere.addChild("VisualModel") sphereVisu.loader = sphereVisu.addObject('MeshOBJLoader', name="loader", filename="mesh/ball.obj") sphereVisu.addObject('OglModel', name="model", src="@loader", scale3d=[50]*3, color=[0., 1., 0.], updateNormals=False) sphereVisu.addObject('RigidMapping') ``` -------------------------------- ### Executing SOFA Simulation with Python Interpreter Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/FirstSteps.rst This Python script demonstrates how to run a SOFA simulation directly from a Python interpreter. It manually creates the root node, calls `createScene()`, initializes the scene graph, and runs a fixed number of simulation steps without a GUI. It requires the SOFA Python modules to be set up in the environment. ```python # Required import for SOFA within python import Sofa def main(): # Call the SOFA function to create the root node root = Sofa.Core.Node("root") # Call the createScene function, as runSofa does createScene(root) # Once defined, initialization of the scene graph Sofa.Simulation.initRoot(root) # Run as many simulation steps (here 10 steps are computed) for iteration in range(10): Sofa.Simulation.animate(root, root.dt.value) print("Computing iteration "+str(iteration+1)) print("Computation is done.") # Same createScene function as in the previous case def createScene(rootNode): #Doesn't do anything yet return rootNode # Function used only if this script is called from a python environment if __name__ == '__main__': main() ``` -------------------------------- ### Auto-loading SofaPython3 Plugin in runSofa Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This configuration line, when added to the `plugin_list.conf` file in the SOFA build directory, enables `runSofa` to automatically load the SofaPython3 plugin upon startup, allowing it to open Python scene files. ```bash SofaPython3 NO_VERSION ``` -------------------------------- ### Executing SOFA Simulation with runSofa (Shell) Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/FirstSteps.rst This command demonstrates how to load and execute a Python-based SOFA simulation directly using the `runSofa` executable. It expects a Python script containing a `createScene()` function, which serves as the simulation's entry point. ```shell runSofa examples/example1.py ``` -------------------------------- ### Disabling Automatic User Site Linking (CMake) Source: https://github.com/sofa-framework/sofapython3/blob/master/README.md This CMake command disables the automatic creation of symbolic links to the Python user site-packages directory during the SofaPython3 installation. This option is useful when manual control over the binding's location or linking is desired, preventing default behavior. ```Shell cmake -DSP3_LINK_TO_USER_SITE=OFF . ``` -------------------------------- ### Modifying Complex SOFA Data with writeableArray in Python Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This example demonstrates how to safely access and modify complex SOFA Data types, such as Deriv (degrees of freedom related data), using the .writeableArray() accessor. This method provides a context manager for direct manipulation of the underlying data array. ```python with root.sphere.CFF.totalForce.writeableArray() as wa: wa[0] += 0.01 # modify the first entry of the Deriv Data "totalForce" ``` -------------------------------- ### Verifying Sofa Bindings Availability (Python) Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/README.md Executes a Python command to check if the 'Sofa' module can be successfully imported. This serves as a quick verification step to ensure that the `PYTHONPATH` and `SOFA_ROOT` are correctly configured and Sofa bindings are accessible. ```python python -c "import importlib;print('OK') if importlib.util.find_spec('Sofa') else print('NOT OK');" ``` -------------------------------- ### Writing Complex SOFA Data with .writeableArray() (Python) Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/FirstSteps.rst This snippet shows how to modify more complex SOFA data structures, such as those related to degrees of freedom, using the `.writeableArray()` accessor. It provides an example of accessing and preparing to modify the `totalForce` data of a `ConstantForceField` component within a SOFA node, ensuring proper handling for array-like data. ```Python with root.sphere.CFF.totalForce.writeableArray() as wa: ``` -------------------------------- ### Adding a Static Floor Object with Collision and Visuals in SOFA (Python) Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This snippet creates a static floor object in the SOFA scene, including its mechanical properties, collision model, and visual representation. It defines the floor's initial position and mass, loads its geometry from an OBJ file for collision detection (using 'TriangleCollisionModel', 'LineCollisionModel', 'PointCollisionModel'), and sets up its visual rendering using 'OglModel'. The floor is configured as non-moving and non-simulated for collision purposes. ```Python # Creating the floor object floor = rootNode.addChild("floor") floor.addObject('MechanicalObject', name="mstate", template="Rigid3", translation2=[0.0,-300.0,0.0], rotation2=[0., 0., 0.], showObjectScale=5.0) floor.addObject('UniformMass', name="mass", vertexMass=[totalMass, volume, inertiaMatrix[:]]) #### Collision subnode for the floor floorCollis = floor.addChild('collision') floorCollis.addObject('MeshOBJLoader', name="loader", filename="mesh/floor.obj", triangulate="true", scale=5.0) floorCollis.addObject('MeshTopology', src="@loader") floorCollis.addObject('MechanicalObject') floorCollis.addObject('TriangleCollisionModel', moving=False, simulated=False) floorCollis.addObject('LineCollisionModel', moving=False, simulated=False) floorCollis.addObject('PointCollisionModel', moving=False, simulated=False) floorCollis.addObject('RigidMapping') #### Visualization subnode for the floor floorVisu = floor.addChild("VisualModel") floorVisu.loader = floorVisu.addObject('MeshOBJLoader', name="loader", filename="mesh/floor.obj") floorVisu.addObject('OglModel', name="model", src="@loader", scale3d=[5.0]*3, color=[1., 1., 0.], updateNormals=False) floorVisu.addObject('RigidMapping') ``` -------------------------------- ### Configuring Sphere Physics with Solvers and Mass in SOFA Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/FirstSteps.rst This snippet configures the physical behavior of the sphere object in SOFA. It imports necessary plugins for ODE solvers, linear solvers, mass, and constraint correction. It then adds an `EulerImplicitSolver` for time integration, a `CGLinearSolver` for solving equations, a `UniformMass` component, and an `UncoupledConstraintCorrection` to the sphere node, enabling it to simulate realistic physical interactions. ```python SofaRuntime.importPlugin("Sofa.Component.ODESolver.Backward") SofaRuntime.importPlugin("Sofa.Component.LinearSolver.Iterative") SofaRuntime.importPlugin("Sofa.Component.Mass") SofaRuntime.importPlugin("Sofa.Component.Constraint.Lagrangian.Correction") sphere = rootNode.addChild("sphere") sphere.addObject('EulerImplicitSolver', name='odesolver') sphere.addObject('CGLinearSolver', name='Solver', iterations=25, tolerance=1e-05, threshold=1e-05) sphere.addObject('MechanicalObject', name="mstate", template="Rigid3", translation2=[0., 0., 0.], rotation2=[0., 0., 0.], showObjectScale=50) sphere.addObject('UniformMass', name="mass", vertexMass=[totalMass, volume, inertiaMatrix[:]]) sphere.addObject('UncoupledConstraintCorrection') ``` -------------------------------- ### Adding Configuration Node and Scene Frame - Python Source: https://github.com/sofa-framework/sofapython3/blob/master/docs/sphinx/source/content/UsingThePlugin.rst This snippet demonstrates how to add a child node named 'Config' to the `rootNode` to organize scene-wide configurations. It then adds an `OglSceneFrame` component to this new node, which displays a visual coordinate system (axes) in the top-right corner of the scene. ```python confignode = rootNode.addChild("Config") confignode.addObject('OglSceneFrame', style="Arrows", alignment="TopRight") ```