-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
553 lines (437 loc) · 16 KB
/
base.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import bpy, mathutils, copy
from . import Blender
from .utils import *
import sys,os
from lib import AttrDict
from _ctypes import ArgumentError
class Base(object):
__slots__ = ['_cache', '_bpy', '_ipokeys']
__overwrites__ = ['copy']
def __init__(self, init = None, *args, **kw):
self._cache = AttrDict()
self._bpy = None
self._ipokeys = []
if isinstance(init, Base):
self._bpy = init._bpy
self._cache = init._cache
self._ipokeys = init._ipokeys
elif isinstance(init, AttrDict):
self._cache = init.copy()
elif isinstance(init, bpy.types.ID):
self._bpy = init
elif isinstance(init, str):
self.name = init
#if self.created:
for k,v in AttrDict(kw).attr_items():
if k not in ('ipokeys'):
#print("BASE SETATTR", self, self.created, k, v)
setattr(self, k, v)
if 'ipokeys' in kw:
self.addAutoKey(kw['ipokeys'])
@property
def created(self): return self._bpy != None
@property
def cache(self): return self._cache
@property
def bpy(self): return self._bpy
@property
def _obj(self):
if self.created: return self._bpy
else: return self._cache
@_obj.deleter
def _obj(self):
if self.created:
del self._bpy
self._bpy = None
@_obj.setter
def _obj(self, value):
if not self.created and value != None:
self._bpy = value
for k, v in self.cache.attr_items():
#set_blender_object_property()
setattr(self._bpy, k, v)
del self._cache
self._cache = AttrDict()
else:
del self._obj
@classmethod
def _get_slots(cls):
slots = cls.__overwrites__[:] + cls.__slots__[:]
for b in cls.__bases__:
if issubclass(b, Base):
slots += b._get_slots()
return slots
def __repr__(self):
return repr(self._bpy) if self.created else ("<Base(%s)>" (repr(self.cache)) )
def __getattr__(self, name):
try:
#print("NULL")
if name[0] != '_' and name not in self._get_slots():
#print("EINS", name)
return self.getobjattr(name)
else:
#print("ZWEI", name, type(self).__name__)
return super().__getattr__(self, name)
except:
raise AttributeError(name)
def __setattr__(self, name, value):
#print("NAMENAME", name, value)
try:
if name[0] != '_' and name not in self._get_slots():
#print("EINS")
self.setobjattr(name, value)
else:
#print("ZWEI")
super().__setattr__(name, value)
except:
pass
#raise AttributeError(name)
def setobjattr(self, name, value):
if self._bpy != None:
old = self.getobjattr(name)
if not isinstance(old, Base):
#print(name, self.getobjattr(name))
oldvalue = copy.deepcopy(self.getobjattr(name))
else:
oldvalue = None
if isinstance(value, Base):
if not value.created: value.create()
value = value._bpy
#print("BPYSET ", name, value, oldvalue)
set_blender_object_property(self._bpy, name, value)
if oldvalue != None and oldvalue != value:
self.notify(name)
else:
setattr(self.cache, name, value)
def getobjattr(self, name, default = None):
try:
if self._bpy != None:
val = get_blender_object_property(self._bpy,name)
else:
val = self.cache[name]
except (KeyError, ArgumentError):
val = default
#print("BPYGET", name, val)
if isinstance(val, bpy.types.Object):
val = Object(val)
elif isinstance(val, bpy.types.Material):
val = Material(val)
elif isinstance(val, bpy.types.ID):
val = Base(val)
return val
def addAutoKey(self, *args):
for a in args:
if a not in self._ipokeys:
if isinstance(a, (tuple, list)):
self.addAutoKey(*tuple(a))
elif isinstance(a, str):
self._ipokeys.append(a)
self.notify(a)
return self
def notify(self, *args, **kw):
if len(args) == 0:
self.notify(*self._ipokeys, **kw)
else:
for name in args:
if Blender.auto_animate() and name in self._ipokeys:
self.keyframe_insert(name, **kw)
def create(self, *args, **kw):
for k,v in AttrDict(kw).attr_items():
try:
#print("set onCreate", k, v)
setattr(self, k, v)
except: raise
return self
def update(self, *args, **kw):
for k,v in AttrDict(kw).attr_items():
try:
setattr(self, k, v)
except: pass
return self
def copy(self, *args, **kw):
clone = self.__class__(None, *args, **kw)
if self._bpy != None:
clone._bpy = self._bpy.copy()
else:
clone._cache = copy.copy(self.cache)
clone._ipokeys = copy.copy(self._ipokeys)
return clone
def keyframe_insert(self, data_path, index=-1, frame=None, group=""):
if self.created:
frame = Blender.getFrame() if frame == None or frame == 'CURRENT' or frame == -1 else frame
if data_path == None or data_path == 'ALL':
for p in data_path:
self.keyframe_insert(p, index, frame, group)
elif isinstance(data_path, str):
print("KEYFRAME")
insert_blender_keyframe(self._bpy, data_path, index, frame, group)
return self
class LazyBase(Base):
__slots__ = []
__overwrites__ = []
def __init__(self, init, *args, **kw):
super().__init__(init, *args, **kw)
@property
def _obj(self): return super()._obj
@_obj.deleter
def _obj(self): del super()._obj
@_obj.setter
def _obj(self, value):
if not self.created and value != None:
self._bpy = value
for k, v in self.cache.attr_items():
self.setobjattr(k, v)
else:
del self._obj
def __getattr__(self, name):
try:
if name[0] != '_' and name not in self._get_slots():
return self.cache[name]
else:
return super().__getattr__(name)
except (KeyError, AttributeError):
print(self.cache, name in self.cache)
raise AttributeError(name)
def __setattr__(self, name, value):
try:
if name[0] != '_' and name not in self._get_slots():
self.cache[name] = value
else:
super().__setattr__(name, value)
except (KeyError, AttributeError):
raise AttributeError(name)
def __repr__(self):
return "<LazyBase(%s)>" % (str(super().cache)) \
if self.created else "<LazyBase(%s), %s>" % (str(super().cache), self._bpy)
def copy(self, *args, **kw):
return super().copy(*args, **kw)
def remove(self, *args, **kw):
return super().remove(*args, **kw)
def create(self, *args, **kw):
super().create(*args, **kw)
for k, v in self.cache.attr_items():
self.setobjattr(k, v)
return self
def update(self, *args, **kw):
super().update(*args, **kw)
for k, v in self.cache.attr_items():
self.setobjattr(k, v)
return self
class Material(Base):
__slots__ = []
__overwrites__ = ['alpha']
def __init__(self, init, *args, **kw):
super().__init__(init, *args, **kw)
def __repr__(self):
return repr(self._obj) if self.created else ("<Material(%s)>" % (self.cache))
def remove(self):
if self.created:
self.data.user_clear()
bpy.data.materials.remove(self._obj.data)
self._bpy = None
return self
def __getitem__(self, name):
return get_blender_object_property(self._obj, name)
def __setitem__(self, name, value):
return set_blender_object_property(self._obj, name, value)
@property
def alpha(self): return super().getobjattr('alpha')
@alpha.setter
def alpha(self, value):
self.setobjattr('alpha', float(value))
if value < 1. and not self.getobjattr('use_transparency'):
self.setobjattr('use_transparency', True)
self.setobjattr('transparency_method', 'Z_TRANSPARENCY')
class LazyMaterial(LazyBase):
__slots__ = []
__overwrites__ = ['alpha']
def __init__(self, init = None, *args, **kw):
if isinstance(init, str):
kw['name'] = init
super().__init__(None, *args, **kw)
def __repr__(self):
return "<LazyMaterial(%s)>" % (self.cache) \
if self.created else "<LazyMaterial(%s), %s>" % (self.cache, self._bpy)
def create(self, *args, **kw):
kw = {"diffuse_color": (.2, .5, .2)}
print("KWLM", kw)
self.cache.update(**kw)
if not self.created:
name = 'LazyMaterial' if 'name' not in self.cache else self.name
self._obj = bpy.data.materials.new(name)
return super().create(*args, **kw)
def update(self, *args, **kw):
self._obj.update(*args, **kw)
return self
@property
def alpha(self): return super().getobjattr('alpha')
@alpha.setter
def alpha(self, value):
self.setobjattr('alpha', float(value))
if value < 1. and not self.getobjattr('use_transparency'):
self.setobjattr('use_transparency', True)
self.setobjattr('transparency_method', 'Z_TRANSPARENCY')
class Object(Base):
__slots__ = []
__overwrites__ = ['parent', 'children', 'hide', 'location', 'scale', 'rotation_euler', 'material']
def __init__(self, init = None, *args, **kw):
super().__init__(init, *args, **kw)
def __repr__(self):
return repr(self._obj) if self.created else "<Object()>"
def remove(self, remove_data = True, remove_material = False, do_unlink = True):
_data_switch = {
'LAMP': bpy.data.lights,
'MESH': bpy.data.meshes,
'CAMERA': bpy.data.cameras,
'CURVE': bpy.data.curves,
'OBJECT': bpy.data.objects,
'MATERIALS': bpy.data.materials,
'FONT': bpy.data.fonts,
#'SURFACE': bpy.data.surfaces
}
if self.created:
if self._obj.data and remove_data:
self.data.user_clear()
if self.type in _data_switch:
_data_switch[self.type].remove(self._bpy.data)
if do_unlink:
try:
Blender.objects.unlink(self._bpy)
except:
print ("Can not unlink ", self._bpy.name)
del self._obj
return self
@property
def linked(self):
#print(self.name)
return self.created and self.name in Blender.context.collection.objects
def link(self, obj = None):
if self.created and obj:
raise RuntimeError("Object %s is already created" % self.name)
if obj != None:
self._obj = obj
if self.created and not self.linked:
#bpy.context.collection.objects.link(self._bpy)
Blender.context.collection.objects.link(self._bpy)
return self
def copy(self, do_link = True, *args, **kw):
copy_data = 'copy_data' in kw and kw['copy_data']
copy_mat = 'copy_material' in kw and kw['copy_material']
clear_anim = 'clear_anim' not in kw or kw['clear_anim']
clone = super().copy(*args, **kw)
if self._bpy:
if copy_data and self.data:
clone._bpy.data = self._bpy.data.copy()
elif copy_mat and self.material:
clone.material = self.material.copy()
if clear_anim:
clone._bpy.animation_data_clear()
if do_link:
clone.link()
#clone.parent = self.parent
return clone
@property
def material(self):
return self.getobjattr('active_material')
@material.setter
def material(self, value):
if isinstance(value, LazyMaterial):
if not value.created: value.create()
self.setobjattr('active_material', value._obj)
elif isinstance(value, Material):
self.setobjattr('active_material', value._obj)
elif isinstance(value, bpy.types.Material):
self.setobjattr('active_material', value)
else:
raise ValueError(repr(value))
@property
def children(self):
objs = []
for c in self.getobjattr('children'):
objs.append(Object(c))
return tuple(objs)
@property
def parent(self): return self.getobjattr('parent')
@parent.setter
def parent(self, value):
if isinstance(value, Object):
self.setobjattr('parent', value._obj)
elif isinstance(value, bpy.types.Object):
self.setobjattr('parent', value)
else:
raise ValueError(repr(value))
@property
def scale(self): return self.getobjattr('scale')
@scale.setter
def scale(self, value):
if isinstance(value, (int,float)):
value = float(value)
value = (value, value, value)
self.setobjattr('scale', mathutils.Vector(value))
@property
def location(self): return self.getobjattr('location')
@location.setter
def location(self, value):
if isinstance(value, (int,float)):
value = float(value)
value = (value, value, value)
self.setobjattr('location', mathutils.Vector(value))
@property
def rotation_euler(self): return self.getobjattr('rotation_euler')
@rotation_euler.setter
def rotation_euler(self, value):
if isinstance(value, (int,float)):
value = float(value)
value = (value, value, value)
self.setobjattr('rotation_euler', value)
@property
def hide(self): return self.getobjattr('hide_viewport') and self.getobjattr('hide_render')
@hide.setter
def hide(self, value):
self.setobjattr('hide_viewport', value)
self.setobjattr('hide_render', value)
def setActive(self):
bpy.context.scene.objects.active = self._obj
class LazyObject(LazyBase):
__slots__ = ['_generator']
def __init__(self, init = None, *args, **kw):
super().__init__(init, *args, **kw)
self._generator = init
def create(self, *args, **kw):
if not self.created and self._generator:
name = 'LazyObject' if 'name' not in self else self.name
self._obj = self._generator(name, **self)
self.update(*args, **kw)
return self
def update(self, *args, **kw):
self._obj.update(*args, **kw)
return self
class DummyObject(Object):
__slots__ = []
__overwrites__ = []
def __init__(self, init = None, *args, **kw):
super().__init__(None, *args, **kw)
if init != None:
self.name = str(init)
@property
def created(self): return True
@property
def _obj(self): return self
@_obj.setter
def _obj(self, _): pass
def __repr__(self):
return "<DummyObject(%s)>" % self.cache
def remove(self):
self.cache.clear()
def create(self, *args, **kw):
return super().update(*args, **kw)
def update(self, *args, **kw):
return super().update(*args, **kw)
def keyframe_insert(self, *args, **kw):
pass
def Empty(name = 'Empty', type = 'PLAIN_AXES', location = (0., 0., 0.), **kw):
bpy.ops.object.empty_add(type=type, \
radius=1., location=location)
obj = Blender.active_object
obj.name = name
return Object(obj, **kw)