-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy path__init__.py
More file actions
105 lines (84 loc) · 4.08 KB
/
__init__.py
File metadata and controls
105 lines (84 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
__all__ = ["core","entities","geometries","materials","modifiers","collision","visual"]
import Sofa.Core
from stlib.core.basePrefab import BasePrefab
from stlib.core.baseEntity import BaseEntity
from stlib.core.baseEntityModifier import BaseEntityModifier
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
def __checkName(context : Sofa.Core.Node, name):
"""Check if the name already exists, if this happens, create a new one.
"""
if name in context.children or name in context.objects:
names = {node.name.value for node in context.children}
names = names.union({object.name.value for object in context.objects})
name = __findName(name, names)
return name
def __processParameters(self : Sofa.Core.Node, typeName, **kwargs):
"""Check if a name is provided, if not, use the one of the class
"""
params = kwargs.copy()
if isinstance(typeName, type) and issubclass(typeName, BasePrefab): #Only for prefabs
if len(params.keys()) > 1 or (len(params.keys()) == 1 and "parameters" not in params):
raise RuntimeError("Invalid argument, a prefab takes only the \"parameters\" kwargs as input")
elif "name" not in params : #This doesn't apply to prefab
if isinstance(typeName, str):
params["name"] = typeName
elif isinstance(typeName, type) and issubclass(typeName, Sofa.Core.Node):
params["name"] = typeName.__name__
elif isinstance(typeName, Sofa.Core.Node):
params["name"] = "Node"
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)
if isinstance(typeName, type) and issubclass(typeName, BasePrefab) and len(params.keys()) == 1:
params["parameters"].name = __checkName(self, params["parameters"].name)
else:
params["name"] = __checkName(self, params["name"])
return params
def __create(self: Sofa.Core.Node, typeName, **kwargs):
params = __processParameters(self, typeName, **kwargs)
if isinstance(typeName, type) and issubclass(typeName, BaseEntityModifier):
node = typeName(**params)
node.creator = self
return node
if isinstance(typeName, type) and issubclass(typeName, BasePrefab):
return typeName(**params)
elif isinstance(typeName, Sofa.Core.Node):
return typeName(**params)
return
def __genericAdd(self: Sofa.Core.Node, typeName, **kwargs):
# Dispatch the creation to either addObject or addChild
if isinstance(typeName, type) and issubclass(typeName, BasePrefab):
params = __processParameters(self, typeName, **kwargs)
pref = self.addChild(typeName(**params))
pref.init()
elif isinstance(typeName, Sofa.Core.Node) and issubclass(typeName.__class__, BaseEntity):
pref = self.addChild(typeName)
pref.init()
elif isinstance(typeName, Sofa.Core.Node):
pref = self.addChild(typeName)
elif isinstance(typeName, type) and issubclass(typeName, Sofa.Core.Object):
params = __processParameters(self, typeName, **kwargs)
pref = self.addObject(typeName(**params))
elif isinstance(typeName, type) and issubclass(typeName, Sofa.Core.ObjectDeclaration):
params = __processParameters(self, typeName, **kwargs)
pref = self.addObject(typeName.__name__, **params)
elif isinstance(typeName, str):
params = __processParameters(self, typeName, **kwargs)
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
Sofa.Core.Node.create = __create