-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoperators.py
2071 lines (1831 loc) · 72.5 KB
/
operators.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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import bpy
import os
from bpy.types import Operator
from .functions import *
from .template import Armature_Templates as AT
class AT_OT_rename_skeleton_bones(Operator):
bl_idname = "at.rename_bones"
bl_label = "Rename Bones"
bl_description = "Rename the active armature bones if they match in the template list as a defined naming convention"
bl_options = {'REGISTER', 'UNDO'}
target: bpy.props.EnumProperty(
name="Target List",
items=[
('LEFT', 'Left', 'Rename the left list of the bones'),
('RIGHT', 'Right', 'Rename the right list of the bones')
],
default='RIGHT'
)
def draw(self, context):
layout = self.layout
row = layout.row(align=True)
row.label(text='', icon='TRACKING_BACKWARDS_SINGLE')
row.prop(self, 'target', expand=True)
row.label(text='', icon='TRACKING_FORWARDS_SINGLE')
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self, width=250)
def execute(self, context):
obj = context.active_object
bone_data = get_mapped_bones_data(context)
bone_names = [b.name for b in obj.data.bones]
if self.target == 'LEFT':
for name in bone_data:
bone_name = bone_data.get(name)
if name not in bone_names:
continue
bone = obj.data.bones.get(name)
if bone and bone_name:
bone.name = bone_name
else:
for name in bone_data:
bone_name = bone_data.get(name)
if bone_name not in bone_names:
continue
bone = obj.data.bones.get(bone_name)
if bone and name:
bone.name = name
# update skin modifier for 3.0+
for ob in bpy.data.objects:
if ob.type == 'MESH':
ob.data.update()
self.report({'INFO'}, "Skeleton bones has been renamed.")
return {'FINISHED'}
class AT_OT_save_bone_mapping(Operator):
bl_idname = "at.save_bone_mapping"
bl_label = "Save Bone Mapping"
bl_description = "Save mapping file to a specified location"
save_to: bpy.props.EnumProperty(
name="Where to save",
items=[
('DEFAULT', 'Config Folder', 'Save the mapping file to the user config folder of the add-on'),
('EXTERNAL', 'External Location', 'Save a separate file to a specified location')
],
default='DEFAULT'
)
overwrite: bpy.props.BoolProperty(
name="Overwrite",
default=False
)
mapping: bpy.props.EnumProperty(
name="Bone Mapping",
items=bone_mapping_to_overwrite_enum
)
name: bpy.props.StringProperty(
name="New Name",
default='new'
)
def execute(self, context):
if self.save_to == 'EXTERNAL':
return {'FINISHED'}
if self.name == '' and not self.overwrite:
self.report({'ERROR'}, "The file name can't be empty!")
return {'FINISHED'}
file_name = check_extension(self.name)
native_mapping_path = get_native_mapping_path(props(context).templates)
config_mapping_path = get_config_mapping_path(props(context).templates)
if self.overwrite:
json_file_path = os.path.join(
config_mapping_path, self.mapping + ".json"
)
if not os.path.isfile(json_file_path):
json_file_path = os.path.join(
native_mapping_path, self.mapping + ".json"
)
else:
json_file_path = os.path.join(config_mapping_path, file_name)
save_data = get_mapped_bones_data(context)
save_json_data(json_file_path, save_data)
return {'FINISHED'}
def draw(self, context):
layout = self.layout
row = layout.row()
row.prop(self, 'save_to', expand=True)
if self.save_to == 'EXTERNAL':
row = layout.row()
row.operator('at.save_bone_mapping_to', text='Browse', icon='FILEBROWSER')
return None
row = layout.row()
row.prop(self, 'overwrite')
subrow = row.row()
subrow.enabled = self.overwrite
subrow.scale_x = 1.5
subrow.prop(self, 'mapping', text='')
row = layout.row()
row.enabled = not self.overwrite
row.label(text='New Name')
subrow = row.row()
subrow.scale_x = 1.5
subrow.prop(self, "name", text='')
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self, width=250)
class AT_OT_load_bone_mapping(Operator):
bl_idname = "at.load_bone_mapping"
bl_label = "Load Bone Mapping"
bl_description = "Load template mapping from a specified file"
directory: bpy.props.StringProperty(
subtype="DIR_PATH"
)
filename: bpy.props.StringProperty(
subtype="FILE_NAME",
)
filepath: bpy.props.StringProperty(
subtype="FILE_PATH"
)
browse_path: bpy.props.EnumProperty(
name="Browse Path",
items=[
('CONFIG', 'User Config', 'Browse to the user config directory', 0),
('NATIVE', 'Native', 'Browse to the native directory of the add-on installation', 1),
('BLEND', 'Blend File', 'Browse to the directory of the current blend file', 2),
],
update=update_browse_path,
default=0
)
def draw(self, context):
layout = self.layout
col = layout.column(align=True)
col.use_property_split = True
col.prop(self, 'browse_path', expand=True)
def invoke(self, context, event):
self.filename = ".json"
self.directory = os.path.join(get_config_path(), "bone_mapping")
validate_path(self.directory)
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
if not self.filepath.endswith(".json"):
msg = "Selected file has to be a .json file"
self.report({'ERROR'}, msg)
return {'FINISHED'}
props(context)['bone_mapping'] = 0
file_data = get_json_data(self.filepath)
apply_bone_mapping_data(context, file_data)
redraw_area('PROPERTIES')
return {'FINISHED'}
class AT_OT_map_selected_bone(Operator):
bl_idname = "at.map_selected_bone"
bl_label = "Map Selected Bone"
bl_description = "Map a link to the bone that currently in the active selection"
prop_name: bpy.props.StringProperty()
def execute(self, context):
if self.prop_name == '':
return {'FINISHED'}
obj = context.active_object
mode = context.mode.split('_')[0]
if mode == 'POSE':
bones = obj.data.bones
else:
bones = obj.data.edit_bones
if not bones.active:
return {'FINISHED'}
search_props = context.window_manager.at_search_list_props
prop = search_props.get(self.prop_name)
prop.bone = bones.active.name
redraw_area('PROPERTIES')
return {'FINISHED'}
class AT_OT_map_custom_bone_name(Operator):
bl_idname = "at.map_custom_bone_name"
bl_label = "Map Custom Name"
bl_description = "Type a name in the popup window to map a custom bone"
prop_name: bpy.props.StringProperty()
custom_name: bpy.props.StringProperty(name="Custom Name")
def invoke(self, context, event):
search_props = context.window_manager.at_search_list_props
self.prop = search_props.get(self.prop_name)
self.custom_name = self.prop.bone
return context.window_manager.invoke_props_dialog(self, width=250)
def draw(self, context):
layout = self.layout
col = layout.column()
col.use_property_split = True
col.prop(self, 'custom_name')
def execute(self, context):
if self.prop_name == '':
return {'FINISHED'}
self.prop.bone = self.custom_name
redraw_area('PROPERTIES')
return {'FINISHED'}
class AT_OT_save_bone_mapping_to(Operator):
bl_idname = "at.save_bone_mapping_to"
bl_label = "Save Bone Mapping"
bl_description = "Browse folders to save bone mapping file"
directory: bpy.props.StringProperty(
subtype="DIR_PATH"
)
filename: bpy.props.StringProperty(
subtype="FILE_NAME",
default="new mapping"
)
filepath: bpy.props.StringProperty(
subtype="FILE_PATH"
)
browse_path: bpy.props.EnumProperty(
name="Browse Path",
items=[
('CONFIG', 'User Config', 'Browse to the user config directory', 0),
('NATIVE', 'Native', 'Browse to the native directory of the add-on installation', 1),
('BLEND', 'Blend File', 'Browse to the directory of the current blend file', 2),
],
update=update_browse_path,
default=2
)
def draw(self, context):
layout = self.layout
col = layout.column(align=True)
col.use_property_split = True
col.prop(self, 'browse_path', expand=True)
def invoke(self, context, event):
self.filename = "new mapping"
if bpy.data.filepath:
self.directory = os.path.dirname(bpy.data.filepath)
else:
self.directory = os.path.expanduser('~')
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
if not self.filename:
msg = "The file name is empty"
self.report({'ERROR'}, msg)
return {'FINISHED'}
if not self.filepath.endswith(".json"):
self.filepath += ".json"
save_data = get_mapped_bones_data(context)
save_json_data(self.filepath, save_data)
msg = "The mapping file has been saved to: " + self.filepath
self.report({'INFO'}, msg)
return {'FINISHED'}
class AT_OT_remove_bone_mapping(Operator):
bl_idname = "at.remove_bone_mapping"
bl_label = "Remove Bone Mapping"
bl_description = "Remove the current file of the bone mapping"
def invoke(self, context, event):
if props(context).bone_mapping == 'custom':
update_apply_bone_mapping(props(context), context)
return {'FINISHED'}
return context.window_manager.invoke_confirm(self, event)
def execute(self, context):
native_path = get_native_mapping_path(props(context).templates)
config_path = get_config_mapping_path(props(context).templates)
file_path = os.path.join(config_path, props(context).bone_mapping + ".json")
if os.path.isfile(file_path):
os.remove(file_path)
props(context)['bone_mapping'] = 0
return {'FINISHED'}
file_path = os.path.join(native_path, props(context).bone_mapping + ".json")
if os.path.isfile(file_path):
props(context)['bone_mapping'] = 0
os.remove(file_path)
return {'FINISHED'}
msg = "Armature Templates: The mapping file '" + props(context).bone_mapping + "' does not exist"
self.report({'ERROR'}, msg)
return {'FINISHED'}
class AT_OT_create_template_category(Operator):
bl_idname = "at.create_template_category"
bl_label = "Create Template Category"
bl_description = "Create a new category in the template for bones"
name: bpy.props.StringProperty(name="Name")
def execute(self, context):
if AT.template_data.get(self.name):
return {'FINISHED'}
AT.new_category(self.name)
return {'FINISHED'}
class AT_OT_remove_template_category(Operator):
bl_idname = "at.remove_template_category"
bl_label = " Remove Template Category"
bl_description = "Remove the category in the template and move the containing bones to the source list"
name: bpy.props.StringProperty(name="Name")
def execute(self, context):
AT.template_data.pop(self.name)
AT.bone_list = evaluate_bone_list(
context, context.active_object, AT.template_data
)
set_template_item_list(AT.bone_list)
if self.name == AT.active_category:
set_template_category_list([])
return {'FINISHED'}
class AT_OT_template_categories(Operator):
bl_idname = "at.template_categories"
bl_label = "Template Category"
bl_description = "Select the category to append items in it"
name: bpy.props.StringProperty(name="Name")
def execute(self, context):
AT.active_category = self.name
set_template_category_list(AT.get_category_items())
return {'FINISHED'}
class AT_OT_add_selected_to_template(Operator):
bl_idname = "at.add_selected_to_template"
bl_label = "Add"
bl_description = "Add selected items in the active category"
def execute(self, context):
if not AT.bone_list:
return {'FINISHED'}
if not AT.get_category_items():
AT.new_category()
item_list = context.window_manager.at_template_item_list
add_list = [i.name for i in item_list if i.flag]
if not add_list:
add_list = [item_list[props(context).active_temp_item_list].name]
AT.append_to_category(item_list=add_list)
while props(context).active_temp_item_list > len(AT.bone_list) - 1:
props(context).active_temp_item_list -= 1
if props(context).active_temp_item_list == 0:
break
set_template_item_list(AT.bone_list)
set_template_category_list(AT.get_category_items())
return {'FINISHED'}
class AT_OT_add_custom_bone_name(Operator):
bl_idname = "at.add_custom_bone_name"
bl_label = "Add Custom Bone Name"
bl_description = "Add custom name to the bone list of the active template category"
name: bpy.props.StringProperty(name="Name")
def execute(self, context):
if not self.name or self.name in AT.template_data:
return {'FINISHED'}
# if not AT.bone_list:
# return {'FINISHED'}
if not AT.get_category_items():
AT.new_category()
AT.append_to_category(item_list=[self.name])
# while props(context).active_temp_item_list > len(AT.bone_list) - 1:
# props(context).active_temp_item_list -= 1
# if props(context).active_temp_item_list == 0:
# break
# set_template_item_list(AT.bone_list)
set_template_category_list(AT.get_category_items())
return {'FINISHED'}
class WM_OT_Copy_to_Clipboard(Operator):
bl_label = 'Copy to Clipboard'
bl_idname = 'wmo.copy_to_clipboard'
bl_description = "Copy text to clipboard buffer"
text: bpy.props.StringProperty()
def execute(self, context):
context.window_manager.clipboard = self.text
self.report({'INFO'}, 'Copy ' + self.text)
return {'FINISHED'}
class AT_OT_remove_selected_from_template_category(Operator):
bl_idname = "at.remove_selected_from_template_category"
bl_label = "Remove"
bl_description = "Remove selected items from the active category"
def execute(self, context):
item_list = context.window_manager.at_template_category_list
new_list = [i.name for i in item_list if not i.flag]
if not item_list:
return {'FINISHED'}
if len(new_list) == len(item_list):
active = item_list[props(context).active_temp_category_list].name
new_list = [i.name for i in item_list if i.name != active]
if AT.get_category_items():
AT.set_category_list(item_list=new_list)
AT.bone_list = evaluate_bone_list(
context, context.active_object, AT.template_data
)
while props(context).active_temp_category_list > len(new_list) - 1:
props(context).active_temp_category_list -= 1
if props(context).active_temp_category_list == 0:
break
set_template_item_list(AT.bone_list)
set_template_category_list(new_list)
return {'FINISHED'}
class AT_OT_remove_all_from_template_category(Operator):
bl_idname = "at.remove_all_from_template_category"
bl_label = "Remove All from Category"
bl_description = "Remove all items from the active category"
def execute(self, context):
AT.template_data[AT.active_category].clear()
AT.bone_list = evaluate_bone_list(
context, context.active_object, AT.template_data
)
set_template_item_list(AT.bone_list)
set_template_category_list([])
return {'FINISHED'}
class AT_OT_move_list_items_up(Operator):
bl_idname = "at.move_list_items_up"
bl_label = "Move Up"
bl_description = (
"Shift the selected items up in the active category list. "
"(Ctrl + Click - Move selected to the top)"
)
def invoke(self, context, event):
self.ctrl_press = False
if event.ctrl:
self.ctrl_press = True
return self.execute(context)
def execute(self, context):
item_list = context.window_manager.at_template_category_list
if not item_list:
return {'FINISHED'}
bones = AT.get_category_items()
move_items = [i.name for i in item_list if i.flag]
if not move_items:
if props(context).active_temp_category_list == 0:
return {'FINISHED'}
active = item_list[props(context).active_temp_category_list].name
if self.ctrl_press:
move_list_items_up_top(bones, [active])
props(context).active_temp_category_list = 0
else:
move_list_item_up(bones, active)
props(context).active_temp_category_list -= 1
elif move_items[0] != bones[0]:
if self.ctrl_press:
move_list_items_up_top(bones, move_items)
else:
for n in move_items:
move_list_item_up(bones, n)
else:
return {'FINISHED'}
set_template_category_list(bones, flag_list=move_items)
return {'FINISHED'}
class AT_OT_move_list_items_down(Operator):
bl_idname = "at.move_list_items_down"
bl_label = "Move Down"
bl_description = (
"Shift the selected items down in the active category list. "
"(Ctrl + Click - Move selected to the bottom)"
)
def invoke(self, context, event):
self.ctrl_press = False
if event.ctrl:
self.ctrl_press = True
return self.execute(context)
def execute(self, context):
item_list = context.window_manager.at_template_category_list
if not item_list:
return {'FINISHED'}
bones = AT.get_category_items()
move_items = [i.name for i in item_list if i.flag]
if not move_items:
if props(context).active_temp_category_list == len(bones) - 1:
return {'FINISHED'}
active = item_list[props(context).active_temp_category_list].name
if self.ctrl_press:
move_list_items_down_bottom(bones, [active])
props(context).active_temp_category_list = len(bones) - 1
else:
move_list_item_down(bones, active)
props(context).active_temp_category_list += 1
elif move_items[-1] != bones[-1]:
if self.ctrl_press:
move_list_items_down_bottom(bones, move_items)
else:
for n in reversed(move_items):
move_list_item_down(bones, n)
else:
return {'FINISHED'}
set_template_category_list(bones, flag_list=move_items)
return {'FINISHED'}
class AT_OT_Select_UList_Items(Operator):
bl_idname = "at.select_list_items"
bl_label = "Select Items"
bl_description = "Select, deselect or invert all list items"
select: bpy.props.BoolProperty(
name="Select",
default=False
)
invert: bpy.props.BoolProperty(
name="Invert",
default=False
)
list_type: bpy.props.EnumProperty(
name="List Type",
items=[
('ITEMS', 'Items', ''),
('CATEGORY', 'Category', '')
],
default='ITEMS'
)
def execute(self, context):
if self.list_type == 'ITEMS':
item_list = context.window_manager.at_template_item_list
elif self.list_type == 'CATEGORY':
item_list = context.window_manager.at_template_category_list
select_ulist_items(item_list, select=self.select, invert=self.invert)
return {'FINISHED'}
class AT_OT_Select_Bone_Layers(Operator):
bl_idname = "at.select_bone_layers"
bl_label = "Select Layers"
bl_description = "Select, deselect or invert layers"
select: bpy.props.BoolProperty(
name="Select",
default=False
)
invert: bpy.props.BoolProperty(
name="Invert",
default=False
)
same: bpy.props.BoolProperty(
name="Same Selected Layers",
default=False
)
def execute(self, context):
if self.same:
armature = context.active_object
layer_bools = [layer for layer in armature.data.layers]
set_select_bone_layers(layer_bools)
elif self.invert:
selected_list = get_selected_bone_layers(context)
invert_bools = [(i not in selected_list) for i in range(32)]
set_select_bone_layers(invert_bools)
else:
set_bone_list_layer(range(32), value=self.select)
return {'FINISHED'}
class AT_OT_rename_template_category(Operator):
bl_idname = "at.rename_template_category"
bl_label = "Rename Category"
bl_description = "Change the name of the active category"
current_name: bpy.props.StringProperty(name="Current Name")
new_name: bpy.props.StringProperty(name="New Name")
def invoke(self, context, event):
self.new_name = self.current_name
return context.window_manager.invoke_props_dialog(self, width=250)
def draw(self, context):
layout = self.layout
row = layout.row(align=True)
row.prop(self, 'new_name')
def execute(self, context):
if self.new_name == '':
self.report({'ERROR'}, "The name can't be empty!")
return {'FINISHED'}
AT.rename_category(self.current_name, self.new_name)
return {'FINISHED'}
class AT_OT_move_template_category_down(Operator):
bl_idname = "at.move_template_category_down"
bl_label = "Move Category Down"
bl_description = "Move the category to the very bottom position"
name: bpy.props.StringProperty(name="Name")
def execute(self, context):
AT.move_category_last(self.name)
return {'FINISHED'}
class TEMPLATE_UL_Armature_bones(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname):
draw_ulist_item(self, layout, item, active_data, active_propname)
class TEMPLATE_UL_Category_bones(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname):
draw_ulist_item(self, layout, item, active_data, active_propname)
class POPUP_OT_Create_Template(Operator):
bl_label = 'Create Template'
bl_idname = 'at.create_template'
bl_description = "Make a new .json file as a template based on the naming convetion of the active armature"
new_category: bpy.props.StringProperty(
name="Bone Category Name",
default="New Category"
)
template_name: bpy.props.StringProperty(
name="Template Name",
default="new template"
)
custom_name: bpy.props.StringProperty(
name="Custom Bone Name",
default="Custom Bone Name"
)
edit: bpy.props.BoolProperty(
default=False
)
def invoke(self, context, event):
obj = context.active_object
bones = get_bones_in_selected_layers(context, obj) # obj.data.bones
AT.initiate_properties([b.name for b in bones])
props(context).active_temp_item_list = 0
props(context).active_temp_category_list = 0
if self.edit:
AT.template_data = get_json_data(get_template_path())
AT.active_category = [c for c in AT.template_data][0]
AT.bone_list = evaluate_bone_list(
context, context.active_object, AT.template_data
)
if is_native_template_path(props(context).templates):
self.template_name = props(context).templates + '_edited'
else:
self.template_name = props(context).templates
set_template_item_list(AT.bone_list)
set_template_category_list(AT.get_category_items())
return context.window_manager.invoke_props_dialog(self, width=450)
def draw(self, context):
wm = context.window_manager
layout = self.layout
row = layout.row(align=True)
col = row.column()
row = col.row(align=True)
col1 = row.column()
col1.ui_units_x = 50
row1 = col1.row(align=True)
row1.operator("at.add_selected_to_template", icon='FORWARD')
op = row1.operator(
"at.select_list_items", text='', icon='CHECKMARK'
)
op.select = True
op.list_type = 'ITEMS'
op.invert = False
op = row1.operator(
"at.select_list_items", text='', icon='CHECKBOX_DEHLT'
)
op.select = False
op.list_type = 'ITEMS'
op.invert = False
op = row1.operator(
"at.select_list_items", text='', icon='SELECT_SUBTRACT'
)
op.select = False
op.list_type = 'ITEMS'
op.invert = True
row1.separator()
row1 = col1.row(align=True)
row1.template_list(
"TEMPLATE_UL_Armature_bones", "",
wm, "at_template_item_list", props(), "active_temp_item_list",
rows=19
)
row1.separator()
row1 = col1.row(align=True)
row1.prop(self, 'custom_name', text='')
row1.operator(
"at.add_custom_bone_name", text='', icon='FORWARD'
).name = self.custom_name
row1.separator()
# Second Column
col2 = row.column()
col2.ui_units_x = 50
row2 = col2.row(align=True)
if AT.template_data.get(AT.active_category):
row2.operator("at.remove_all_from_template_category", text='', icon='X')
row2.operator("at.remove_selected_from_template_category", icon='BACK')
op = row2.operator(
"at.select_list_items", text='', icon='CHECKMARK'
)
op.select = True
op.list_type = 'CATEGORY'
op.invert = False
op = row2.operator(
"at.select_list_items", text='', icon='CHECKBOX_DEHLT'
)
op.select = False
op.list_type = 'CATEGORY'
op.invert = False
op = row2.operator(
"at.select_list_items", text='', icon='SELECT_SUBTRACT'
)
op.select = False
op.list_type = 'CATEGORY'
op.invert = True
row2.operator("at.move_list_items_up", text='', icon='TRIA_UP')
row2.operator("at.move_list_items_down", text='', icon='TRIA_DOWN')
for category in AT.template_data:
row2 = col2.row(align=True)
is_active = True if AT.active_category == category else False
icon = 'DISCLOSURE_TRI_DOWN' if is_active else 'DISCLOSURE_TRI_RIGHT'
row2.operator(
"at.template_categories", text=category, icon=icon,
depress=is_active
).name = category
row2.operator(
"at.rename_template_category", text='', icon='OUTLINER_DATA_GP_LAYER',
depress=is_active
).current_name = category
row2.operator(
"at.move_template_category_down", text='', icon='TRIA_DOWN_BAR',
depress=is_active
).name = category
subrow = row2.row(align=False)
subrow.emboss = 'PULLDOWN_MENU'
subrow.operator(
"at.remove_template_category", text='', icon='REMOVE'
).name = category
if is_active:
row2 = col2.row(align=False)
row2.template_list(
"TEMPLATE_UL_Category_bones", "",
wm, "at_template_category_list", props(), "active_temp_category_list",
rows=17
)
row2 = col2.row(align=False)
row2.prop(self, 'new_category', text='')
subrow = row2.row(align=False)
subrow.emboss = 'PULLDOWN_MENU'
op = subrow.operator("at.create_template_category", text='', icon='ADD')
op.name = self.new_category
row = col.row(align=True)
row.separator()
row = col.row(align=True)
row.prop(self, 'template_name')
def execute(self, context):
if self.template_name == '':
self.report({'ERROR'}, "The name can't be empty!")
return {'FINISHED'}
template_name = check_extension(self.template_name)
base_name = base_file_name(self.template_name)
if self.edit:
location, template_path = get_template_path(return_type=True)
if base_name == props(context).templates:
save_json_data(template_path, AT.template_data)
save_custom_file(base_name, AT.template_data, location=location)
props(context)['bone_category'] = 0
initiate_search_props()
redraw_area('PROPERTIES')
return {'FINISHED'}
config_templates_path = os.path.join(get_config_path(), "templates")
config_file_path = os.path.join(config_templates_path, template_name)
save_json_data(config_file_path, AT.template_data)
# save blank custom mapping
save_custom_file(base_name, AT.template_data)
return {'FINISHED'}
class AT_OT_remove_template(Operator):
bl_idname = "at.remove_template"
bl_label = "Remove Template"
bl_description = "Delete the current template file, can't be undone"
remove_mapping_files: bpy.props.BoolProperty(
name='Yes',
description='Aggre to delete all mapping files that associated with the current template',
default=False
)
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self, width=350)
def draw(self, context):
layout = self.layout
row = layout.row()
row.label(text='Delete all mapping files that associated with the current template?')
row = layout.row()
row.alignment = 'CENTER'
row.prop(self, 'remove_mapping_files')
def execute(self, context):
location, template_file_path = get_template_path(return_type=True)
template = props(context).templates
if os.path.isfile(template_file_path):
os.remove(template_file_path)
props(context)['templates'] = 0
else:
msg = "Armature Templates: The template file '" + template + "' does not exist"
self.report({'ERROR'}, msg)
return {'FINISHED'}
initiate_search_props()
if not self.remove_mapping_files:
redraw_area('PROPERTIES')
return {'FINISHED'}
mapping_native_path = get_native_mapping_path(template)
mapping_config_path = get_config_mapping_path(template)
import shutil
if os.path.isdir(mapping_config_path):
shutil.rmtree(mapping_config_path)
props(context)['bone_mapping'] = 0
if os.path.isfile(mapping_native_path) and location == 'NATIVE':
shutil.rmtree(mapping_native_path)
props(context)['bone_mapping'] = 0
redraw_area('PROPERTIES')
return {'FINISHED'}
class AT_OT_rename_template(Operator):
bl_idname = "at.rename_template"
bl_label = "Rename Template"
bl_description = "Change the name of the current template file"
new_name: bpy.props.StringProperty(
name="New Name"
)
def invoke(self, context, event):
self.new_name = props(context).templates
return context.window_manager.invoke_props_dialog(self, width=250)
def draw(self, context):
layout = self.layout
row = layout.row()
row.prop(self, 'new_name')
def execute(self, context):
location, template_file_path = get_template_path(return_type=True)
template = props(context).templates
mapping_path = get_mapping_path(template, location=location)
if os.path.isfile(template_file_path):
new_name = check_extension(self.new_name)
new_file = os.path.join(os.path.dirname(template_file_path), new_name)
os.rename(template_file_path, new_file)
else:
msg = "Armature Templates: The template file '" + template + "' does not exist"
self.report({'ERROR'}, msg)
return {'FINISHED'}
initiate_search_props()
if not os.path.isdir(mapping_path):
redraw_area('PROPERTIES')
return {'FINISHED'}
parent_folder = os.path.abspath(os.path.join(mapping_path, '..'))
new_template_folder = base_file_name(self.new_name)
new_path = os.path.join(parent_folder, new_template_folder)
os.rename(mapping_path, new_path)
redraw_area('PROPERTIES')
return {'FINISHED'}
class AT_OT_rename_mapping(Operator):
bl_idname = "at.rename_mapping"
bl_label = "Rename Mapping"
bl_description = "Change the name of the current mapping file"
new_name: bpy.props.StringProperty(
name="New Name"
)
def invoke(self, context, event):
self.new_name = props(context).bone_mapping
return context.window_manager.invoke_props_dialog(self, width=250)
def draw(self, context):
layout = self.layout
row = layout.row()
row.prop(self, 'new_name')
def execute(self, context):
location, template_file_path = get_template_path(return_type=True)
mapping = check_extension(props(context).bone_mapping)
mapping_path = get_mapping_path(props(context).templates, location=location)
mapping_file_path = os.path.join(mapping_path, mapping)
if not os.path.isfile(mapping_file_path):
mapping_file_path = os.path.join(
get_config_mapping_path(props(context).templates), mapping)
if os.path.isfile(mapping_file_path):
new_name = check_extension(self.new_name)
new_file = os.path.join(mapping_path, new_name)
os.rename(mapping_file_path, new_file)
else:
msg = "Armature Templates: The template file '" + props(context).bone_mapping + "' does not exist"
self.report({'ERROR'}, msg)
return {'FINISHED'}
redraw_area('PROPERTIES')
return {'FINISHED'}
class AT_OT_guess_mapping_bones(Operator):
bl_idname = "at.guess_mapping_bones"
bl_label = "Guess Mapping"
bl_description = "Search for bone names of the active armature and try to match them in the naming convention of the current template"