Skip to content

Commit 8037538

Browse files
committed
Add default name
1 parent f1435ba commit 8037538

3 files changed

Lines changed: 36 additions & 51 deletions

File tree

examples/stlib/node_modifier.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,17 @@ def createScene(root):
8484
S = ModelsNode.add(Entity, parameters = SParams)
8585

8686
#TODO make the name automatically match the modifier type if none is given
87-
root.add(NodeModifier, on = [ModelsNode], parameters = SimulationSolversParameters(name = "SimulationSolvers",
88-
constantSparsity=False))
87+
root.add(NodeModifier, on = [ModelsNode], parameters = SimulationSolversParameters(constantSparsity=False))
8988

90-
root.add(NodeModifier, on = [root], parameters = SimulationSettingsParameters(name = "SimulationSettings",
91-
displayFlags = ["showVisualModels", "showInteractionForceFields"],
89+
root.add(NodeModifier, on = [root], parameters = SimulationSettingsParameters(displayFlags = ["showVisualModels", "showInteractionForceFields"],
9290
enableCollisionDetection = True,
9391
useLagrangian = True,
9492
parallelComputing = False,
9593
alarmDistance=0.3, contactDistance=0.02,
9694
frictionCoef=0.5, tolerance=1.0e-4, maxIterations=20))
9795

98-
Logo.add(NodeModifier, on = [Logo], parameters = FixConstraintParameters(name = "FixConstraintParameters", boxROIs=[[-1, -2, -13, 3, 2, -7]]))
96+
Logo.add(NodeModifier, on = [Logo], parameters = FixConstraintParameters( boxROIs=[[-1, -2, -13, 3, 2, -7]]))
97+
Logo.add(NodeModifier, on = [Logo], parameters = FixConstraintParameters( boxROIs=[[-100, -2, -13, -300, 2, -7]]))
9998
ModelsNode.add(NodeModifier, on = [S, Logo], parameters = AttachmentConstraintParameters(name = "AttachmentConstraintParameters", indices1=[26,20,119,121], indices2=[722,732,574,573], stiffness=0.5, damping=0.0,
10099
length=[((9.43-9.35)**2 + (-.44-0.48)**2 + (-6.01+6.56)**2)**(0.5) ]))
101100

stlib/geometries/extract.py

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,33 @@
66

77
import Sofa
88
from Sofa.Core import Node
9+
from functools import partial
910

1011

1112
class ExtractInternalDataProvider(InternalDataProvider):
12-
destinationType : ElementType
13-
sourceType : ElementType
14-
sourceName : str
1513

16-
def __init__(self, destinationType : ElementType, sourceType : ElementType, sourceName : str):
17-
self.destinationType = destinationType
18-
self.sourceType = sourceType
19-
self.sourceName = sourceName
20-
21-
def __post_init__(self):
22-
if(not (self.sourceType == ElementType.TETRAHEDRA and self.destinationType == ElementType.TRIANGLES)
23-
and not (self.sourceType == ElementType.HEXAHEDRA and self.destinationType == ElementType.QUADS) ):
24-
raise ValueError("Only configuration possible are 'Tetrahedra to Triangles' and 'Hexahedra to Quads'")
25-
26-
InternalDataProvider.__init__(self)
14+
def __init__(self):
15+
super().__init__()
2716

2817
def generateAttribute(self, parent : Geometry):
29-
node = parent.addChild("ExtractedGeometry")
18+
self.position = parent.parents[0].parents[0].getChild("Geometry").container.position.linkpath
19+
3020

31-
#TODO: Specify somewhere in the doc that this should only be used for mapped topologies that extract parent topology surface
32-
# fromLink = parent.parents[0].parents[0].getChild(self.SourceName).container.linkpath
33-
# TODO: the line above cannot work if the nodes and objects are not added to the graph prior the end of __init__() call
34-
# !!! also, on a fail, nothing is added to the graph, which makes things harder to debug
35-
# !!! also, does not work because of the function canCreate(), which checks the input (not yet created?)
36-
# this is all related
37-
fromLink = "@../../../Geometry/container" # TODO: can we do better than this?
38-
addDynamicTopology(node, elementType=self.destinationType, container={"position" : fromLink + ".position"})
39-
if self.sourceType == ElementType.TETRAHEDRA:
40-
node.addObject("Tetra2TriangleTopologicalMapping", input=fromLink, output=node.container.linkpath)
41-
elif self.sourceType == ElementType.HEXAHEDRA:
42-
node.addObject("Hexa2QuadTopologicalMapping", input=fromLink, output=node.container.linkpath)
43-
else:
44-
Sofa.msg_error("[stlib/geometry/exctrat.py]", "Element type: " + str(self.sourceType) + " not supported.")
21+
def extractGeometry(sourceType : ElementType, parent : Geometry):
22+
#TODO: Specify somewhere in the doc that this should only be used for mapped topologies that extract parent topology surface
23+
# fromLink = parent.parents[0].parents[0].getChild(self.SourceName).container.linkpath
24+
# TODO: the line above cannot work if the nodes and objects are not added to the graph prior the end of __init__() call
25+
# !!! also, on a fail, nothing is added to the graph, which makes things harder to debug
26+
# !!! also, does not work because of the function canCreate(), which checks the input (not yet created?)
27+
# this is all related
28+
fromLink = parent.parents[0].parents[0].getChild("Geometry").container.linkpath # TODO: can we do better than this?
29+
if sourceType == ElementType.TETRAHEDRA:
30+
parent.addObject("Tetra2TriangleTopologicalMapping", input=fromLink, output=parent.container.linkpath)
31+
elif sourceType == ElementType.HEXAHEDRA:
32+
parent.addObject("Hexa2QuadTopologicalMapping", input=fromLink, output=parent.container.linkpath)
33+
else:
34+
Sofa.msg_error("[stlib/geometry/exctrat.py]", "Element type: " + str(sourceType) + " not supported.")
4535

46-
self.position = node.container.position.linkpath
47-
if node.container.findData("edges") is not None:
48-
self.edges = node.container.edges.linkpath
49-
if node.container.findData("triangles") is not None:
50-
self.triangles = node.container.triangles.linkpath
51-
if node.container.findData("quads") is not None:
52-
self.quads = node.container.quads.linkpath
53-
if node.container.findData("hexahedra") is not None:
54-
self.hexahedra = node.container.hexahedra.linkpath
55-
if node.container.findData("tetras") is not None:
56-
self.tetrahedra = node.container.tetras.linkpath
5736

5837

5938

@@ -68,9 +47,6 @@ def __init__(self,
6847

6948
self.postInit = partial(extractGeometry, sourceParameters.elementType)
7049

71-
7250
if(not (sourceParameters.elementType == ElementType.TETRAHEDRA and destinationType == ElementType.TRIANGLES)
73-
and not (sourceParameters.elementType == ElementType.HEXAHEDRA and destinationType == ElementType.QUADS) ):
74-
raise ValueError("Only configuration possible are 'Tetrahedra to Triangles' and 'Hexahedra to Quads'")
75-
76-
51+
and not (sourceParameters.elementType == ElementType.HEXAHEDRA and destinationType == ElementType.QUADS) ):
52+
raise ValueError("Only configuration possible are 'Tetrahedra to Triangles' and 'Hexahedra to Quads'")

stlib/node_modifiers/__node_modifier__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,18 @@ def wrapper(*args, **kwargs):
2626

2727
@dataclasses.dataclass
2828
class BaseNodeModifierParameters(object):
29-
name : str = "NodeModifier"
29+
name : str = None
3030
kwargs : dict = dataclasses.field(default_factory=dict)
3131

32+
def __post_init__(self):
33+
#Give parameter type name as default name
34+
if self.name is None:
35+
self.name = type(self).__name__
36+
pId = self.name.find("Parameters")
37+
if pId > 0:
38+
self.name = self.name[0:pId]
39+
40+
3241
@AffectedNodes(0)
3342
def modify(self, owner, node : list[Node]) -> list[Node] :
3443
return []
@@ -50,6 +59,7 @@ def __init__(self, parameters : BaseNodeModifierParameters):
5059
Sofa.Core.Component.__init__(self, **(parameters.toDict()))
5160
self.parameters = parameters
5261

62+
5363
def register( self, owner, nodes : list[Node]) :
5464
if len(nodes) == 0:
5565
raise ValueError(f"No node seems to have been modified by the node modifier {self.parameters.name}. Make sure the \"modify\" method of the used parameter type returns the list of nodes modified by this modifier.")

0 commit comments

Comments
 (0)