Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
a6ff885
add folder for STLIB
bakpaul Apr 14, 2025
60352a4
Add reusable methods
bakpaul Apr 14, 2025
d024105
Add working files
bakpaul Apr 14, 2025
10082bf
Created example scenes
bakpaul Apr 14, 2025
d167479
Fix example
bakpaul Apr 14, 2025
dd9cc34
orga folders
hugtalbot Apr 14, 2025
7589706
Fix package
bakpaul Apr 14, 2025
2f844d7
bootstrap examples
bakpaul Apr 14, 2025
2c55e7d
start beginner scene
hugtalbot Apr 14, 2025
9b9e057
Move around files
bakpaul Apr 14, 2025
b989159
Move examples into SofaPython3 main example folder
bakpaul Apr 14, 2025
b42833b
Add unsaved modifications
bakpaul Apr 14, 2025
fa0bd74
reoganisation after discussion
EulalieCoevoet Apr 14, 2025
4380356
Add gemetries
bakpaul Apr 14, 2025
ba978ce
WIP FileParameters
bakpaul Apr 14, 2025
fc88100
Add beginner example
bakpaul Apr 14, 2025
7c314c6
Add unsaved changes
bakpaul Apr 14, 2025
b70a52b
Start work on visual and collision
bakpaul Apr 14, 2025
7cedc42
work on Tuesday : visual, geometry
hugtalbot Apr 15, 2025
5f183d5
add basePrefabParameters.py
hugtalbot Apr 15, 2025
97a2381
Add extracted geometry
bakpaul Apr 15, 2025
00fd16b
Fix visual example
bakpaul Apr 15, 2025
423a484
Finished collisions
bakpaul Apr 15, 2025
c97bfa3
[stlib-splib] work on prefabs: cleaning (#501)
EulalieCoevoet Jun 2, 2025
995d1b3
[stlib] adds README
EulalieCoevoet Jun 2, 2025
01531ca
[stlib] minor changes from discussion
EulalieCoevoet Jun 2, 2025
fc1404a
[splib] updates entity
EulalieCoevoet Jun 3, 2025
dd230b9
update README
hugtalbot Jun 3, 2025
57efa1d
add init in prefab
hugtalbot Jun 3, 2025
938f05b
Add first implem of entity
bakpaul Jun 3, 2025
592c13d
before the train 3/3
hugtalbot Jun 3, 2025
ca864d8
very last commit befor train
EulalieCoevoet Jun 3, 2025
1d8cdd3
WIP
bakpaul Jun 3, 2025
bacb258
Retore original. Still crash due to wrong string conversion
bakpaul Jun 3, 2025
09f45c5
[stlib] Deformable Entity (#502)
EulalieCoevoet Jun 13, 2025
d4b6042
Fix linkpath by introducing init method + make addDeformableMaterial …
bakpaul Jun 13, 2025
aad2a5e
[STLIB] Add the unified "add" in Sofa.Core.Node (#503)
damienmarchal Jul 17, 2025
1b15154
Add new tests for prefabs
bakpaul Jul 17, 2025
e2fff56
Revert modification of __genericAdd
bakpaul Jul 17, 2025
9de87d4
update addMass using elementType
hugtalbot Jul 17, 2025
5dd9eef
Add and "apply" méthod to node
damienmarchal Oct 29, 2025
475b564
Add a spot model
damienmarchal Oct 29, 2025
84a22bd
Add few experiment with prefabs
damienmarchal Oct 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[STLIB] Add the unified "add" in Sofa.Core.Node (#503)
* Add Sofa.Core.Node.add with preliminary test

* rename the "add" function and make it private

* Apply suggestions from code review

Co-authored-by: Hugo <hugo.talbot@sofa-framework.org>

---------

Co-authored-by: Hugo <hugo.talbot@sofa-framework.org>
  • Loading branch information
2 people authored and bakpaul committed Oct 28, 2025
commit aad2a5e4c4d3f54ff412d06d55f8b3f0a49a0be5
55 changes: 55 additions & 0 deletions stlib/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,56 @@
__all__ = ["core","entities","prefabs","shapes"]

import Sofa.Core
def __genericAdd(self : Sofa.Core.Node, typeName, **kwargs):
def findName(cname, names):
"""Compute a working unique name in the node"""
rname = cname
for i in range(0, len(names)):
if rname not in names:
return rname
rname = cname + str(i+1)
return rname

# Check if a name is provided, if not, use the one of the class
params = kwargs.copy()
isNode = False
if "name" not in params:
if isinstance(typeName, str):
params["name"] = typeName
isNode=True
elif isinstance(typeName, type) and issubclass(typeName, Sofa.Core.Node):
params["name"] = "Node"
isNode=True
elif isinstance(typeName, Sofa.Core.Node):
params["name"] = "Node"
isNode=True
elif isinstance(typeName, type) and issubclass(typeName, Sofa.Core.Object):
params["name"] = typeName.name.value
elif isinstance(typeName, type) and issubclass(typeName, Sofa.Core.ObjectDeclaration):
params["name"] = typeName.__name__
else:
raise RuntimeError("Invalid argument ", typeName)

# Check if the name already exists, if this happens, create a new one.
if params["name"] in self.children or params["name"] in self.objects:
names = {node.name.value for node in self.children}
names = names.union({object.name.value for object in self.objects})
params["name"] = findName(params["name"], names)

# Dispatch the creation to either addObject or addChild
if isinstance(typeName, type) and issubclass(typeName, Sofa.Core.Node):
pref = self.addChild(typeName(params["name"]))
elif isinstance(typeName, Sofa.Core.Node):
pref = self.addChild(typeName)
elif isinstance(typeName, type) and issubclass(typeName, Sofa.Core.Object):
pref = self.addObject(typeName(**params))
elif isinstance(typeName, type) and issubclass(typeName, Sofa.Core.ObjectDeclaration):
pref = self.addObject(typeName.__name__, **params)
elif isinstance(typeName, str):
pref = self.addObject(typeName, **params)
else:
raise RuntimeError("Invalid argument", typeName)
return pref

# Inject the method so it become available as if it was part of Sofa.Core.Node
Sofa.Core.Node.add = __genericAdd
27 changes: 0 additions & 27 deletions stlib/core/basePrefab.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,6 @@
import Sofa.Core
from stlib.core.basePrefabParameters import BasePrefabParameters


def addFromTypeName(self : Sofa.Core.Node, typeName, params = BasePrefabParameters, **kwargs):
def findName(cname, node):
"""Compute a working unique name in the node"""
rname = cname
for i in range(0, len(node.children)):
if rname not in node.children:
return rname
rname = cname + str(i+1)
return rname

for k,v in kwargs.items():
if hasattr(params, k):
setattr(params, k, v)

params = copy.copy(params)
if params.name in self.children:
params.name = findName(params.name, self)

pref = self.addChild(typeName(params))
pref.init()

return pref

Sofa.Core.Node.add = addFromTypeName


class BasePrefab(Sofa.Core.Node):
"""
A Prefab is a Sofa.Node that assembles a set of components and nodes
Expand Down
64 changes: 64 additions & 0 deletions tests/test_new_add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import unittest
import Sofa
import SofaRuntime
import Sofa.Core
import stlib

class ObjectDeclaration(object):
...

Sofa.Core.ObjectDeclaration = ObjectDeclaration

class MechanicalObject(ObjectDeclaration):
pass

class TestNewAdd(unittest.TestCase):
def test_add_node_with_node_type(self):
root = Sofa.Core.Node("root")
root.add(Sofa.Core.Node, name="aNodeA")
self.assertEqual(len(root.children), 1)
self.assertEqual(root.children[0].name.value, "aNodeA")

def test_add_node_with_node_instance(self):
root = Sofa.Core.Node("root")
root.add(Sofa.Core.Node("aNodeB"))
self.assertEqual(len(root.children), 1)
self.assertEqual(root.children[0].name.value, "aNodeB")

def test_add_object_with_string_type(self):
root = Sofa.Core.Node("root")
root.add("MechanicalObject", name="anObject1", position=[[1,2,3]])
self.assertEqual(len(root.objects), 1)
self.assertEqual(root.objects[0].name.value, "anObject1")
self.assertEqual(root.objects[0].position.value.shape, (1,3))

def test_add_object_with_object_type(self):
root = Sofa.Core.Node("root")
root.add(MechanicalObject, name="anObject2", position=[[1,2,3]])
self.assertEqual(len(root.objects), 1)
self.assertEqual(root.objects[0].name.value, "anObject2")
self.assertEqual(root.objects[0].position.value.shape, (1,3))

def test_automatic_name_generation(self):
root = Sofa.Core.Node("root")
root.add(MechanicalObject, position=[[1,2,3]])
root.add(MechanicalObject, position=[[1,2,3]])
root.add(MechanicalObject, position=[[1,2,3]])
self.assertEqual(root.objects[0].name.value, "MechanicalObject")
self.assertEqual(root.objects[1].name.value, "MechanicalObject1")
self.assertEqual(root.objects[2].name.value, "MechanicalObject2")

root.add(Sofa.Core.Node, name="TestNode")
root.add(Sofa.Core.Node, name="TestNode")
self.assertEqual(root.children[0].name.value, "TestNode")
self.assertEqual(root.children[1].name.value, "TestNode1")

root.add(Sofa.Core.Node)
root.add(Sofa.Core.Node)
self.assertEqual(root.children[2].name.value, "Node")
self.assertEqual(root.children[3].name.value, "Node1")

if __name__ == '__main__':

SofaRuntime.importPlugin("Sofa.Component.StateContainer")
unittest.main()