### Create and Get Path Name Example Source: https://sofapython3.readthedocs.io/en/latest/content/modules/Sofa/generated/Sofa.Core/classes/Sofa.Core.Base Demonstrates creating a Sofa node and adding an object, then retrieving the path name of the added object. This example illustrates object creation and path verification in Sofa. ```python a = Sofa.Core.Node("root") b = a.addObject("Camera", name="camera") b.getPathName() # should returns "/root/camera" ``` -------------------------------- ### Full SOFA Scene Initialization and GUI Setup in Python Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/_sources/content/UsingThePlugin.rst This comprehensive Python script initializes a SOFA scene, sets up the root node with gravity and time step, and loads necessary SOFA modules. It also configures the collision pipeline and sets up the graphical user interface (GUI) for visualization and interaction. This serves as a complete example for creating and running a basic 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.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') ``` -------------------------------- ### Sofa Node and Context API Methods (Python) Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/content/modules/Sofa/generated/Sofa.Core/classes/Sofa.Core.Node Provides Python examples for various API methods related to Sofa nodes and contexts. This includes getting node paths, root nodes, contexts, and template names, as well as checking object existence and initialization status. ```python getPathName(_self : Sofa.Core.Node_) -> str getRoot(_self : Sofa.Core.Node_) -> object getRootContext(_self : Sofa.Core.BaseContext_) -> Sofa.Core.BaseContext getRootPath(_self : Sofa.Core.Node_) -> str getState(_self : Sofa.Core.BaseContext_) -> sofa::core::BaseState getTemplateName(_self : Sofa.Core.Base_) -> str getTime(_self : Sofa.Core.BaseContext_) -> float getTopology(_self : Sofa.Core.BaseContext_) -> sofa::core::topology::Topology hasObject(_self : Sofa.Core.Node_, _arg0 : str_) -> object init(_self : Sofa.Core.Node_) -> None isActive(_self : Sofa.Core.BaseContext_) -> bool isInitialized(_self : Sofa.Core.Node_) -> bool isSleeping(_self : Sofa.Core.BaseContext_) -> bool ``` -------------------------------- ### Python: Sofa.Core.Link Example Usage Source: https://sofapython3.readthedocs.io/en/latest/content/modules/Sofa/generated/Sofa.Core/classes/Sofa.Core.Link Demonstrates how to create, find, and interact with Sofa.Core.Link objects in a Python script. This example assumes SofaRuntime and necessary plugins are imported and a basic SOFA node structure is set up. ```python import Sofa.Core import SofaRuntime SofaRuntime.importPlugin("SofaComponentAll") root = Sofa.Core.Node("root") root.addObject("MechanicalObject", name="t") # Assuming a link to 't' exists or can be created # For demonstration, let's assume a link named 'mechanicalState' # In a real scenario, this link would be established between components. # link = root.findLink("mechanicalState") # This line would work if the link was predefined # Placeholder for demonstration if 'findLink' doesn't directly work without prior setup # If you have a Data object, you can create a link to it. # For example, if 't' had a 'position' Data field: # from Sofa.Core import Data # my_data = root.getObject("MechanicalObject").position # link = Sofa.Core.Link(my_data) # Simplified, actual link creation might be more complex # Example usage of link methods (assuming 'link' is a valid Sofa.Core.Link object): # print(link.getValueString()) # will print the value string of the linked data # print(link.getHelp()) # will print the help message for the linked data ``` -------------------------------- ### Install OpenGL Library on Ubuntu Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/_sources/content/UsingThePlugin.rst Command to install the OpenGL library on Ubuntu, which may be required for launching runSofa. ```bash sudo apt install libopengl0 ``` -------------------------------- ### Cloning, Configuring, Building, and Installing SofaPython3 Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/_sources/content/Compilation.rst This sequence of commands clones the SofaPython3 repository, configures the build using CMake with specific paths and build type, builds the plugin, and then installs it. This is part of the out-of-tree build process. ```bash 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 ``` -------------------------------- ### Out-of-tree Build: Cloning, Configuring, and Building SofaPython3 Source: https://sofapython3.readthedocs.io/en/latest/content/Compilation This sequence of commands clones the SofaPython3 repository, configures the build using CMake with specified paths and types, builds the project, and installs it. It requires SOFA to be installed and its path set in SOFA_ROOT. ```bash $ 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 ``` -------------------------------- ### Camera Class Methods: Initialization and View Setup in Python Source: https://sofapython3.readthedocs.io/en/latest/content/modules/Sofa/generated/Sofa.Core/classes/Sofa.Core.MassRigid3d This snippet covers initialization and view setup methods for the Camera class in Python. It includes functions for creating orthographic and perspective projections, adding data and links, and managing camera initialization and cleanup. ```python class Camera: def Orthographic(self): # ... implementation ... pass def Perspective(self): # ... implementation ... pass def addData(self, data): # ... implementation ... pass def addLink(self, link): # ... implementation ... pass def addSlave(self, slave): # ... implementation ... pass def bwdInit(self): # ... implementation ... pass def cleanup(self): # ... implementation ... pass def clearLoggedMessages(self): # ... implementation ... pass def computeBBox(self): # ... implementation ... pass def countLoggedMessages(self): # ... implementation ... pass def findData(self, name): # ... implementation ... pass def findLink(self, name): # ... implementation ... pass def getAsACreateObjectParameter(self): # ... implementation ... pass def getCategories(self): # ... implementation ... pass def getClass(self): # ... implementation ... pass def getClassName(self): # ... implementation ... pass def getContext(self): # ... implementation ... pass def getData(self, name): # ... implementation ... pass def getDataFields(self): # ... implementation ... pass def getDefinitionSourceFileName(self): # ... implementation ... pass def getDefinitionSourceFilePos(self): # ... implementation ... pass def getInstanciationFileName(self): # ... implementation ... pass def getInstanciationSourceFilePos(self): # ... implementation ... pass def getLinkPath(self): # ... implementation ... pass def getLinks(self): # ... implementation ... pass def getLoggedMessagesAsString(self): # ... implementation ... pass def getLookAtFromOrientation(self): # ... implementation ... pass def getMaster(self): # ... implementation ... pass def getModelViewMatrix(self): # ... implementation ... pass def getName(self): # ... implementation ... pass def getOpenGLModelViewMatrix(self): # ... implementation ... pass def getOpenGLProjectionMatrix(self): # ... implementation ... pass def getOrientationFromLookAt(self): # ... implementation ... pass def getPathName(self): # ... implementation ... pass def getPositionFromOrientation(self): # ... implementation ... pass def getProjectionMatrix(self): # ... implementation ... pass def getSlaves(self): # ... implementation ... pass def getTarget(self): # ... implementation ... pass def getTemplateName(self): # ... implementation ... pass def init(self): # ... implementation ... pass def reinit(self): # ... implementation ... pass def reset(self): # ... implementation ... pass def rotate(self, angle, axis): # ... implementation ... pass def rotateCameraAroundPoint(self, angle, axis, point): # ... implementation ... pass def rotateWorldAroundPoint(self, angle, axis, point): # ... implementation ... pass def screenToWorldPoint(self, screen_pos): # ... implementation ... pass def setCameraType(self, camera_type): # ... implementation ... pass def setDataValues(self, data_values): # ... implementation ... pass def setDefaultView(self): # ... implementation ... pass def setDefinitionSourceFileName(self, filename): # ... implementation ... pass def setDefinitionSourceFilePos(self, filepos): # ... implementation ... pass def setInstanciationSourceFileName(self, filename): # ... implementation ... pass def setInstanciationSourceFilePos(self, filepos): # ... implementation ... pass def setName(self, name): # ... implementation ... pass def setSrc(self, src_path): # ... implementation ... pass def storeResetState(self): # ... implementation ... pass ``` -------------------------------- ### Initialize and Access Camera Properties in Python Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/content/modules/Sofa/generated/_autosummary/_autosummary/Sofa.Core.Camera Demonstrates how to import necessary modules, initialize the SOFA runtime, create a root node, add a camera object, and access its position and orientation properties. ```python import Sofa.Core import Sofa.Runtime SofaRuntime.importPlugin("SofaComponentAll") root = Sofa.Core.Node("root") root.addObject("Camera", name="c") # Access the position of the camera print(root.c.position.value) # Access the orientation of the camera in quaternion print(root.c.orientation.value) ``` -------------------------------- ### Install Python and Libraries (Ubuntu) Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/_sources/content/Installation.rst Installs Python 3.8, pip, and necessary libraries like NumPy on Ubuntu systems. It also provides a command to install OpenGL for runSofa. ```bash sudo add-apt-repository ppa:deadsnakes/ppa sudo apt install libpython3.8 python3.8 python3-pip python3.8 -m pip install numpy sudo apt install libopengl0 ``` -------------------------------- ### Executing a SOFA Simulation with runSofa Source: https://sofapython3.readthedocs.io/en/latest/content/FirstSteps This command initiates a SOFA simulation by loading a Python script named 'example.py' using the runSofa executable. Ensure the SofaPython3 plugin is loaded for this to work. The script 'example.py' must define a `createScene(root)` function, which is the entry point for building the SOFA scene graph. ```shell runSofa example.py ``` -------------------------------- ### Install Python and Libraries (macOS) Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/_sources/content/Installation.rst Installs Python 3.8 using Homebrew on macOS and configures the PATH environment variable. Includes commands for installing NumPy for BigSur and Catalina systems. ```bash brew install python@3.8 export PATH="/usr/local/opt/python@3.8/bin/:$PATH" pip3 install --upgrade pip python3.8 -m pip install numpy pip3 install numpy ``` -------------------------------- ### Setup SofaPython3 in runSofa Source: https://sofapython3.readthedocs.io/en/latest/content/Compilation Demonstrates multiple methods to load a SofaPython3 plugin built out-of-tree into the runSofa application. These include using the '-l' flag, setting the SOFA_PLUGIN_PATH environment variable, adding a repository to the scene file, or using the Plugin Manager within runSofa. ```bash # Using the -l flag: runSofa -l /path/to/SofaPython3_build/lib/libSofaPython3.so # Using SOFA_PLUGIN_PATH environment variable: export SOFA_PLUGIN_PATH=/path/to/SofaPython3_build/ runSofa # Adding to scene file: # ``` -------------------------------- ### Set Environment Variables (Windows) Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/_sources/content/Installation.rst Configures SOFA_ROOT, PYTHON_ROOT, PYTHONPATH, and the system Path variable on Windows. Also includes commands to verify Python version and install NumPy and SciPy. ```powershell * Create a system variable **SOFA_ROOT** and set it to ```` * Create a system variable **PYTHON_ROOT** and set it to ```` * Create a system variable **PYTHONPATH** and set it to ``%SOFA_ROOT%\plugins\SofaPython3\lib\python3\site-packages`` * Edit the system variable **Path** and add at the end ``;%PYTHON_ROOT%;%PYTHON_ROOT%\DLLs;%PYTHON_ROOT%\Lib;%SOFA_ROOT%\bin;`` Open a Console (cmd.exe) and run ``python -V && python -m pip install numpy scipy`` runSofa -lSofaPython3 ``` -------------------------------- ### Launch SOFA GUI with ImGui from Python Source: https://sofapython3.readthedocs.io/en/latest/content/FirstSteps This snippet demonstrates how to initialize and launch the SOFA ImGui (Immediate Mode Graphical User Interface) from a Python script. It sets up the scene graph, imports the necessary GUI packages, initializes the GUI manager, and starts the main simulation loop. Dependencies include the SOFA core library and the ImGui package. ```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 SofaImGui import Sofa.Gui # Launch the GUI (imgui is now by default, to use Qt please refer to the example "basic-useQtGui.py") Sofa.Gui.GUIManager.Init("myscene", "imgui") 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() ``` -------------------------------- ### Set Environment Variables (Linux/macOS) Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/_sources/content/Installation.rst Sets the SOFA_ROOT and PYTHONPATH environment variables in the terminal for Linux and macOS. This is necessary for Python to locate SOFA components and plugins. ```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.8/bin/:$PATH" ``` -------------------------------- ### Create Scene Setup with Visual Grid Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/content/UsingThePlugin Initializes the scene by adding a visual grid for clarity and a configuration node for plugins and scene elements. This sets up the basic structure of the SOFA scene. ```python def createScene(rootNode): rootNode.addObject("VisualGrid", nbSubdiv=10, size=1000) confignode = rootNode.addChild("Config") confignode.addObject('OglSceneFrame', style="Arrows", alignment="TopRight") ``` -------------------------------- ### Import SOFA Components in Python Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/_sources/content/Installation.rst Python code snippet demonstrating how to import necessary SOFA modules (SofaRuntime, Sofa.Component, Sofa.Core) to interact with SOFA objects and run simulations. ```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 ``` -------------------------------- ### Full SOFA Scene Setup (Python) Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/content/FirstSteps Provides the complete Python code for setting up a SOFA scene. This includes importing SOFA modules, defining the main function to create the root node, calling a createScene function (not shown), initializing the simulation, and launching the graphical user interface. ```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) ``` -------------------------------- ### Initialize and Run SOFA Simulation Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/_sources/content/UsingThePlugin.rst This snippet demonstrates the basic workflow for setting up a SOFA simulation. It involves creating a root node, defining the scene graph, initializing the simulation, running it for a set number of steps, and printing the position of a sphere. It also shows how to modify gravity and rerun the simulation. ```python import Sofa # 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) ``` -------------------------------- ### Get Instantiation Source File Information in SofaPython3 Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/content/modules/Sofa/generated/Sofa.Core/classes/Sofa.Core.Node Retrieves the source file name and line number where a Sofa object was instantiated. This aids in tracking the creation of objects during simulation setup. ```python getInstanciationFileName(_self : Sofa.Core.Base_) → str Returns the line number where the object is instanciated. ``` ```python getInstanciationSourceFilePos(_self : Sofa.Core.Base_) → int Returns the line number where the object is instanciated. ``` -------------------------------- ### Python Example: Creating and Accessing a SOFA Link Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/content/modules/Sofa/generated/Sofa.Core/classes/Sofa.Core.Link Demonstrates how to create a Node, add a component with a mechanical object, find a link to that object, and retrieve its value or help message. This snippet requires the Sofa.Core and SofaRuntime modules, and the SofaComponentAll plugin. ```python import Sofa.Core import SofaRuntime SofaRuntime.importPlugin("SofaComponentAll") root = Sofa.Core.Node("root") root.addObject("MechanicalObject", name="t") link = root.findLink("mechanicalState") # access the link print(link.getValueString()) # will print '@/t' print(link.getHelp()) # will print the help message for all mechanical states ``` -------------------------------- ### Initialization and Re-initialization of MassRigid2d in Python Source: https://sofapython3.readthedocs.io/en/latest/content/modules/Sofa/generated/Sofa.Helper/functions/Sofa.Helper.msg_info init() performs the initial setup and configuration of the MassRigid2d object. reinit() re-initializes the object, often used after significant changes to the scene or simulation parameters. These are critical for starting and resetting simulations. ```python mass_rigid.init() ``` ```python mass_rigid.reinit() ``` -------------------------------- ### Get Object Path Name - Python Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/content/modules/Sofa/generated/Sofa.Core/classes/Sofa.Core.Context Retrieves the full path name of a Sofa object within the scene graph. This is useful for identifying and accessing specific nodes or objects in a hierarchical structure. The example demonstrates creating a node and then retrieving its path name. ```python a = Sofa.Core.Node("root") b = a.addObject("Camera", name="camera") b.getPathName() # should returns "/root/camera" ``` -------------------------------- ### Import SOFA Plugins and Initialize Simulation (Python) Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/_modules/Sofa This snippet demonstrates how to import SOFA plugins, create a node, add a mechanical object, and initialize the SOFA simulation. It requires the Sofa.Core, Sofa.Simulation, and SofaRuntime modules. ```python import Sofa.Core import Sofa.Simulation import SofaRuntime SofaRuntime.importPlugin("Sofa.Component") n = Sofa.Core.Node("MyNode") n.addChild("Node2") n.addObject("MechanicalObject", name="dofs") Sofa.Simulation.init(n) Sofa.Simulation.print(n) ``` -------------------------------- ### Install Python 3.8 and NumPy (macOS) Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/content/UsingThePlugin Commands to install Python 3.8 and NumPy on macOS. This includes setting the PATH environment variable to use the installed Python version. ```bash brew install python@3.8 export PATH="/usr/local/opt/python@3.8/bin/:$PATH" ``` -------------------------------- ### Install Python 3.12 and numpy on Ubuntu Source: https://sofapython3.readthedocs.io/en/latest/content/Installation Installs Python 3.12, pip, and the numpy library on Ubuntu systems. It also includes a command to install libopengl0 if runSofa is intended to be launched. ```bash sudo add-apt-repository ppa:deadsnakes/ppa sudo apt install libpython3.12 python3.12 python3-pip python3.12 -m pip install numpy sudo apt install libopengl0 ``` -------------------------------- ### Executing a SOFA Simulation with python3 Interpreter Source: https://sofapython3.readthedocs.io/en/latest/content/FirstSteps This command executes a Python script named 'example.py' using the python3 interpreter. Before running, ensure that the SOFA Python modules' path is added to your PYTHONPATH environment variable. The script should contain the `createScene` function and typically a `main` function for setup and simulation execution. ```shell python3 example.py ``` -------------------------------- ### Install Python 3.12 and numpy on macOS Source: https://sofapython3.readthedocs.io/en/latest/content/Installation Installs Python 3.12 using Homebrew and configures the PATH environment variable. It also installs numpy for Python 3.12, with specific instructions for BigSur and Catalina. ```bash brew install python@3.12 export PATH="/usr/local/opt/python@3.12/bin/:$PATH" pip3 install --upgrade pip python3.12 -m pip install numpy ``` -------------------------------- ### Initialize and Launch SOFA Scene with ImGui Source: https://sofapython3.readthedocs.io/en/latest/content/FirstSteps This Python code initializes a SOFA scene, sets up the GUI using ImGui, and launches the simulation. It defines the root node, calls a createScene function (not shown), initializes the simulation environment, and configures the GUI window dimensions. ```python import Sofa import SofaImGui 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 (imgui is now by default, to use Qt please refer to the example "basic-useQtGui.py") Sofa.Gui.GUIManager.Init("myscene", "imgui") Sofa.Gui.GUIManager.createGUI(root, __file__) Sofa.Gui.GUIManager.SetDimension(1080, 800) ``` -------------------------------- ### Install Python 3.8 and NumPy on macOS Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/_sources/content/UsingThePlugin.rst Commands to install Python 3.8 using Homebrew and install the NumPy library on macOS. Also includes setting the PATH environment variable. ```bash brew install python@3.8 export PATH="/usr/local/opt/python@3.8/bin/:$PATH" pip3 install --upgrade pip python3.8 -m pip install numpy ``` -------------------------------- ### Install NumPy (macOS BigSur) Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/content/UsingThePlugin Commands to upgrade pip and install NumPy for macOS BigSur. ```bash pip3 install --upgrade pip python3.8 -m pip install numpy ``` -------------------------------- ### Install NumPy on macOS (Catalina) Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/_sources/content/UsingThePlugin.rst Specific command to install the NumPy library on macOS Catalina. ```bash pip3 install numpy ``` -------------------------------- ### Initialize and Print Sofa Scene (Python) Source: https://sofapython3.readthedocs.io/en/latest/content/modules/Sofa/generated/Sofa.Simulation This code snippet demonstrates how to initialize a Sofa scene and print its structure using the Sofa.Simulation module. It requires importing Sofa.Core and SofaRuntime, and also importing the 'SofaComponentAll' plugin. The function takes a Sofa.Core.Node as input. ```python import Sofa.Core import SofaRuntime SofaRuntime.importPlugin("SofaComponentAll") n = Sofa.Core.Node("MyNode") Sofa.Simulation.init(n) Sofa.Simulation.print(n) ``` -------------------------------- ### Out-of-tree Build: Compiling and Installing SofaPython3 Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/content/Compilation This code snippet completes the out-of-tree build process for SofaPython3. It first builds the compiled targets using `cmake --build` and then installs the plugin to the location specified by `SP3_ROOT` using `cmake --install`. ```bash cmake --build $SP3_BUILD cmake --install $SP3_BUILD ``` -------------------------------- ### bwdInit Method Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/content/modules/Sofa/generated/Sofa.Core/classes/Sofa.Core.Object Documentation for the `bwdInit` method, responsible for bottom-up initialization during graph creation and modification. ```APIDOC ## bwdInit ### Description Initialization method called at graph creation and modification, during bottom-up traversal. ### Parameters - **self** (Sofa.Core.Object): The object to initialize. ### Returns - None ``` -------------------------------- ### Prefab Methods Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/content/modules/Sofa/generated/Sofa.Core/classes/Sofa.Core.Base This section details the methods available for interacting with Prefab objects, including getting and setting various properties and states. ```APIDOC ## Prefab Methods ### Description Provides methods for accessing and manipulating Prefab object properties, state, and associated data. ### Methods - **`Prefab.getLinks()`**: Retrieves links associated with the Prefab. - **`Prefab.getLoggedMessagesAsString()`**: Gets logged messages as a string. - **`Prefab.getMass()`**: Retrieves the mass of the Prefab. - **`Prefab.getMechanicalMapping()`**: Gets the mechanical mapping of the Prefab. - **`Prefab.getMechanicalState()`**: Retrieves the mechanical state. - **`Prefab.getMeshTopology()`**: Gets the mesh topology. - **`Prefab.getMeshTopologyLink()`**: Retrieves a specific mesh topology link. - **`Prefab.getName()`**: Gets the name of the Prefab. - **`Prefab.getObject()`**: Retrieves an object associated with the Prefab. - **`Prefab.getPathName()`**: Gets the full path name of the Prefab. - **`Prefab.getRoot()`**: Retrieves the root of the Prefab. - **`Prefab.getRootContext()`**: Gets the root context. - **`Prefab.getRootPath()`**: Retrieves the root path. - **`Prefab.getState()`**: Gets the current state of the Prefab. - **`Prefab.getTemplateName()`**: Retrieves the template name. - **`Prefab.getTime()`**: Gets the current time. - **`Prefab.getTopology()`**: Retrieves the topology information. - **`Prefab.hasObject()`**: Checks if the Prefab has a specific object. - **`Prefab.init()`**: Initializes the Prefab. - **`Prefab.isActive()`**: Checks if the Prefab is active. - **`Prefab.isInitialized()`**: Checks if the Prefab is initialized. - **`Prefab.isSleeping()`**: Checks if the Prefab is sleeping. - **`Prefab.moveChild()`**: Moves a child object. - **`Prefab.reinit()`**: Reinitializes the Prefab. - **`Prefab.removeChild()`**: Removes a child object. - **`Prefab.removeObject()`**: Removes an object. - **`Prefab.sendEvent()`**: Sends an event. - **`Prefab.setActive()`**: Sets the active state. - **`Prefab.setAnimate()`**: Sets the animation state. - **`Prefab.setChangeSleepingState()`**: Sets the sleeping state change. - **`Prefab.setDataValues()`**: Sets data values. - **`Prefab.setDefinitionSourceFileName()`**: Sets the definition source file name. - **`Prefab.setDefinitionSourceFilePos()`**: Sets the definition source file position. - **`Prefab.setDt()`**: Sets the delta time. - **`Prefab.setGravity()`**: Sets the gravity. - **`Prefab.setInstanciationSourceFileName()`**: Sets the instanciation source file name. - **`Prefab.setInstanciationSourceFilePos()`**: Sets the instanciation source file position. - **`Prefab.setName()`**: Sets the name of the Prefab. - **`Prefab.setSleeping()`**: Sets the sleeping state. - **`Prefab.setSourceTracking()`**: Sets the source tracking. ### Attributes - **`Prefab.objects`**: Accesses objects associated with the Prefab. - **`Prefab.parents`**: Accesses parent objects. ``` -------------------------------- ### Initialize SOFA Scene and GUI Source: https://sofapython3.readthedocs.io/en/latest/content/FirstSteps This snippet shows the initial steps to start and manage the SOFA GUI, including launching the main event loop and closing the GUI. It relies on the Sofa.Gui.GUIManager module. There are no explicit inputs or outputs shown for these calls, and they are typically used at the beginning and end of a SOFA application's interaction with the graphical interface. ```python Sofa.Gui.GUIManager.MainLoop(root) Sofa.Gui.GUIManager.closeGUI() ``` -------------------------------- ### Get Root Context in Python Source: https://sofapython3.readthedocs.io/en/latest/content/modules/Sofa/generated/Sofa.Core/classes/Sofa.Core.BaseContext Gets the root context of the Sofa graph. This function is essential for navigating to the top level of the simulation scene hierarchy. ```python getRootContext(_self : Sofa.Core.BaseContext_) → Sofa.Core.BaseContext # Get the root context of the graph ``` -------------------------------- ### Install Python 3.8 and NumPy on Ubuntu Source: https://sofapython3.readthedocs.io/en/v24.12.00-doc-generation/_sources/content/UsingThePlugin.rst Commands to install Python 3.8, pip, and the NumPy library on Ubuntu. This is a prerequisite for using SofaPython3. ```bash sudo add-apt-repository ppa:deadsnakes/ppa sudo apt install libpython3.8 python3.8 python3-pip python3.8 -m pip install numpy ```