-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathnode_wrapper.py
More file actions
75 lines (55 loc) · 2.72 KB
/
node_wrapper.py
File metadata and controls
75 lines (55 loc) · 2.72 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
from functools import wraps
from splib.core.utils import defaultValueType
# The two classes are not merged because one could want to use a ReusableMethod
# (enabling to pass dictionary fixing params) without wanting to use a full PrefabSimulation
class BaseWrapper(object):
def __init__(self,node):
self.node = node
def __getattr__(self, item):
return getattr(self.node,item)
class ObjectWrapper(BaseWrapper):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
def addObject(self,*args, **kwargs):
parameters = {}
# Expand any parameter that has been set by the user in a custom dictionary
# and having the same key as the component name
if "name" in kwargs:
parameters["name"] = kwargs["name"]
if kwargs["name"] in kwargs:
if isinstance(kwargs[kwargs["name"]], dict):
for param in kwargs[kwargs["name"]]:
if not(isinstance(kwargs[kwargs["name"]][param],defaultValueType)):
parameters = {**parameters, param : kwargs[kwargs["name"]][param]}
else:
print("[Warning] You are passing a keyword arg with the same name as one obj without it being a Dict, it will not be used. ")
# Add all the parameters from kwargs that are not dictionary
for param in kwargs:
if param in parameters:
if not(param == "name"):
print("[Warning] You are redefining the parameter '"+ param + "' of object " + str(args[0]))
elif not(isinstance(kwargs[param], dict)) and not(isinstance(kwargs[param],defaultValueType)):
parameters = {**parameters,param:kwargs[param]}
return self.node.addObject(*args,**parameters)
class ChildWrapper(ObjectWrapper):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
# This method enforces that the object that is created by the addChild method keeps the prefab type
def addChild(self,*args, **kwargs):
child = self.node.addChild(*args,**kwargs)
returnObject = self.__new__(type(self))
returnObject.__init__(child)
return returnObject
def ReusableMethod(method):
@wraps(method)
def wrapper(*args, **kwargs):
node = args[0]
# We don't want to wrap an object that is already wrapped
# It wouldn't break anything but nodes might get wrapped and wrapped again multiplying redirections...
if( not isinstance(node,ObjectWrapper) ):
node = ObjectWrapper(node)
if len(args)>1:
return method(node,*args[1:],**kwargs)
else:
return method(node,**kwargs)
return wrapper