Sofa.prefab.Prefab

class Sofa.prefab.Prefab(*args, **kwargs)[source]

Bases: RawPrefab

Prefabs are python templates for a SOFA scene

They allow to simplify the design of a simulation by using pre-structured and reusable python script. Inherit from this class to create your own Prefab. What makes Prefab special is that they have a set of special data named prefabParameters. When any of prefabParameter is changed the prefab is completely recreated by calling the onParameterChanged method so the scene graph is always kept synchronized with the parameter’s content.

To specify the prefabParameters, it is possible to provide in the class a list of dictionaries containing the 3 required fields (“name”, “type”, “help”) and one optional field (“default”).

The same syntax can be used to also add prefab’s data.

Example

import Sofa.Core

class Foo(Sofa.Core.Prefab):
    prefabParameters = [{ 'name': 'n', 'type': 'int', 'help': 'number of repetition, 'default': 1},
                        {'name': 'message', 'type': 'string', 'help': 'message to display', 'default': ''}]

    myAttribute = 0

    def __init__(self, *a, *k):
        Sofa.Core.Prefab.__init__(self, *a, **k)

    def init(self):
        myAttribute += 1
        for i in range(0, self.n.value):
            print(self.message.value)

n = Sofa.Core.Node()
n.addChild(Foo(name="aFooPrefab", n=42, message="hello universe!"))
Prefab has protected the following additional keywords:
  • “name” = the name of the prefab instance

  • “parent” and “parents” = can’t be used together, they set the context of the prefab, thus allowing paths resolution for Prefab parameters whose arguments are passed as link paths (strings). parents (with an ‘-s’) sets multi-node contexts

class SearchDirection(self: Sofa.Core.BaseContext.SearchDirection, value: int)

Bases: pybind11_object

Members:

SearchUp

Local

SearchDown

SearchRoot

SearchParents

property name
add(self: Sofa.Core.Node, arg0: object, **kwargs) object

Add an prefab,

Example

def Cube(parentNodes, name="Cube"):
    cube = parentNode.addChild(name)
    cube.addObject("MechanicalObject")
    return cube

node.add(Cube, name="MyCube"")
addChild(*args, **kwargs)

Overloaded function.

  1. addChild(self: Sofa.Core.Node, arg0: str, **kwargs) -> object

    Add a new node as a child

    param name

    name of the child node to be added

    type name

    str

    param kwargs

    Extra parameters passed to the created Sofa.Node

    type kwargs

    dict

    rtype

    the created :class: Sofa.Simulation.Node

  2. addChild(self: Sofa.Core.Node, arg0: Sofa.Core.Node) -> Sofa.Core.Node

    Add an existing node as child

    param node

    the node to be added

    type node
    class

    Sofa.Simulation.Node

    rtype

    the added :class: Sofa.Simulation.Node

addData(*args, **kwargs)

Overloaded function.

  1. addData(self: object, name: str, value: object = None, default: object = None, help: str = ‘’, group: str = ‘’, type: str = ‘’) -> sofa::core::objectmodel::BaseData

    Create a data field, then adds it to the base

    Note that this method should only be called if the field was not initialized with the initData method

    param self

    the base itself

    param name

    the name of the data to be added

    param value

    the value from which the data can be created

    param help

    help message that describes the data to be created

    param group

    the group the data belongs to

    param type

    the type of the data

    type self

    object

    type name

    string

    type value

    object

    type help

    string

    type group

    string

    type type

    string

  2. addData(self: Sofa.Core.Base, arg0: object) -> sofa::core::objectmodel::BaseData

    Add a data field

    param self

    the base itself

    param d

    the data to be added

    type self

    Base*

    type d

    object

    This method should only be called if the field was not initialized with the initData method

Create a Link to a SOFA component and adds it to the base

Parameters
  • self (object) – the base itself

  • name (string) – the name of the link to be added

  • value (object) – the value from which the data can be created (either a pathname or a SofaBase)

  • help (string) – help message that describes the link to be created

This method should only be called if the field was not initialized with the initLink method

addObject(*args, **kwargs)

Overloaded function.

  1. addObject(self: Sofa.Core.Node, arg0: str, **kwargs) -> object

    Add an object.

    param component_type

    the Sofa component’s type name to add

    type component_type

    str

    param kwargs

    additional keyword arguments

    type kwargs

    dict

    rtype

    the created :class: Sofa.Core.Object

  2. addObject(self: Sofa.Core.Node, arg0: Sofa.Core.Object) -> object

    Add an existing sofa object.

    param component

    The Sofa component

    type component
    class

    Sofa.Core.Object

    rtype

    the added :class: Sofa.Core.Object

addPrefabParameter(self: Sofa.Core.RawPrefab, name: str, help: str, type: str, default: object = None) None
canChangeSleepingState(self: Sofa.Core.BaseContext) bool

Whether the context can change its sleeping state or not

property children

Field interface to acces the children of a node. The returned object is a iteratable featuring the following operations: len, remove_at, __contains__, get_at

Example: >>> n = Sofa.Core.Node(“MyNode”) >>> n.addChild(“child1”) >>> for child in n.children: >>> print(child.name) >>> >>> if “child1” in n.children: >>> print(“Yes”) >>> print(len(n.children))

clearLoggedMessages(self: Sofa.Core.Base) object

Remove all logged messages in the object’s logs

countLoggedMessages(self: Sofa.Core.Base) object

Returns the number of messages in the object’s logs

createChild(self: Sofa.Core.Node, arg0: str, **kwargs) object

Deprecated, see addChild

createObject(self: Sofa.Core.Node, arg0: str, **kwargs) object

Deprecated, see addObject

detachFromGraph(self: Sofa.Core.Node) None

Remove the current node from the graph: depending on the type of Node, it can have one or several parents.

findData(self: Sofa.Core.Base, arg0: str) sofa::core::objectmodel::BaseData

Find a data field given its name

If more than one field is found (due to aliases), only the first is returned.

:param name :type name: string :return: the data field or None

Find a link given its name

Return NULL if not found. If more than one link is found (due to aliases), only the first is returned.

Parameters

name (string) – the name of the link

Returns

the link

getAnimate(self: Sofa.Core.BaseContext) bool

Animation flag

getAsACreateObjectParameter(self: Sofa.Core.Node) str

Get the link of the current node :rtype: string

getChild(self: Sofa.Core.Node, arg0: str) object

Get the child of a node.

Parameters
  • n (Sofa.Simulation.Node) –

  • name (string) –

Returns

the child with ‘name’, None otherwise

getClass(self: Sofa.Core.Base) sofa::core::objectmodel::BaseClass

Return the class of the object

getClassName(self: Sofa.Core.Base) str

Get the name of the class of the Base.

getData(self: Sofa.Core.Base, arg0: str) object

Get the data field given its name

Parameters
  • self (Base&) –

  • s (string) –

Returns

the first data found of this name

getDataFields(self: Sofa.Core.Base) list

Accessor to the vector containing all the fields of this object :return: A vector containing the data fields

getDefinitionSourceFileName(self: Sofa.Core.Base) str

Returns the name of the file that contains the object definition.

getDefinitionSourceFilePos(self: Sofa.Core.Base) int

Returns the line number where the object is defined.

getDt(self: Sofa.Core.BaseContext) float

Simulation timestep

getForceField(self: Sofa.Core.Node, arg0: int) object

Get the force field of a node, given an index. :param index: index of the force field :type index: unsigned int.

getGravity(self: Sofa.Core.BaseContext) sofa::type::Vec<3u, double>

Gravity in local coordinates

getInstanciationFileName(self: Sofa.Core.Base) str

Returns the line number where the object is instanciatiated.

getInstanciationSourceFilePos(self: Sofa.Core.Base) int

Returns the line number where the object is instanciatiated.

getLinkPath(self: Sofa.Core.Node) str

Get the link of the current node :param node: :type node: Sofa.Simulation.Node*

Accessor to the vector containing all the links of this object

Returns

A vector containing the links

getLoggedMessagesAsString(self: Sofa.Core.Base) object

Returns a single string with all the messages logged in the internal buffer of a sofa object. Return empty string if there is no messages.

getMass(self: Sofa.Core.Node) object

Get the mass of the node

getMechanicalMapping(self: Sofa.Core.Node) object

Get the mechanical mapping of the node.

getMechanicalState(self: Sofa.Core.Node) object

Get the mechanical state of the node.

getMeshTopology(self: Sofa.Core.BaseContext, SearchDirection: Sofa.Core.BaseContext.SearchDirection = <SearchDirection.SearchUp: -1>) sofa::core::topology::BaseMeshTopology

Mesh Topology (unified interface for both static and dynamic topologies)

Mesh Topology (unified interface for both static and dynamic topologies)

getName(self: Sofa.Core.Base) str

Return the name of the entity

Return type

string

getObject(self: Sofa.Core.Node, arg0: str, **kwargs) object

Get a sofa component hold by a node.

Parameters

name (string) –

Returns

the component with ‘name’, None otherwise

Note

The extra arguments allowed in the SofaPython (warning=True/False) binding are not supported SofaPython3.

# SofaPython3:
if node.getObject("MyObject") != None:
    pass

if node.hasObject("MyObject"):
    pass

if "MyObject" in node.objects:
    pass
getPathName(self: Sofa.Core.Node) str

Get the path name of the current node :rtype: string

getRoot(self: Sofa.Core.Node) object

Get the root node of the current node. :rtype: Sofa.Simulation.BaseNode*

getRootContext(self: Sofa.Core.BaseContext) Sofa.Core.BaseContext

Get the root context of the graph

getRootPath(self: Sofa.Core.Node) str

Return the path from this node to the root node

getState(self: Sofa.Core.BaseContext) sofa::core::BaseState

Mechanical Degrees-of-Freedom

getTemplateName(self: Sofa.Core.Base) str

Get the name of the template of the Base.

getTime(self: Sofa.Core.BaseContext) float

Simulation time

getTopology(self: Sofa.Core.BaseContext) sofa::core::topology::Topology

Topology

hasObject(self: Sofa.Core.Node, arg0: str) object

Check if there is a component with provided name.

:param n :param name :type n: Sofa.Simulation.Node :type name: string :return: True if the node has an object with correspdonding name.

init(self: Sofa.Core.RawPrefab) None
isActive(self: Sofa.Core.BaseContext) bool

The Context is active

isInitialized(self: Sofa.Core.Node) bool

Checks if the node has been initialized :return: true if it has been initialized

isSleeping(self: Sofa.Core.BaseContext) bool

Sleeping state of the context

moveChild(self: Sofa.Core.Node, arg0: Sofa.Core.BaseNode, arg1: Sofa.Core.BaseNode) None

Move a node from another node. :param child: the node to be moved :param prevParent: the previous parent of the node to be moved :type child: Sofa.Simulation.Node :type prevParent: Sofa.Simulation.Node

property objects

Field interface to acces the objects of a node. The returned object is a iteratable featuring the following operations: len, remove_at, __contains__, get_at

Example: >>> n = Sofa.Core.Node(“MyNode”) >>> n.addObject(“MechanicalObject”, name=”object1”) >>> n.addObject(“MechanicalObject”, name=”object2”) >>> for object in n.objects: >>> print(object.name) >>> >>> if “object2” in c.objects: >>> print(“Yes”) >>> print(len(n.objects))

property parents

Field interface to acces the parents of a node. The returned object is a iteratable featuring the following operations: len, remove_at, __contains__, get_at

Example: >>> n = Sofa.Core.Node(“parent1”) >>> c = n.addChild(“child1”) >>> for parent in c.parents: >>> print(parent.name) >>> >>> if “parent1” in c.parents: >>> print(“Yes”) >>> print(len(n.parents))

reinit(self: Sofa.Core.RawPrefab) None
removeChild(*args, **kwargs)

Overloaded function.

  1. removeChild(self: Sofa.Core.Node, arg0: Sofa.Core.Node) -> None

    Remove a child of a node. :param self: the node itself :param n: the child to remove :type self: Sofa.Simulation.Node :type n: Sofa.Simulation.Node Example: >>> node1.removeChild(node2)

  2. removeChild(self: Sofa.Core.Node, arg0: str) -> object

    Remove a child of a node. :param n: the node itself :param name: the name of the child to remove :type n: Sofa.Simulation.Node& :type name: string Example: >>> node1.removeChild(“nameNode2”)

removeObject(self: Sofa.Core.Node, arg0: Sofa.Core.Object) None

Remove an object :param object: the object to be removed :type object: BaseObject

sendEvent(self: Sofa.Core.Node, arg0: object, arg1: str) None

Send an event to other nodes, by creating a PythonScriptEvent and propagating it to the rest of the tree. Only the nodes and objects downstream will receive the message. :param pyUserData: the user data that can be sent :param eventName: the name of the event :type pyUserData: py::object :type eventName: string

setActive(self: Sofa.Core.BaseContext, arg0: bool) None

State of the context

setAnimate(self: Sofa.Core.BaseContext, arg0: bool) None

Animation flag

setChangeSleepingState(self: Sofa.Core.BaseContext, arg0: bool) None

Sleeping state change of the context

setDataValues(self: Sofa.Core.Base, **kwargs) object

Set values for a the given data field, multiple pairs of args are allowed.

setDefinitionSourceFileName(self: Sofa.Core.Base, arg0: str) None

set the name of the file that contains the object definition.

setDefinitionSourceFilePos(self: Sofa.Core.Base, arg0: int) None

Set the line number where the object is defined.

setDt(self: Sofa.Core.BaseContext, arg0: float) None

Simulation timestep

setGravity(self: Sofa.Core.BaseContext, arg0: sofa::type::Vec<3u, double>) None

Gravity in local coordinates

setInstanciationSourceFileName(self: Sofa.Core.Base, arg0: str) None

Set the line number where the object is instanciatiated.

setInstanciationSourceFilePos(self: Sofa.Core.Base, arg0: int) None

Set the line number where the object is instanciatiated.

setName(self: Sofa.Core.Base, arg0: str) None

Set the name of this object

:param n :type n: string

setSleeping(self: Sofa.Core.BaseContext, arg0: bool) None

Sleeping state of the context

setSourceTracking(self: Sofa.Core.RawPrefab, arg0: str) None