-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollections.py
188 lines (144 loc) · 5.03 KB
/
collections.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import bpy
from . import Blender
from .base import Object, Empty, Material, LazyMaterial
class ObjectCollection(Object):
__slots__ = ['_objects']
__overwrites__ = ['hide', 'select']
def __init__(self, init = None, *args, **kw):
if not init:
init = 'ObjectCollection'
if isinstance(init, str):
if init == "BOND":
self._objects = []
super().__init__(init, **kw)
print("NAME", init)
# We need to initialize the base to set _bpy
super().__init__(init, **kw)
pass
#super().__init__(Empty(init), **kw)
else:
super().__init__(init, **kw)
self._objects = []
for a in args:
self.append(a)
@property
def back(self):
print("OBJECTS",self.objects)
return self._objects[-1]
@property
def front(self): return self._objects[0]
def append(self, obj):
print("APPEND")
if isinstance(obj, Object):
self._objects.append(obj)
else:
self._objects.append(Object(obj))
if self.created and self.back.created:
print("SELF_BACK_PARENT")
self.back.parent = self._bpy
print("SELF_BACK_PARENT_END")
def __len__(self):
return len(self._objects)
def __iter__(self):
return iter(self._objects)
def __delitem__(self, i):
self._objects[i].remove()
del self._objects[i]
def __getitem__(self, i):
return self._objects[i]
def __setitem__(self, i, v):
self._objects[i].parent = None
self._objects[i] = v
if self.created:
self._objects[i].parent = self._bpy
@property
def hide(self):
value = self.getobjattr('hide_viewport') and self.getobjattr('hide_render')
for o in self:
value = value and o.hide
return value
@hide.setter
def hide(self, value):
for o in self:
o.hide = bool(value)
self.setobjattr('hide_viewport', bool(value))
self.setobjattr('hide_render', bool(value))
@property
def select(self):
value = self.getobjattr('select')
for o in self:
value = value and o.select
return value
@select.setter
def select(self, value):
for o in self:
o.select= bool(value)
self.setobjattr('select', bool(value))
self.setActive()
def copy(self, **kw):
clone = super().copy(**kw)
for c in self:
clone.append(c.copy(**kw))
return clone
def create(self, *args, **kargs):
super().create(*args, **kargs)
for c in self:
c.create(*args, **kargs)
if self.created and c.created:
c.parent = self._bpy
return self
def update(self, *args, **kargs):
super().update(*args, **kargs)
for c in self:
c.update(*args, **kargs)
return self
class MaterialCollection(object):
default_props = {
'diffuse_color': (.2, .2, .2)
}
def __init__(self, **kw):
self.__materials = {}
self.lazy = 'lazy' in kw and kw['lazy']
self.always_clone = 'always_clone' in kw and kw['always_clone']
for k, v in kw.items():
if k not in ('always_clone', 'lazy'):
self.default_props[k] = v
def setDefault(self, name, value):
self.default_props[name] = value
def __getitem__(self, name):
if name not in self.__materials:
if self.lazy:
self.new(name)
else: raise KeyError(name)
if not self.__materials[name].created:
self.__materials[name].create(**self.default_props)
return self.__materials[name]
def __setitem__(self, name, value):
if name in self.__materials:
self.remove(name)
if isinstance(value, Material):
self.__materials[name] = value.copy() if self.always_clone else value
elif isinstance(value, bpy.types.Material):
self.__materials[name] = Material(name)
self.__materials[name]._obj = value.copy() if self.always_clone else value
#self.__materials[name].update(**self.default_props)
def new(self, key, name = None, **kargs):
if name == None: name = str(key)
if self.lazy:
self.__materials[key] = LazyMaterial(name, **self.default_props)
else:
self.__materials[key] = Material(name, **self.default_props)
self.__materials[key].update(**kargs)
return self.__materials[key]
def remove(self, name, do_unlink = True):
if name in self.__materials:
self.__materials[name].remove(do_unlink = do_unlink)
return self
def update(self):
for mat in self.__materials.values():
mat.update(**self.default_props)
def copy(self, **kw):
clone = self.__class__()
for k,v in self.__materials.items():
clone[k] = v.copy(**kw)
return clone