-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
1910 lines (1553 loc) · 68 KB
/
__init__.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
# 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 3 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
# MERCHANTIBILITY 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, see <http://www.gnu.org/licenses/>.
import bpy
import time
import random
from .ReampArmature import *
import math
from bpy_extras.io_utils import ExportHelper
from . import decoder
from . import encoder
from . import tz
from . import config
from . import ik_help
from . import armature_set
bl_info = {
"name" : "AniTool",
"author" : "M",
"description" : "",
"blender" : (4, 0, 0),
"version" : (0, 0, 1),
"location" : "",
"warning" : "",
"category" : "Animation"
}
my_source_chains = []
my_target_chains = []
toggle_update = True
Toggle_IK_State = True
toggle_set_chain_map_name = True
save_new_bind_rule = False
save_new_ignore_name = False
boneIgnoreName = 'lowerarm_r_IK,calf_r_IK,lowerarm_l_IK,calf_l_IK,thigh_twist_01_l,calf_twist_01_r,calf_twist_01_l,thigh_twist_01_r,upperarm_twist_01_l,upperarm_twist_01_r,lowerarm_twist_01_l,lowerarm_twist_01_r,ik_hand_root,ik_hand_r,ik_foot_root,ik_foot_r,ik_foot_l'
# ik_bones = [
# {'name':'lowerarm_r','type':'IK','left_right':'left','role':'','child_bone':'hand_r','pole_bone':'lowerarm_r','pole':180,'pole_loc':Vector((0,20,0)),'edit_offset':Vector((0,20,0)),'edit_bone_head':'tail','head_tail':1,'chain':2,'use_tail':True},
# {'name':'lowerarm_l','type':'IK','left_right':'left','role':'','child_bone':'hand_l','pole_bone':'lowerarm_l','pole':0,'pole_loc':Vector((0,20,0)),'edit_offset':Vector((0,20,0)),'edit_bone_head':'tail','head_tail':1,'chain':2,'use_tail':True},
# {'name':'calf_r','type':'IK','left_right':'right','role':'calf','child_bone':'foot_r','pole_bone':'calf_r','pole':0,'pole_loc':Vector((0,-20,0)),'edit_offset':Vector((0,20,0)),'edit_bone_head':'tail','head_tail':1,'chain':2,'use_tail':True},
# {'name':'calf_l','type':'IK','left_right':'left','role':'calf','child_bone':'foot_l','pole_bone':'calf_l','pole':180,'pole_loc':Vector((0,-20,0)),'edit_offset':Vector((0,20,0)),'edit_bone_head':'tail','head_tail':1,'chain':2,'use_tail':True},
# {'name':'foot_r','type':'IK','left_right':'right','role':'foot','child_bone':'ball_r','pole_bone':'','pole':180,'pole_loc':Vector((0,0,20)),'edit_offset':Vector((0,0,10)),'edit_bone_head':'tail','head_tail':1,'chain':1,'use_tail':True},
# {'name':'foot_l','type':'IK','left_right':'left','role':'foot','child_bone':'ball_l','pole_bone':'','pole':180,'pole_loc':Vector((0,0,20)),'edit_offset':Vector((0,0,10)),'edit_bone_head':'tail','head_tail':1,'chain':1,'use_tail':True},
# {'name':'ball_r','type':'IK','left_right':'right','role':'toe','child_bone':Vector((0,-20,0)),'pole_bone':'','pole':180,'pole_loc':Vector((0,0,20)),'edit_offset':Vector((0,0,10)),'edit_bone_head':'tail','head_tail':1,'chain':1,'use_tail':True},
# {'name':'ball_l','type':'IK','left_right':'left','role':'toe','child_bone':Vector((0,-20,0)),'pole_bone':'','pole':180,'pole_loc':Vector((0,0,20)),'edit_offset':Vector((0,0,10)),'edit_bone_head':'tail','head_tail':1,'chain':1,'use_tail':True},
# {'name':'pelvis','type':'Transform','left_right':'left','role':'','child_bone':'spine_01','pole_bone':'','pole':180,'pole_loc':Vector((0,-20,0)),'edit_offset':Vector((0,20,0)),'edit_bone_head':'head','head_tail':0,'chain':2,'use_tail':True}
# ]
ik_bones = config.ik_bones
default_rule_source_name_LR = ['_l','_r']
default_rule_target_name_LR = ['Left','Right']
default_rule_source_name_body = ['index','middle','pinky','ring','thumb','thigh','clavicle','neck']
default_rule_target_name_body = ['Index','Middle','Pinky','Ring','Thumb','UpLeg','Shoulder','Neck']
def set_bone_chain(self,context,BoneChains,scnBoneChains):
scnBoneChains.clear()
scn = bpy.context.scene
for bone_chain in BoneChains:
# print(bone_chain)
item = scnBoneChains.add()
boneNames = ''
for bone in bone_chain:
item_bone_chain = item.bone_chain.add()
item_bone_chain.name = bone.name
boneNames += item_bone_chain.name
item.name = boneNames
def drawBoneChains(self,context,BoneChains):
# scn = bpy.context.scene
layout = self.layout
for idx,item in enumerate(BoneChains):
row = layout.row(align=False)
row.alignment = 'LEFT'
row.prop(item,'isExtraBone')
row.label(text=item.name)
row.split(align=True,factor=0.3)
def sortBoneChains(self,context,scnList):
newSortList = []
for bone_chain in scnList.values():
boneList = []
for bone in bone_chain.bone_chain.values():
boneList.append(bone.name)
newSortList.append(boneList)
newSortList.sort()
scnList.clear()
for bone_chain in newSortList:
item = scnList.add()
chainName = ''
for bone in bone_chain:
item2 = item.bone_chain.add()
item2.name = bone
chainName += bone
item.name = chainName
def getItem(self,context):
items = []
for item in bpy.context.scene.my_target_chains.values():
Bones = ''
Bones += item.IsSelected
for bone in item.bone_chain.values():
Bones += bone.name
items.append((Bones,Bones,''))
return items
def get_enum(self):
import random
# return value
# print(self['testprop'])
return random.randint(1, 1)
def set_enum(self, value):
print (dir(self))
print(self.BoneChain)
print(self.name)
# print(self.value)
# print(self.BoneChain.values()[0])
self.value = value
# self.BoneChain = bpy.context.scene.my_enum_bone_chain.values()[value]
# self.BoneChain = 'mixamorig:RightUpLegmixamorig:RightLegmixamorig:RightFootmixamorig:RightToeBasemixamorig:RightToe_End'
# self['testprop'] = value
print("setting value", value)
def vec_roll_to_mat3(vec, roll):
epsi = 1e-10
target = Vector((0, 0.1, 0))
nor = vec.normalized()
axis = target.cross(nor)
if axis.dot(axis) > epsi:
axis.normalize()
theta = target.angle(nor)
bMatrix = Matrix.Rotation(theta, 3, axis)
else:
updown = 1 if target.dot(nor) > 0 else -1
bMatrix = Matrix.Scale(updown, 3)
bMatrix[2][2] = 1.0
rMatrix = Matrix.Rotation(roll, 3, nor)
mat = rMatrix @ bMatrix
return mat
def mat3_to_vec_roll(mat, ret_vec=False):
vec = mat.col[1]
vecmat = vec_roll_to_mat3(mat.col[1], 0)
vecmatinv = vecmat.inverted()
rollmat = vecmatinv @ mat
roll = math.atan2(rollmat[0][2], rollmat[2][2])
if ret_vec:
return vec, roll
else:
return roll
def create_edit_bone(bone_name,arm=None,coll_name = None,color=None):
eb = bpy.context.active_object.data.edit_bones.get(bone_name)
if not eb:
eb = bpy.context.active_object.data.edit_bones.new(bone_name)
if coll_name:
set_bone_layer(arm,eb,coll_name)
if color:
eb.color.palette = color
return eb
def make_foot_IK(ik_bones,left_right):
for bone in ik_bones:
# 先处理左边再处理右边
if bone['left_right'] == left_right:
if bone['role'] == 'calf':
ik_bone_calf = create_edit_bone(bone['name']+'_IK')
if bone['role'] == 'foot':
bone_foot = create_edit_bone(bone['name'])
ik_bone_foot = create_edit_bone(bone['name']+'_IK')
ik_bone_foot_rotate = create_edit_bone(bone['name']+'_IK_rotate',1)
ik_bone_foot_bottom = create_edit_bone(bone['name']+'_IK_bottom',1)
if bone['role'] == 'toe':
ik_bone_toe = create_edit_bone(bone['name']+'_IK')
ik_bone_calf.parent = ik_bone_foot_rotate
ik_bone_foot_rotate.head = ik_bone_foot_rotate.tail = ik_bone_foot.head[:]
ik_bone_foot_rotate.tail += Vector((0,20,0))
ik_bone_foot_bottom.head = ik_bone_foot_bottom.tail = ik_bone_foot.head[:]
ik_bone_foot_bottom.head += Vector((0,0,-2))
ik_bone_foot_bottom.tail = ik_bone_foot_bottom.head + Vector((0,20,0))
ik_bone_foot_rotate.parent = ik_bone_foot.parent = ik_bone_toe.parent = ik_bone_foot_bottom
def create_constraint(rig,bone_name,cst_name,type):
for cnsts in rig.pose.bones[bone_name].constraints:
if cnsts.name == cst_name:
return cnsts
cst = rig.pose.bones[bone_name].constraints.new(type)
cst.name = cst_name
return cst
def copy_bone_transforms(bone1, bone2):
bone2.head = bone1.head.copy()
bone2.tail = bone1.tail.copy()
bone2.roll = bone1.roll
def set_active_object(object_name):
bpy.context.view_layer.objects.active = object_name
object_name.hide_set(False)
object_name.select_set(state=True)
def set_bone_layer(arm,editbone, coll_name, multi=False):
try:
arm.data.collections[coll_name].assign(editbone)
except:
arm.data.collections.new(coll_name)
arm.data.collections[coll_name].assign(editbone)
# editbone.layers[layer_idx] = True
# if multi:
# return
# for i, lay in enumerate(editbone.layers):
# if i != layer_idx:
# editbone.layers[i] = False
def set_armature_layer(object,collection_name=None,Viewtype=None):
if type(Viewtype) == bool:
for bcoll in object.data.collections:
bcoll.is_visible = Viewtype
if collection_name :
for bcoll in object.data.collections:
if bcoll.name == collection_name:
bcoll.is_visible = True
else:
bcoll.is_visible =False
# while i <= 31:
# if i == collection_name:
# bpy.context.object.data.layers[i] = True
# else:
# bpy.context.object.data.layers[i] = False
# i+= 1
def build_bones_map():
scn = bpy.context.scene
scn.my_bones_map.clear()
for chain_idx,chain_item in enumerate(scn.my_chain_map):
if chain_item.name:
# try:
# print(chain_item.source_chain)
for source_bone,target_bone in zip(chain_item.source_chain.split(','),chain_item.name.split(',')):
item = scn.my_bones_map.add()
item.source_bone = source_bone
item.name = target_bone
# for bone_idx,bone_item in enumerate(scn.my_target_chains[scn.my_target_chains.find(chain_item.name)].bone_chain):
# item = scn.my_bones_map.add()
# item.source_bone = scn.my_source_chains[scn.my_source_chains.find(chain_item.source_chain)].bone_chain[bone_idx].name
# item.name = bone_item.name
if chain_item.is_root:
item.is_root = True
# except Exception:
# print("error bone",bone_item.name)
def build_bone_tweak(self,context,scn_source_chains,scn_source_rig):
scn = bpy.context.scene
scn_target_rig = scn.my_target_rig
set_active_object(scn_target_rig)
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
scn_source_rig.select_set(state=True)
# set_active_object(scn.my_source_rig)
scn_target_rig.select_set(state=True)
bpy.ops.object.mode_set(mode='EDIT')
# create a transform dict of target bones
obj_mat = scn_target_rig.matrix_world
tar_bones_dict = {}
for edit_bone in scn_target_rig.data.edit_bones:
tar_bones_dict[edit_bone.name] = {
'matrix': obj_mat @ edit_bone.matrix,
'head': obj_mat @ edit_bone.head,
'tail': obj_mat @ edit_bone.tail,
'roll': mat3_to_vec_roll(obj_mat.to_3x3() @ edit_bone.matrix.to_3x3()),
'x_axis': (obj_mat @ edit_bone.x_axis.normalized()).normalized()
}
print(" Creating Bones...")
bone_names = []
for chains in scn_source_chains:
for bone in chains.bone_chain:
bone_names.append(bone.name)
# print(bone.name)
#autorot_constraints = True
ik_chains = {}
loc_helper_bones = []
# create the _tweak skeleton for Interactive Tweaks (Bind)
tweak_suffix = '_REMAPTWEAK'
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
set_active_object(scn_source_rig)
bpy.context.view_layer.objects.active = scn_source_rig
scn_source_rig.select_set(state=True)
bpy.ops.object.mode_set(mode='EDIT')
# bpy.ops.object.select_all(action='DESELECT')
# scn_source_rig.select_set(state=True)
# return
bone_names = [b.name for b in scn_source_rig.data.edit_bones]
temp_bone_names = bone_names[:]
for bone in temp_bone_names:
if ArmatureBoneInfo.check_helper_bones(bone=bone):
bone_names.remove(bone)
for bname in bone_names:
if not bname.endswith(tweak_suffix):
eb = bpy.context.object.data.edit_bones.get(bname)
if not bpy.context.object.data.edit_bones.get(eb.name+tweak_suffix):
tweak_b = bpy.context.active_object.data.edit_bones.new(eb.name+tweak_suffix)
tweak_b.use_deform = False
# tweak_b['arp_remap_temp_bone'] = 1# tag it for deletion
tweak_b.head = eb.head.copy()
tweak_b.tail = eb.tail.copy()
tweak_b.roll = eb.roll
set_bone_layer(tweak_b, 25)
# set as root
# copy_bone_transforms(eb, tweak_b)
# set_bone_layer(tweak_b, 25)
# parent
for eb in scn_source_rig.data.edit_bones:
# if eb.layers[25]:
if eb.name.endswith(tweak_suffix):
source_par = bpy.context.object.data.edit_bones.get(eb.name[:-len(tweak_suffix)])
par = source_par.parent
if par:
eb.parent = bpy.context.object.data.edit_bones.get(par.name+tweak_suffix)
# constrain
bpy.ops.object.mode_set(mode='POSE')
for pb in scn_source_rig.pose.bones:
# if pb.bone.layers[25]:
if pb.name.endswith(tweak_suffix):
if not len(pb.constraints):
cns = pb.constraints.new('COPY_TRANSFORMS')
cns.target = scn_source_rig
cns.subtarget = pb.name[:-len(tweak_suffix)]
cns.owner_space = cns.target_space = 'LOCAL'
cns.mix_mode = 'BEFORE'
bpy.ops.object.mode_set(mode='EDIT')
idxi = 0
obj_mat = scn_source_rig.matrix_world.inverted()
for bone_item in scn.my_bones_map:
idxi += 1
source_bone_name = bone_item.source_bone
eb_source_bone = bpy.context.object.data.edit_bones.get(source_bone_name)
if bone_item.name != "" and bone_item.name != "None" and eb_source_bone and bone_item.name in tar_bones_dict:
# if bone_item.name != "" and bone_item.name != "None" and eb_source_bone and bone_item.name in tar_bones_dict:
# main
if bpy.context.active_object.data.edit_bones.get(bone_item.name+"_REMAP"):
bone_remap = bpy.context.active_object.data.edit_bones.get(bone_item.name+"_REMAP")
else:
bone_remap = bpy.context.active_object.data.edit_bones.new(bone_item.name+"_REMAP")
# bone_remap['arp_remap_temp_bone'] = 1# tag it for deletion
# copy target bones transforms
bone_remap.matrix = obj_mat @ tar_bones_dict[bone_item.name]['matrix']
bone_remap.head, bone_remap.tail = obj_mat @ tar_bones_dict[bone_item.name]['head'], obj_mat @ tar_bones_dict[bone_item.name]['tail']
# align_bone_x_axis(bone_remap, obj_mat @ tar_bones_dict[bone_item.name]['x_axis'])
bone_remap.parent = bpy.context.object.data.edit_bones.get(eb_source_bone.name+tweak_suffix)
set_bone_layer(bone_remap, 24)
if bone_item.is_root:
# root offset
root_offset_name = bone_item.name+"_ROOT_OFFSET"
root_offset = create_edit_bone(root_offset_name)
# root_offset = bpy.context.active_object.data.edit_bones.new(root_offset_name)
# root_offset['arp_remap_temp_bone'] = 1# tag it for deletion
copy_bone_transforms(eb_source_bone, root_offset)
set_bone_layer(root_offset, 24)
# root pos
root_pos_name = bone_item.name+"_ROOT"
root_pos = create_edit_bone(root_pos_name)
# root_pos['arp_remap_temp_bone'] = 1# tag it for deletion
root_pos.matrix = obj_mat @ tar_bones_dict[bone_item.name]['matrix']
root_pos.head, root_pos.tail = obj_mat @ tar_bones_dict[bone_item.name]['head'], obj_mat@ tar_bones_dict[bone_item.name]['tail']
root_pos.length = eb_source_bone.length
#align_bone_x_axis(root_pos, tar_bones_dict[bone_item.name]['x_axis'])
root_pos.parent = root_offset
set_bone_layer(root_pos, 24)
bpy.ops.object.mode_set(mode='POSE')
# add location constraint
bone_root_offset_pb = scn_source_rig.pose.bones.get(root_offset_name)
cns = bone_root_offset_pb.constraints.new('COPY_LOCATION')
cns.target = scn_source_rig
cns.subtarget = source_bone_name
cns.name += '_REMAP'
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = scn_target_rig
scn_target_rig.select_set(state=True)
# bpy.data.objects['Armature'].select_set(state=True)
bpy.ops.object.mode_set(mode='POSE')
if scn_target_rig.animation_data:
scn_target_rig.animation_data.action = None
for bone_item in scn.my_bones_map:
if bone_item.name != "" and bone_item.name != "None" and context.active_object.pose.bones.get(bone_item.name):
pose_bone = context.active_object.pose.bones[bone_item.name]
context.active_object.data.bones.active = pose_bone.bone
# Add constraints
# main rotation
if 'Copy Rotation_REMAP' in pose_bone.constraints:
cns = pose_bone.constraints['Copy Rotation_REMAP']
else:
cns = pose_bone.constraints.new('COPY_ROTATION')
cns.name += '_REMAP'
cns.target = scn_source_rig
cns.subtarget = bone_item.name + "_REMAP"
if bone_item.is_root:
if 'Copy Location_loc_REMAP' in pose_bone.constraints:
cns_root = pose_bone.constraints['Copy Location_loc_REMAP']
else:
cns_root = pose_bone.constraints.new('COPY_LOCATION')
cns_root.name += '_loc_REMAP'
cns_root.target = scn_source_rig
cns_root.subtarget = bone_item.name + "_ROOT"
cns_root.owner_space = cns_root.target_space = 'WORLD'
def set_string(self,value):
# print(value)
# print(dir(self))
self.my_test_string = value
print(self.my_test_string)
self['abc'] = value
pass
def get_string(self):
# print(value)
# print(dir(self))
# print(self.my_test_string)
return self['abc']
return self.my_test_string
def update_string(self,context):
if toggle_update:
# bpy.ops.an.build_chains()
filter_name()
def set_ignore_source(self,value):
global boneIgnoreName
self.enteranl_add_to_ignore_source = value
if value:
ignore_name = self.source_chain.split(',')[0]
boneIgnoreName += ignore_name
boneIgnoreName += ','
bpy.ops.an.build_list()
def get_ignore_source(self):
return self.enteranl_add_to_ignore_source
def set_ignore_target(self,value):
global boneIgnoreName
self.enteranl_add_to_ignore_target = value
if value:
ignore_name = self.name.split(',')[0]
boneIgnoreName += ignore_name
boneIgnoreName += ','
bpy.ops.an.build_list()
def get_ignore_target(self):
return self.enteranl_add_to_ignore_target
def set_ignore_bool(self,value):
scn = bpy.context.scene
self.in_is_ignore = value
list = scn.my_ignore_bone_name.split(',')
if value:
if self.name not in list:
list.append(self.name)
else:
if self.name in list:
list.remove(self.name)
scn.my_ignore_bone_name = ','.join([a for a in list if a != ''])
def get_ignore_bool(self):
return self.in_is_ignore
def recoerd_old_value():
scn = bpy.context.scene
for item in scn.my_chain_map:
item.old_name = item.name
def get_chain_map_name(self):
return self.in_name
def set_chain_map_name(self,value):
scn = bpy.context.scene
global toggle_set_chain_map_name
if toggle_set_chain_map_name == True:
toggle_set_chain_map_name = False
for item in scn.my_chain_map:
if item.name == value:
item.name = self.name
toggle_set_chain_map_name = True
self.in_name = value
self.target_bones.clear()
for bone in value.split(','):
item = self.target_bones.add()
item.name = bone
def set_source_chain(self,value):
self.in_source_chain = value
self.source_bones.clear()
for bone in value.split(','):
item = self.source_bones.add()
item.name = bone
def get_source_chain(self):
return self.in_source_chain
def filter_name():
scn = bpy.context.scene
chain_map = scn.my_chain_map
name_need_change = []
name_all = []
global my_target_chains
for item in my_target_chains:
name_all.append(','.join(a.name for a in item['chain']))
for item in chain_map:
if item.old_name != item.name:
name_need_change.append(item.name)
name_all = list(set(name_all)-set(name_need_change))
idx_need_change = []
for idx,item in enumerate(chain_map):
if item.name in name_need_change and item.name == item.old_name:
idx_need_change.append(idx)
elif item.name in name_all :
name_all.remove(item.name)
for idx in idx_need_change:
chain_map[idx].name = name_all[0]
name_all.pop(0)
recoerd_old_value()
def build_default_bind_rule():
scn = bpy.context.scene
scn.my_chain_bind_rule_LR.clear()
scn.my_chain_bind_rule_body.clear()
global default_rule_source_name_LR
global default_rule_target_name_LR
global default_rule_source_name_body
global default_rule_target_name_body
for source_name , name in zip(default_rule_source_name_LR,default_rule_target_name_LR):
item = scn.my_chain_bind_rule_LR.add()
item.source_name = source_name
item.name = name
for source_name , name in zip(default_rule_source_name_body,default_rule_target_name_body):
item = scn.my_chain_bind_rule_body.add()
item.source_name = source_name
item.name = name
def selects_mode(objs,mode):
scn = bpy.context.scene
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
for obj in objs:
obj.hide_set(False)
bpy.context.view_layer.objects.active = obj
obj.select_set(state=True)
bpy.ops.object.mode_set(mode=mode)
def select_mode(obj,mode):
scn = bpy.context.scene
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
obj.hide_set(False)
bpy.context.view_layer.objects.active = obj
obj.select_set(state=True)
bpy.ops.object.mode_set(mode=mode)
def copy_transforms(bone_map):
scn = bpy.context.scene
select_mode(scn.my_source_rig,"POSE")
for item in bone_map:
pb1 = scn.my_source_rig.pose.bones[item.source_bone]
pb2 = scn.my_target_rig.pose.bones[item.name]
cnst = pb2.constraints.new('COPY_TRANSFORMS')
cnst.target = scn.my_source_rig
cnst.subtarget = pb1.name
def copy_transforms_all(source,target):
select_mode(target,"POSE")
for pb1 in source.pose.bones:
pb2 = target.pose.bones[pb1.name]
cnst = pb2.constraints.new('COPY_TRANSFORMS')
cnst.target = source
cnst.subtarget = pb1.name
def connect_bones():
scn = bpy.context.scene
select_mode(scn.my_source_rig,'EDIT')
for chain in my_source_chains:
bpy.context.view_layer.update()
bone_list = chain['chain']
for idx,bone in enumerate(bone_list):
eb = create_edit_bone(bone.name)
if idx < len(bone_list) - 1:
eb2 = create_edit_bone(bone_list[idx+1].name)
offset = eb2.head.copy()
eb.tail = offset
def rotate_edit_bones():
scn = bpy.context.scene
_spine = ['pelvis', 'spine', 'neck', 'head']
_arms = ['clavicle', 'upperarm', 'lowerarm']
_hand = ['hand']
_fingers = ['thumb', 'index', 'middle', 'ring', 'pinky']
_legs = ['thigh', 'calf']
_foot = ['foot']
_toes = ['ball']
# _spine = ['pelvis']
# _arms = []
# _hand = []
# _fingers = []
# _legs = []
# _foot = []
# _toes = []
bones_transforms_dict = {}
print("\nSet Mannequin Axes bones...")
#display layers
print(" Build transform dict...")
#build a dict of bones transforms, excluding custom bones, bend bones, helper bones
rotate_value = 0.0
rotate_axis = 'X'
roll_value = 0.0
created_bones_dict = {}
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
scn.my_source_rig.select_set(state=True)
bpy.ops.object.mode_set(mode='EDIT')
for chain in my_source_chains:
for pb in chain['chain']:
bone_name = pb.name
bone_to_create_name = ""
rotate_value = 0.0
roll_value = 0.0
rotate_axis = Vector((1,0,0))
if pb.name == 'thigh_twist_01_l':
a = 1
pass
#spine
for b in _spine:
if b in bone_name:
rotate_value = -math.pi/2
roll_value = math.radians(-90)
bone_to_create_name = bone_name
rotate_axis = 'Z'
break
#arms
if bone_to_create_name == "":
for b in _arms:
if b in bone_name:
if bone_name.endswith(".l") or bone_name.endswith("_l"):
rotate_value = -math.pi/2
roll_value = 0.0
if scn.arp_retro_axes:
rotate_value = -math.pi/2
roll_value = math.radians(180)
if bone_name.endswith(".r") or bone_name.endswith("_r"):
rotate_value = math.pi/2
roll_value = math.radians(180)
if scn.arp_retro_axes:
rotate_value = -math.pi/2
roll_value = math.radians(0)
bone_to_create_name = bone_name
rotate_axis = 'Z'
break
#hand
if bone_to_create_name == "":
for b in _hand:
if b in bone_name:
if bone_name.endswith(".l") or bone_name.endswith("_l"):
rotate_value = -math.pi/2
roll_value = math.radians(-90)
if bone_name.endswith(".r") or bone_name.endswith("_r"):
rotate_value = math.pi/2
roll_value = math.radians(-90)
bone_to_create_name = bone_name
rotate_axis = 'Z'
break
#fingers
if bone_to_create_name == "":
for b in _fingers:
if b in bone_name:
if bone_name.endswith(".l") or bone_name.endswith("_l"):
rotate_value = -math.pi/2
roll_value = math.radians(-90)
if bone_name.endswith(".r") or bone_name.endswith("_r"):
rotate_value = math.pi/2
roll_value = math.radians(-90)
bone_to_create_name = bone_name
rotate_axis = 'Z'
break
#legs
if bone_to_create_name == "":
for b in _legs:
if b in bone_name:
if bone_name.endswith(".l") or bone_name.endswith("_l"):
rotate_value = math.pi/2
roll_value = math.radians(180)
if bone_name.endswith(".r") or bone_name.endswith("_r"):
rotate_value = -math.pi/2
roll_value = math.radians(0)
bone_to_create_name = bone_name
rotate_axis = 'Z'
break
#foot
if bone_to_create_name == "":
for b in _foot:
if b in bone_name:
if bone_name.endswith(".l") or bone_name.endswith("_l"):
rotate_value = math.pi
roll_value = math.radians(90)
if bone_name.endswith(".r") or bone_name.endswith("_r"):
rotate_value = 0.0
roll_value = math.radians(-90)
bone_to_create_name = bone_name
rotate_axis = 'Z'
break
#toes
if bone_to_create_name == "":
for b in _toes:
if b in bone_name:
if bone_name.endswith(".l") or bone_name.endswith("_l"):
rotate_value = -math.pi/2
roll_value = math.radians(-90)
if bone_name.endswith(".r") or bone_name.endswith("_r"):
rotate_value = math.pi/2
roll_value = math.radians(-90)
bone_to_create_name = bone_name
rotate_axis = 'Z'
break
if bone_to_create_name != "" :
#create _childof bones
# new_bone = create_edit_bone(bone_to_create_name + "_childof", deform=False)
# new_bone.head = bones_transforms_dict[bone_to_create_name][0]
# new_bone.tail = bones_transforms_dict[bone_to_create_name][1]
# new_bone.roll = bones_transforms_dict[bone_to_create_name][2]
# R = Matrix.Rotation(math.radians(-90.0),4,eb.z_axis.normalized())
eb = bpy.context.object.data.edit_bones.get(bone_name)
R = Matrix.Rotation(rotate_value,4,eb.z_axis.normalized() if rotate_axis == 'Z' else eb.x_axis.normalized())
# print(R)
old_head = eb.head.copy()
eb.transform(R,roll=True)
# eb.tail.rotate(R)
offset = -(eb.head - old_head)
eb.head += offset
eb.tail += offset
set_bone_layer(eb, 16)
# get_edit_bone(bone_to_create_name).use_deform = False
# store the new bones in a dict
# created_bones_dict[bone_to_create_name] = rotate_value, roll_value, rotate_axis
# print(math.pi)
# print(math.radians(90))
def duplicate(obj, data=True, actions=True, collection=None):
obj_copy = obj.copy()
if data:
obj_copy.data = obj_copy.data.copy()
if actions and obj_copy.animation_data:
obj_copy.animation_data.action = obj_copy.animation_data.action.copy()
bpy.context.collection.objects.link(obj_copy)
return obj_copy
def bake_root_action(source_rig,target_rig):
bpy.context.view_layer.update()
select_mode(source_rig,'POSE')
bpy.ops.pose.select_all(action='SELECT')
if source_rig.animation_data:
i = 0
while i < 2:
copy_transforms_all(source_rig,target_rig)
bpy.context.view_layer.update()
select_mode(target_rig,'POSE')
bpy.ops.pose.select_all(action='SELECT')
bpy.context.view_layer.update()
bpy.ops.nla.bake(
frame_start=int(target_rig.animation_data.action.frame_range[0]),
frame_end=int(target_rig.animation_data.action.frame_range[1]),
step=1,
only_selected=True,
visual_keying=True,
clear_constraints = True,
bake_types={'POSE'}
)
bpy.context.view_layer.update()
i += 1
def export_map(file_path):
scn = bpy.context.scene
# add extension
if not file_path.endswith(".toml"):
file_path += ".toml"
file = open(file_path, 'w', encoding='utf8', newline='\n')
file.write('')
config = {}
common = {}
chains = {}
config = {'common':common,'chains':chains}
common['ignore_name'] = scn.my_ignore_bone_name
name = locals()
for idx,chain in enumerate(scn.my_chain_map):
idx = str(idx)
name[str(chain.source_chain)] = {}
# name[str(chain.source_chain)]['source_chain'] = chain.source_chain
name[str(chain.source_chain)]['target_chain'] = chain.name
name[str(chain.source_chain)]['is_root'] = chain.is_root
chains[str(chain.source_chain)] = name[str(chain.source_chain)]
r = encoder.dump(config, file)
file.close()
return
def import_map(file_path):
scn = bpy.context.scene
file = open(file_path, 'r', encoding='utf8', newline='\n')
config = decoder.load(file)
scn.my_ignore_bone_name = config['common']['ignore_name']
global save_new_ignore_name
save_new_ignore_name = True
bpy.ops.an.build_list()
for chain in scn.my_chain_map:
for source_chain in config['chains']:
if source_chain == chain.source_chain:
chain.name = config['chains'][source_chain]['target_chain']
chain.is_root = config['chains'][source_chain]['is_root']
del config['chains'][source_chain]
break
file.close()
return
def root_motion():
scn = bpy.context.scene
source_action = scn.my_source_rig.animation_data.action
location_index = 0
for fc in source_action.fcurves:
if fc.data_path == 'location':
scn.my_target_rig.keyframe_insert(data_path=fc.data_path, index=fc.array_index, group=scn.my_target_rig.data.name,frame=0)
target_fc = scn.my_target_rig.animation_data.action.fcurves.find(fc.data_path,index=fc.array_index)
for kp in fc.keyframe_points:
scn.my_target_rig.keyframe_insert(data_path=fc.data_path, index=fc.array_index, group=scn.my_target_rig.data.name,frame=kp.co[0])
target_kp = target_fc.keyframe_points[-1]
target_kp.co = kp.co
if location_index >= 2:
break
def auto_add_ignore(source,target):
ignore = []
source_pb_list = [pb.name for pb in source.pose.bones]
for target_pb in target.pose.bones:
if target_pb.name not in source_pb_list:
ignore.append(target_pb.name)
return ignore
def copy_rest_pose(bone_map):
scn = bpy.context.scene
selects_mode([scn.my_source_rig,scn.my_target_rig],"POSE")
for pb in scn.my_source_rig.pose.bones:
pb.matrix_basis = Matrix()
if scn.my_copy_rest_pose_rule == 'U2U':
for item in bone_map:
pb1 = scn.my_source_rig.pose.bones[item.source_bone]
pb2 = scn.my_target_rig.pose.bones[item.name]
mat_loc = Matrix.Translation(pb.head)
pb1.matrix = pb2.matrix
bpy.context.view_layer.update()
# pb1.matrix = mat_loc @ pb1.matrix
elif scn.my_copy_rest_pose_rule == 'U2M':
for item in bone_map:
bpy.context.view_layer.update()
pb1 = scn.my_source_rig.pose.bones[item.source_bone]
pb2 = scn.my_target_rig.pose.bones[item.name]
v1 = pb1.tail - pb1.head
v1.normalize()