Skip to content

Commit 6721f3f

Browse files
committed
Modify generic add to support object creation passing parameter for prefab
1 parent 9de87d4 commit 6721f3f

1 file changed

Lines changed: 40 additions & 7 deletions

File tree

stlib/__init__.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
__all__ = ["core","entities","prefabs","shapes"]
22

33
import Sofa.Core
4-
def __genericAdd(self : Sofa.Core.Node, typeName, **kwargs):
4+
5+
from stlib.core.baseParameters import BaseParameters
6+
from stlib.core.basePrefab import BasePrefab
7+
8+
9+
def __genericAdd(self : Sofa.Core.Node, typeName, *param, **kwargs):
10+
11+
512
def findName(cname, names):
613
"""Compute a working unique name in the node"""
714
rname = cname
@@ -11,7 +18,37 @@ def findName(cname, names):
1118
rname = cname + str(i+1)
1219
return rname
1320

14-
# Check if a name is provided, if not, use the one of the class
21+
def checkName(context : Sofa.Core.Node, name):
22+
# Check if the name already exists, if this happens, create a new one.
23+
if name in context.children or name in context.objects:
24+
names = {node.name.value for node in context.children}
25+
names = names.union({object.name.value for object in context.objects})
26+
name = findName(name, names)
27+
return name
28+
29+
30+
if len(param) == 1 and isinstance(typeName, type) and not issubclass(typeName, BasePrefab):
31+
raise RuntimeError("Invalid argument : only prefabs take positionnal argument which should be a parameter")
32+
elif len(param) > 1:
33+
raise RuntimeError("Invalid argument : only one positionnal argument accepted and only when used with a prefab")
34+
elif len(param) == 0 and isinstance(typeName, type) and issubclass(typeName, BasePrefab):
35+
raise RuntimeError("Invalid argument : one positionnal argument is required when calling add with prefab type")
36+
elif len(param) == 1 and isinstance(typeName, type) and issubclass(typeName, BasePrefab) and not isinstance(param[0], BaseParameters):
37+
raise RuntimeError("Invalid argument : when calling add with prefab type the positionnal argument is expected to be a type derived from stlib.core.BaseParameter")
38+
39+
40+
if len(param) == 1 and isinstance(typeName, type) and issubclass(typeName, BasePrefab):
41+
param[0].name = checkName(self, param[0].name)
42+
if(len(kwargs)):
43+
param[0].kwargs = kwargs.copy()
44+
45+
newEntity = self.addChild(typeName(param[0]))
46+
newEntity.init()
47+
return newEntity
48+
49+
## If we ever get here, it means we are not adding a prefab by giving its type name and its parameter set
50+
51+
# Check if a name is provided, if not, use the one of the class
1552
params = kwargs.copy()
1653
isNode = False
1754
if "name" not in params:
@@ -31,11 +68,7 @@ def findName(cname, names):
3168
else:
3269
raise RuntimeError("Invalid argument ", typeName)
3370

34-
# Check if the name already exists, if this happens, create a new one.
35-
if params["name"] in self.children or params["name"] in self.objects:
36-
names = {node.name.value for node in self.children}
37-
names = names.union({object.name.value for object in self.objects})
38-
params["name"] = findName(params["name"], names)
71+
params["name"] = checkName(self, params["name"])
3972

4073
# Dispatch the creation to either addObject or addChild
4174
if isinstance(typeName, type) and issubclass(typeName, Sofa.Core.Node):

0 commit comments

Comments
 (0)