-
Notifications
You must be signed in to change notification settings - Fork 0
/
epoch_manager.py
806 lines (629 loc) · 28.6 KB
/
epoch_manager.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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# this library is a heavy modified version of the original code from:
# "name": "Super Grouper",
# "author": "Paul Geraskin, Aleksey Juravlev, BA Community",
import bpy
import random
import string
from .functions import *
from bpy.props import *
from bpy.types import Operator
from bpy.types import Menu, Panel, UIList, PropertyGroup
from bpy.props import StringProperty, BoolProperty, IntProperty, CollectionProperty, BoolVectorProperty, PointerProperty
from bpy.app.handlers import persistent
SCENE_EM = '#EM'
UNIQUE_ID_NAME = 'em_belong_id'
class EM_BasePanel(bpy.types.Panel):
bl_label = "Epochs Manager"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_context = "objectmode"
bl_category = 'EM'
def draw(self, context):
layout = self.layout
scene = context.scene
sg_settings = scene.sg_settings
row = layout.row(align=True)
op = row.operator(
"epoch_manager.change_selected_objects", text="", emboss=False, icon='BBOX')
op.sg_objects_changer = 'BOUND_SHADE'
op = row.operator(
"epoch_manager.change_selected_objects", text="", emboss=False, icon='WIRE')
op.sg_objects_changer = 'WIRE_SHADE'
op = row.operator(
"epoch_manager.change_selected_objects", text="", emboss=False, icon='SOLID')
op.sg_objects_changer = 'MATERIAL_SHADE'
op = row.operator(
"epoch_manager.change_selected_objects", text="", emboss=False, icon='RETOPO')
op.sg_objects_changer = 'SHOW_WIRE'
op = row.operator(
"emset.emmaterial", text="", emboss=False, icon='MATERIAL')
#op.sg_objects_changer = 'EM_COLOURS'
row = layout.row()
row.template_list(
"EM_named_epoch_managers", "", scene, "epoch_managers", scene, "epoch_managers_index")
layout.label(text="Selection Settings:")
row = layout.row(align=True)
row.prop(sg_settings, "select_all_layers", text='Layers')
row.prop(sg_settings, "unlock_obj", text='UnLock')
row.prop(sg_settings, "unhide_obj", text='Unhide')
row = layout.row(align=True)
class EM_named_epoch_managers(UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
epoch_manager = item
# user_preferences = context.user_preferences
icons_style = 'OUTLINER'
if self.layout_type in {'DEFAULT', 'COMPACT'}:
layout.prop(epoch_manager, "name", text="", emboss=False)
# select operator
icon = 'RESTRICT_SELECT_OFF' if epoch_manager.use_toggle else 'RESTRICT_SELECT_ON'
#if icons_style == 'OUTLINER':
icon = 'VIEWZOOM' if epoch_manager.use_toggle else 'VIEWZOOM'
op = layout.operator(
"epoch_manager.toggle_select", text="", emboss=False, icon=icon)
op.group_idx = index
op.is_menu = False
op.is_select = True
# lock operator
icon = 'LOCKED' if epoch_manager.is_locked else 'UNLOCKED'
#if icons_style == 'OUTLINER':
icon = 'RESTRICT_SELECT_ON' if epoch_manager.is_locked else 'RESTRICT_SELECT_OFF'
op = layout.operator(
"epoch_manager.change_grouped_objects", text="", emboss=False, icon=icon)
op.sg_group_changer = 'LOCKING'
op.group_idx = index
# view operator
icon = 'RESTRICT_VIEW_OFF' if epoch_manager.use_toggle else 'RESTRICT_VIEW_ON'
op = layout.operator(
"epoch_manager.toggle_visibility", text="", emboss=False, icon=icon)
op.group_idx = index
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
class EM_Add_Objects_Sub_Menu(bpy.types.Menu):
bl_idname = "epoch_manager.add_objects_sub_menu"
bl_label = "Add Selected Objects"
bl_description = "Add Objects Menu"
def draw(self, context):
layout = self.layout
for i, e_manager in enumerate(context.scene.epoch_managers):
op = layout.operator(EM_add_to_group.bl_idname, text=e_manager.name)
op.group_idx = i
class EM_Remove_SGroup_Sub_Menu(bpy.types.Menu):
bl_idname = "epoch_manager.remove_e_manager_sub_menu"
bl_label = "Remove Super Group"
bl_description = "Remove Super Group Menu"
def draw(self, context):
layout = self.layout
for i, e_manager in enumerate(context.scene.epoch_managers):
op = layout.operator(EM_epoch_manager_remove.bl_idname, text=e_manager.name)
op.group_idx = i
class EM_Select_SGroup_Sub_Menu(bpy.types.Menu):
bl_idname = "epoch_manager.select_e_manager_sub_menu"
bl_label = "Select SGroup"
bl_description = "Select SGroup Menu"
def draw(self, context):
layout = self.layout
for i, e_manager in enumerate(context.scene.epoch_managers):
op = layout.operator(EM_toggle_select.bl_idname, text=e_manager.name)
op.group_idx = i
op.is_select = True
op.is_menu = True
class EM_Deselect_SGroup_Sub_Menu(bpy.types.Menu):
bl_idname = "epoch_manager.deselect_e_manager_sub_menu"
bl_label = "Deselect SGroup"
bl_description = "Deselect SGroup Menu"
def draw(self, context):
layout = self.layout
for i, e_manager in enumerate(context.scene.epoch_managers):
op = layout.operator(EM_toggle_select.bl_idname, text=e_manager.name)
op.group_idx = i
op.is_select = False
op.is_menu = True
class EM_Toggle_Visible_SGroup_Sub_Menu(bpy.types.Menu):
bl_idname = "epoch_manager.toggle_e_manager_sub_menu"
bl_label = "Toggle SGroup"
bl_description = "Toggle SGroup Menu"
def draw(self, context):
layout = self.layout
for i, e_manager in enumerate(context.scene.epoch_managers):
op = layout.operator(EM_toggle_visibility.bl_idname, text=e_manager.name)
op.group_idx = i
class EM_Toggle_Shading_Sub_Menu(bpy.types.Menu):
bl_idname = "epoch_manager.toggle_shading_sub_menu"
bl_label = "Toggle Shading"
bl_description = "Toggle Shading Menu"
def draw(self, context):
layout = self.layout
op = layout.operator(EM_change_selected_objects.bl_idname, text="Bound Shade")
op.sg_objects_changer = 'BOUND_SHADE'
op = layout.operator(EM_change_selected_objects.bl_idname, text="Wire Shade")
op.sg_objects_changer = 'WIRE_SHADE'
op = layout.operator(EM_change_selected_objects.bl_idname, text="Material Shade")
op.sg_objects_changer = 'MATERIAL_SHADE'
op = layout.operator(EM_change_selected_objects.bl_idname, text="Show Wire")
op.sg_objects_changer = 'SHOW_WIRE'
layout.separator()
op = layout.operator(EM_change_selected_objects.bl_idname, text="One Side")
op.sg_objects_changer = 'ONESIDE_SHADE'
op = layout.operator(EM_change_selected_objects.bl_idname, text="Double Side")
op.sg_objects_changer = 'TWOSIDE_SHADE'
def generate_id():
# Generate unique id
other_ids = []
for scene in bpy.data.scenes:
if scene != bpy.context.scene and scene.name.endswith(SCENE_EM) is False:
for e_manager in scene.epoch_managers:
other_ids.append(e_manager.unique_id)
while True:
uni_numb = None
uniq_id_temp = ''.join(random.choice(string.ascii_uppercase + string.digits)
for _ in range(10))
if uniq_id_temp not in other_ids:
uni_numb = uniq_id_temp
break
other_ids = None # clean
return uni_numb
class EM_clean_object_ids(bpy.types.Operator):
"""Remove selected layer group"""
bl_idname = "epoch_manager.clean_object_ids"
bl_label = "Clean Objects IDs if the objects were imported from other blend files"
bl_options = {'REGISTER', 'UNDO'}
# group_idx = bpy.props.IntProperty()
@classmethod
def poll(cls, context):
return bool(context.scene)
def execute(self, context):
check_same_ids() # check scene ids
scenes_ids = []
for scene in bpy.data.scenes:
if scene.epoch_managers:
for e_manager in scene.epoch_managers:
if e_manager.unique_id not in scenes_ids:
scenes_ids.append(e_manager.unique_id)
for obj in bpy.data.objects:
EM_del_properties_from_obj(UNIQUE_ID_NAME, scenes_ids, obj, False)
scenes_ids = None # clean
return {'FINISHED'}
def EM_get_group_scene(context):
group_scene_name = context.scene.name + SCENE_EM
if group_scene_name in bpy.data.scenes:
return bpy.data.scenes[group_scene_name]
return None
def EM_create_group_scene(context):
group_scene_name = context.scene.name + SCENE_EM
if context.scene.name.endswith(SCENE_EM) is False:
if group_scene_name in bpy.data.scenes:
return bpy.data.scenes[group_scene_name]
else:
return bpy.data.scenes.new(group_scene_name)
return None
def EM_select_objects(context, ids, do_select):
if do_select:
scene = context.scene
temp_scene_layers = list(scene.layers[:]) # copy layers of the scene
for obj in scene.objects:
if obj.em_belong_id:
for prop in obj.em_belong_id:
if prop.unique_id_object in ids:
for i in range(20):
if obj.layers[i] is True:
if scene.layers[i] is True or scene.sg_settings.select_all_layers:
# unlock
if scene.sg_settings.unlock_obj:
obj.hide_select = False
# unhide
if scene.sg_settings.unhide_obj:
obj.hide = False
# select
obj.select = True
# break if we need to select only visible
# layers
if scene.sg_settings.select_all_layers is False:
break
else:
temp_scene_layers[i] = obj.layers[i]
# set layers switching to a scene
if scene.sg_settings.select_all_layers:
scene.layers = temp_scene_layers
else:
for obj in context.selected_objects:
if obj.em_belong_id:
for prop in obj.em_belong_id:
if prop.unique_id_object in ids:
obj.select = False
class EM_toggle_select(bpy.types.Operator):
bl_idname = "epoch_manager.toggle_select"
bl_label = "Toggle Select"
bl_description = "Toggle Select"
bl_options = {'REGISTER', 'UNDO'}
group_idx = IntProperty()
is_menu = BoolProperty(name="Is Menu?", default=True)
is_select = BoolProperty(name="Is Select?", default=True)
def invoke(self, context, event):
scene = context.scene
if self.group_idx < len(scene.epoch_managers):
# check_same_ids() # check scene ids
e_manager = scene.epoch_managers[self.group_idx]
if event.ctrl is True and self.is_menu is False:
self.is_select = False
if e_manager.use_toggle is True:
if self.is_select is True:
# add active object if no selection
has_selection = False
if context.selected_objects:
has_selection = True
EM_select_objects(context, [e_manager.unique_id], True)
if scene.sg_settings.unlock_obj:
e_manager.is_locked = False
# set last active object if no selection was before
if has_selection is False and context.selected_objects:
scene.objects.active = context.selected_objects[-1]
else:
EM_select_objects(context, [e_manager.unique_id], False)
return {'FINISHED'}
class EM_toggle_visibility(bpy.types.Operator):
"""Draw a line with the mouse"""
bl_idname = "epoch_manager.toggle_visibility"
bl_label = "Toggle Visibility"
bl_description = "Toggle Visibility"
bl_options = {'REGISTER', 'UNDO'}
group_idx = IntProperty()
def execute(self, context):
scene = context.scene
if self.group_idx < len(scene.epoch_managers):
# check_same_ids() # check scene ids
current_e_manager = scene.epoch_managers[self.group_idx]
# Try to get or create new GroupScene
group_scene = EM_get_group_scene(context)
if group_scene is None and current_e_manager.use_toggle is True:
group_scene = EM_create_group_scene(context)
# if GroupScene exists now we can switch objects
if group_scene is not None:
if current_e_manager.use_toggle is True:
for obj in scene.objects:
EM_switch_object(
obj, scene, group_scene, current_e_manager.unique_id)
else:
for obj in group_scene.objects:
EM_switch_object(
obj, group_scene, scene, current_e_manager.unique_id)
if len(group_scene.objects) == 0:
bpy.data.scenes.remove(group_scene)
current_e_manager.use_toggle = not current_e_manager.use_toggle # switch visibility
# set active object so that WMenu worked
if current_e_manager.use_toggle is False and scene.objects.active is None:
if scene.objects:
scene.objects.active = scene.objects[0]
return {'FINISHED'}
def EM_switch_object(obj, scene_source, scene_terget, e_manager_id):
do_switch = False
if obj.em_belong_id:
for prop in obj.em_belong_id:
if prop.unique_id_object == e_manager_id:
do_switch = True
break
if do_switch is True:
layers = obj.layers[:] # copy layers
obj.select = False
# if object is not already linked
if obj.name not in scene_terget.objects:
obj2 = scene_terget.objects.link(obj)
obj2.layers = layers # paste layers
scene_source.objects.unlink(obj)
layers = None # clean
def sg_is_object_in_e_managers(groups_prop_values, obj):
is_in_group = False
for prop in obj.em_belong_id:
if prop.unique_id_object in groups_prop_values:
is_in_group = True
break
if is_in_group:
return True
else:
return False
class EM_change_grouped_objects(bpy.types.Operator):
bl_idname = "epoch_manager.change_grouped_objects"
bl_label = "Change Grouped"
bl_description = "Change Grouped"
bl_options = {'REGISTER', 'UNDO'}
sg_group_changer = EnumProperty(
items=(('COLOR_WIRE', 'COLOR_WIRE', ''),
('DEFAULT_COLOR_WIRE', 'DEFAULT_COLOR_WIRE', ''),
('LOCKING', 'LOCKING', '')
),
default = 'DEFAULT_COLOR_WIRE'
)
list_objects = ['LOCKING']
group_idx = IntProperty()
def execute(self, context):
scene_parse = context.scene
if scene_parse.epoch_managers:
# check_same_ids() # check scene ids
e_manager = None
if self.sg_group_changer not in self.list_objects:
e_manager = scene_parse.epoch_managers[
scene_parse.epoch_managers_index]
else:
if self.group_idx < len(scene_parse.epoch_managers):
e_manager = scene_parse.epoch_managers[self.group_idx]
if e_manager is not None and e_manager.use_toggle is True:
for obj in scene_parse.objects:
if sg_is_object_in_e_managers([e_manager.unique_id], obj):
if self.sg_group_changer == 'COLOR_WIRE':
r = e_manager.wire_color[0]
g = e_manager.wire_color[1]
b = e_manager.wire_color[2]
obj.color = (r, g, b, 1)
obj.show_wire_color = True
elif self.sg_group_changer == 'DEFAULT_COLOR_WIRE':
obj.show_wire_color = False
elif self.sg_group_changer == 'LOCKING':
if e_manager.is_locked is False:
obj.hide_select = True
obj.select = False
else:
obj.hide_select = False
# switch locking for the group
if self.sg_group_changer == 'LOCKING':
if e_manager.is_locked is False:
e_manager.is_locked = True
else:
e_manager.is_locked = False
return {'FINISHED'}
class EM_set_EM_materials(bpy.types.Operator):
bl_idname = "emset.emmaterial"
bl_label = "Change proxy materials"
bl_description = "Change proxy materials"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
update_icons(context)
set_EM_materials_using_EM_list(context)
return {'FINISHED'}
class EM_change_selected_objects(bpy.types.Operator):
bl_idname = "epoch_manager.change_selected_objects"
bl_label = "Change Selected"
bl_description = "Change Selected"
bl_options = {'REGISTER', 'UNDO'}
sg_objects_changer = EnumProperty(
items=(('BOUND_SHADE', 'BOUND_SHADE', ''),
('WIRE_SHADE', 'WIRE_SHADE', ''),
('MATERIAL_SHADE', 'MATERIAL_SHADE', ''),
('SHOW_WIRE', 'SHOW_WIRE', ''),
('EM_COLOURS', 'EM_COLOURS', ''),
('ONESIDE_SHADE', 'ONESIDE_SHADE', ''),
('TWOSIDE_SHADE', 'TWOSIDE_SHADE', '')
),
default = 'MATERIAL_SHADE'
)
sg_do_with_groups = [
'COLOR_WIRE', 'DEFAULT_COLOR_WIRE', 'LOCKED', 'UNLOCKED']
def execute(self, context):
for obj in context.selected_objects:
if self.sg_objects_changer == 'BOUND_SHADE':
obj.draw_type = 'BOUNDS'
obj.show_wire = False
elif self.sg_objects_changer == 'WIRE_SHADE':
obj.draw_type = 'WIRE'
obj.show_wire = False
elif self.sg_objects_changer == 'MATERIAL_SHADE':
obj.draw_type = 'TEXTURED'
obj.show_wire = False
elif self.sg_objects_changer == 'SHOW_WIRE':
obj.draw_type = 'TEXTURED'
obj.show_wire = True
elif self.sg_objects_changer == 'ONESIDE_SHADE':
if obj.type == 'MESH':
obj.data.show_double_sided = False
elif self.sg_objects_changer == 'TWOSIDE_SHADE':
if obj.type == 'MESH':
obj.data.show_double_sided = True
# elif self.sg_objects_changer == 'EM_COLOURS':
# if obj.type == 'MESH':
# set_EM_materials_using_EM_list(context)
return {'FINISHED'}
class EM_epoch_manager_add(bpy.types.Operator):
"""Add and select a new layer group"""
bl_idname = "epoch_manager.epoch_manager_add"
bl_label = "Add Epoch group"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return bool(context.scene)
def execute(self, context):
bpy.ops.epoch_manager.epoch_manager_remove()
scene = context.scene
epoch_number = len(scene.epoch_list)
for epoch in range(epoch_number):
epochname = scene.epoch_list[epoch].name
check_same_ids() # check scene ids
epoch_managers = scene.epoch_managers
# get all ids
all_ids = []
for e_manager in epoch_managers:
if e_manager.unique_id not in all_ids:
all_ids.append(e_manager.unique_id)
# generate new id
uni_numb = generate_id()
all_ids = None
group_idx = len(epoch_managers)
new_e_manager = epoch_managers.add()
new_e_manager.name = epochname
new_e_manager.unique_id = uni_numb
scene.epoch_managers_index = group_idx
return {'FINISHED'}
class EM_epoch_manager_remove(bpy.types.Operator):
"""Remove selected layer group"""
bl_idname = "epoch_manager.epoch_manager_remove"
bl_label = "Clear all epochs"
bl_options = {'REGISTER', 'UNDO'}
group_idx = IntProperty()
@classmethod
def poll(cls, context):
return bool(context.scene)
def execute(self, context):
scene_parse = context.scene
epoch_num = len(scene_parse.epoch_managers)
for i in range(epoch_num):
self.group_idx = scene_parse.epoch_managers_index
# if a scene contains goups
if scene_parse.epoch_managers:
check_same_ids() # check scene ids
get_e_manager = scene_parse.epoch_managers[self.group_idx]
if get_e_manager is not None and self.group_idx < len(scene_parse.epoch_managers):
e_manager_id = get_e_manager.unique_id
# get all ids
e_managers = []
for e_manager in scene_parse.epoch_managers:
e_managers.append(e_manager.unique_id)
# clear context scene
for obj in scene_parse.objects:
EM_del_properties_from_obj(
UNIQUE_ID_NAME, [e_manager_id], obj, True)
# clear SGR scene
sgr_scene_name = scene_parse.name + SCENE_EM
if sgr_scene_name in bpy.data.scenes:
sgr_scene = bpy.data.scenes[scene_parse.name + SCENE_EM]
for obj in sgr_scene.objects:
SGR_switch_object(obj, sgr_scene, scene_parse, e_manager_id)
EM_del_properties_from_obj(
UNIQUE_ID_NAME, [e_manager_id], obj, True)
# remove group_scene if it's empty
if len(sgr_scene.objects) == 0:
bpy.data.scenes.remove(sgr_scene)
# finally remove e_manager
scene_parse.epoch_managers.remove(self.group_idx)
if len(scene_parse.epoch_managers) > 0:
scene_parse.epoch_managers_index = len(scene_parse.epoch_managers) - 1
else:
scene_parse.epoch_managers_index = -1
# self.group_idx = scene_parse.epoch_managers_index
return {'FINISHED'}
class EM_add_to_group(bpy.types.Operator):
bl_idname = "epoch_manager.add_to_group"
bl_label = "Add Selected Objects"
bl_description = "Add To Super Group"
bl_options = {'REGISTER', 'UNDO'}
group_idx = IntProperty()
def execute(self, context):
scene_parse = context.scene
if scene_parse.epoch_managers:
check_same_ids() # check ids
# remove e_managers
ids = []
for e_manager in scene_parse.epoch_managers:
ids.append(e_manager.unique_id)
for obj in context.selected_objects:
for e_manager in scene_parse.epoch_managers:
EM_del_properties_from_obj(UNIQUE_ID_NAME, ids, obj, True)
ids = None
e_manager = scene_parse.epoch_managers[self.group_idx]
if e_manager is not None and self.group_idx < len(scene_parse.epoch_managers):
for obj in context.selected_objects:
# add the unique id of selected group
EM_add_property_to_obj(e_manager.unique_id, obj)
# switch locking for obj
if e_manager.is_locked is True:
obj.hide_select = True
obj.select = False
else:
obj.hide_select = False
# check if the group is hidden
if e_manager.use_toggle is False:
# Try to get or create new GroupScene
group_scene = EM_get_group_scene(context)
if group_scene is None:
group_scene = EM_create_group_scene(context)
# Unlink object
if group_scene is not None:
group_scene.objects.link(obj)
context.scene.objects.unlink(obj)
return {'FINISHED'}
class EM_remove_from_group(bpy.types.Operator):
bl_idname = "epoch_manager.super_remove_from_group"
bl_label = "Remove Selected Objects"
bl_description = "Remove from Super Group"
bl_options = {'REGISTER', 'UNDO'}
# group_idx = bpy.props.IntProperty()
def execute(self, context):
scene = context.scene
if scene.epoch_managers:
check_same_ids() # check ids
# get all ids
e_managers = []
for e_manager in scene.epoch_managers:
e_managers.append(e_manager.unique_id)
# remove e_managers
for obj in context.selected_objects:
EM_del_properties_from_obj(UNIQUE_ID_NAME, e_managers, obj, True)
e_managers = None # clear
return {'FINISHED'}
def EM_add_property_to_obj(prop_name, obj):
props = obj.em_belong_id
has_value = False
if props:
for prop in props:
if prop.unique_id_object == prop_name:
has_value = True
break
# add the value if it does not exist
if has_value == False:
added_prop = props.add()
added_prop.unique_id_object = prop_name
def EM_del_properties_from_obj(prop_name, e_managers_ids, obj, delete_in_e_managers=True):
props = obj.em_belong_id
if len(props.values()) > 0:
# remove item
prop_len = len(props)
index_prop = 0
for i in range(prop_len):
prop_obj = props[index_prop]
is_removed = False
if prop_obj.unique_id_object in e_managers_ids and delete_in_e_managers == True:
props.remove(index_prop)
is_removed = True
elif prop_obj.unique_id_object not in e_managers_ids and delete_in_e_managers == False:
props.remove(index_prop)
is_removed = True
if is_removed is False:
index_prop += 1
if len(props.values()) == 0:
del bpy.data.objects[obj.name][prop_name]
def check_same_ids():
scenes = bpy.data.scenes
current_scene = bpy.context.scene
check_scenes = []
for scene in scenes:
if scene.name.endswith(SCENE_EM) is False and scene != current_scene:
check_scenes.append(scene)
if check_scenes:
other_ids = []
for scene in check_scenes:
for e_manager in scene.epoch_managers:
if e_manager.unique_id not in other_ids:
other_ids.append(e_manager.unique_id)
all_obj_list = None
if other_ids:
for i in range(len(current_scene.epoch_managers)):
current_e_manager = current_scene.epoch_managers[i]
current_id = current_e_manager.unique_id
if current_id in other_ids:
new_id = generate_id()
if all_obj_list is None:
all_obj_list = []
all_obj_list += current_scene.objects
group_scene = EM_get_group_scene(bpy.context)
if group_scene is not None:
all_obj_list += group_scene.objects
for obj in all_obj_list:
has_id = False
for prop in obj.em_belong_id:
if prop.unique_id_object == current_e_manager.unique_id:
has_id = True
break
if has_id == True:
EM_add_property_to_obj(new_id, obj)
# set new id
current_e_manager.unique_id = new_id
# clean
check_scenes = None
all_obj_list = None
other_ids = None