Skip to main content

Sofa helper to build scenes

Project description

Easily build complex simulation graphs for SOFA

Install

pip install sofacomponents

Quick start

from sofacomponents import Node, SofaFactory

# build the factory
fa = SofaFactory(debug=False)

# create the root node
root = Node()
# add attributes
root.attrs(dt=0.05, gravity=[0, -1, 0])
# add sofa components from the factory
root.add(
    fa.VisualStyle(displayFlags="showVisual"),
    fa.DefaultAnimationLoop(),
    fa.RequiredPlugin(pluginName="SofaOpenglVisual SofaPython"),
)
# add child node
childNode = root.child("childNode").add(
    fa.MeshVTKLoader(filename="mesh.vtk"),
    # add components not from the factory
    fa.UnknownComponent(data="foo"),
)

# display the scene
print(root)

# Export the scene
root.write_scene_to("scene.py")

# Export the graph
root.write_graph_to("graph.txt")

Output:

root
  └ VisualStyle (displayFlags=showVisual)
  └ DefaultAnimationLoop ()
  └ RequiredPlugin (pluginName=SofaOpenglVisual SofaPython)
  └ childNode
    └ MeshVTKLoader (filename=mesh.vtk)
    └ UnknownComponent (data=foo)

Python generated scene:

import Sofa

class SceneElectroMechanical(Sofa.PythonScriptController):
    def __init__(self, node):
        self.createGraph(node)

    def createGraph(self, root):
        # root
        root.dt = 0.05
        root.gravity = [0, -1, 0]
        root.createObject("VisualStyle", displayFlags="showVisual")
        root.createObject("DefaultAnimationLoop",)
        root.createObject("RequiredPlugin", pluginName="SofaOpenglVisual SofaPython")

        # root/childNode
        childNode = root.createChild("childNode")
        childNode.createObject("MeshVTKLoader", filename="mesh.vtk")
        childNode.createObject("UnknownComponent", data="foo")

def createScene(root_node):
    SceneElectroMechanical(root_node)

Extend

Create a factory instanciating the SofaFactory class.

The sofa_component decorator allows to update the parameters directory with:

  1. the general default parameters (See SofaFactory._defaults)
  2. the params you choose for your component
  3. the custom kwargs given by the user
from sofacomponents import SofaFactory, sofa_component

class MyFactory(SofaFactory):
    def __init__(self, debug=False):
        super().__init__(debug)

    @sofa_component
    def MyComponentNoParams(self, **kwargs):
        """ Minimum signature to register the component to the factory """
        return "MyComponentName"

    @sofa_component
    def MyComponent(self, **kwargs):
        params = dict(
            data="foo",
            # ...
        )
        return "MyComponentName", params

And then

# build the factory
fa = MyFactory()

Embeded documentation

pycharm documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sofacomponents-0.1.11.tar.gz (252.3 kB view details)

Uploaded Source

Built Distribution

sofacomponents-0.1.11-py3-none-any.whl (646.0 kB view details)

Uploaded Python 3

File details

Details for the file sofacomponents-0.1.11.tar.gz.

File metadata

  • Download URL: sofacomponents-0.1.11.tar.gz
  • Upload date:
  • Size: 252.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/54.1.2 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for sofacomponents-0.1.11.tar.gz
Algorithm Hash digest
SHA256 3e8fc81211602f9e2f9880f7c779d4c54943cfcf8b35a29b1d5f13ad9dc9c4ac
MD5 68f42c71177a7aa6f403dca527b7b68a
BLAKE2b-256 b056ad65086d2701a75febea6f3c9a3d375fc7486353fde846963eb7c1d6f989

See more details on using hashes here.

Provenance

File details

Details for the file sofacomponents-0.1.11-py3-none-any.whl.

File metadata

  • Download URL: sofacomponents-0.1.11-py3-none-any.whl
  • Upload date:
  • Size: 646.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/54.1.2 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for sofacomponents-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 40527dbc5e9474fff05191170ccdce8c3ae13a391bc67f4579a6c07bcfd5e330
MD5 9d7d4b86620a9dece7c6db1ecafd49bf
BLAKE2b-256 c782d99b1b4dbc6f2baa95ac8babc6ce9cf295b6cf01a14b438c98444349bcaa

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page