-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
load_mocap.py
3464 lines (2807 loc) · 160 KB
/
load_mocap.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
import json
import os
import bpy
from bpy import context
from bpy_extras.io_utils import ImportHelper
from bpy_extras.io_utils import ExportHelper
from bpy.types import Operator
from bpy.props import StringProperty, BoolProperty, EnumProperty
from . helper import helper_functions, skeleton_import, CacheChannel, CacheFile
import math
class OT_TestOpenFilebrowser(Operator, ImportHelper):
bl_idname = "test.open_filebrowser"
bl_label = "Open the file browser (yay)"
def execute(self, context):
filename, extension = os.path.splitext(self.filepath)
print('real path', os.path.dirname(self.filepath))
print('Selected file:', self.filepath)
print('File name:', filename)
print('File extension:', extension)
# print('Some Boolean:', self.some_boolean)
return {'FINISHED'}
class Import_SMPL_easymocap(Operator, ImportHelper):
bl_idname = "mocap.import_smpl_easymocap"
bl_label = "Import data"
bl_description = "Import SMPL EasyMOCAP"
filename_ext = ".json"
filter_glob: StringProperty(
default="*.json",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
def execute(self,context):
import os
import json
import bpy
from bpy import context
import math
multiplier = context.scene.sk_value_prop.sk_value
raw_bool = context.scene.sk_value_prop.sk_raw_bool
debug_bool = context.scene.sk_value_prop.sk_debug_bool
# reload_skeleton_bool = context.scene.sk_value_prop.sk_reload_skeleton_bool
limit_rotation_bool = context.scene.sk_value_prop.sk_constraint_limit_rotation_bool
#========================
#EASYMOCAP
#=====================
import sys
import bpy
from os.path import join
import math
import numpy as np
from mathutils import Matrix, Vector, Quaternion, Euler
import json
import pickle
part_match = {'root': 'root', 'bone_00': 'Pelvis', 'bone_01': 'L_Hip', 'bone_02': 'R_Hip',
'bone_03': 'Spine1', 'bone_04': 'L_Knee', 'bone_05': 'R_Knee', 'bone_06': 'Spine2',
'bone_07': 'L_Ankle', 'bone_08': 'R_Ankle', 'bone_09': 'Spine3', 'bone_10': 'L_Foot',
'bone_11': 'R_Foot', 'bone_12': 'Neck', 'bone_13': 'L_Collar', 'bone_14': 'R_Collar',
'bone_15': 'Head', 'bone_16': 'L_Shoulder', 'bone_17': 'R_Shoulder', 'bone_18': 'L_Elbow',
'bone_19': 'R_Elbow', 'bone_20': 'L_Wrist', 'bone_21': 'R_Wrist', 'bone_22': 'L_Hand', 'bone_23': 'R_Hand'}
# part_match_custom = {'root': 'root', 'bone_00': 'pelvis', 'bone_01': 'left_hip', 'bone_02': 'right_hip',
# 'bone_03': 'spine1', 'bone_04': 'left_knee', 'bone_05': 'right_knee', 'bone_06': 'spine2',
# 'bone_07': 'left_ankle', 'bone_08': 'right_ankle', 'bone_09': 'spine3', 'bone_10': 'left_foot',
# 'bone_11': 'right_foot', 'bone_12': 'neck', 'bone_13': 'left_collar', 'bone_14': 'right_collar',
# 'bone_15': 'head', 'bone_16': 'left_shoulder', 'bone_17': 'right_shoulder', 'bone_18': 'jaw',
# 'bone_19': 'left_eye_smplhf', 'bone_20': 'right_eye_smplhf', 'bone_21': 'left_elbow', 'bone_22': 'right_elbow',
# 'bone_23': 'left_wrist', 'bone_24': 'right_wrist',
# # 'bone_25': 'left_hand','bone_26': 'right_hand'
# 'bone_25': 'left_index1','bone_26': 'left_middle1','bone_27': 'left_pinky1','bone_28': 'left_ring1','bone_29': 'left_thumb1',
# 'bone_30': 'right_index1','bone_31': 'right_middle1','bone_32': 'right_pinky1','bone_33': 'right_ring1','bone_34': 'right_thumb1',
# 'bone_35': 'left_index2','bone_36': 'left_middle2','bone_37': 'left_pinky2','bone_38': 'left_ring2','bone_39': 'left_thumb2',
# 'bone_40': 'right_index2','bone_41': 'right_middle2','bone_42': 'right_pinky2','bone_43': 'right_ring2','bone_44': 'right_thumb2',
# 'bone_45': 'left_index3','bone_46': 'left_middle3','bone_47': 'left_pinky3','bone_48': 'left_ring3','bone_49': 'left_thumb3',
# 'bone_50': 'right_index3','bone_51': 'right_middle3','bone_52': 'right_pinky3','bone_53': 'right_ring3','bone_54': 'right_thumb3'
# }
part_match_custom = {'root': 'root', 'bone_00': 'pelvis', 'bone_01': 'left_hip', 'bone_02': 'right_hip',
'bone_03': 'spine1', 'bone_04': 'left_knee', 'bone_05': 'right_knee', 'bone_06': 'spine2',
'bone_07': 'left_ankle', 'bone_08': 'right_ankle', 'bone_09': 'spine3', 'bone_10': 'left_foot',
'bone_11': 'right_foot', 'bone_12': 'neck', 'bone_13': 'left_collar', 'bone_14': 'right_collar',
'bone_15': 'head', 'bone_16': 'left_shoulder', 'bone_17': 'right_shoulder', 'bone_18': 'left_elbow',
'bone_19': 'right_elbow', 'bone_20': 'left_wrist', 'bone_21': 'right_wrist', 'bone_22': 'jaw',
'bone_23': 'left_eye_smplhf', 'bone_24': 'right_eye_smplhf',
'bone_25': 'left_index1', 'bone_26': 'left_index2', 'bone_27': 'left_index3',
'bone_28': 'left_middle1', 'bone_29': 'left_middle2', 'bone_30': 'left_middle3',
'bone_31': 'left_pinky1', 'bone_32': 'left_pinky2', 'bone_33': 'left_pinky3',
'bone_34': 'left_ring1', 'bone_35': 'left_ring2', 'bone_36': 'left_ring3',
'bone_37': 'left_thumb1', 'bone_38': 'left_thumb2', 'bone_39': 'left_thumb3',
'bone_40': 'right_index1', 'bone_41': 'right_index2', 'bone_42': 'right_index3',
'bone_43': 'right_middle1', 'bone_44': 'right_middle2', 'bone_45': 'right_middle3',
'bone_46': 'right_pinky1', 'bone_47': 'right_pinky2', 'bone_48': 'right_pinky3',
'bone_49': 'right_ring1', 'bone_50': 'right_ring2', 'bone_51': 'right_ring3',
'bone_52': 'right_thumb1', 'bone_53': 'right_thumb2', 'bone_54': 'right_thumb3'
}
part_match_custom_less = {'root': 'root', 'bone_00': 'pelvis', 'bone_01': 'left_hip', 'bone_02': 'right_hip',
'bone_03': 'spine1', 'bone_04': 'left_knee', 'bone_05': 'right_knee', 'bone_06': 'spine2',
'bone_07': 'left_ankle', 'bone_08': 'right_ankle', 'bone_09': 'spine3', 'bone_10': 'left_foot',
'bone_11': 'right_foot', 'bone_12': 'neck', 'bone_13': 'left_collar', 'bone_14': 'right_collar',
'bone_15': 'head', 'bone_16': 'left_shoulder', 'bone_17': 'right_shoulder', 'bone_18': 'left_elbow',
'bone_19': 'right_elbow', 'bone_20': 'left_wrist', 'bone_21': 'right_wrist', 'bone_22': 'jaw',
'bone_23': 'left_eye_smplhf', 'bone_24': 'right_eye_smplhf',
'bone_25': 'left_index1',
'bone_26': 'left_middle1',
'bone_27': 'left_pinky1',
'bone_28': 'left_ring1',
'bone_29': 'left_thumb1',
'bone_30': 'right_index1',
'bone_31': 'right_middle1',
'bone_32': 'right_pinky1',
'bone_33': 'right_ring1',
'bone_34': 'right_thumb1'
}
gender = 'n'
def deg2rad(angle):
return -np.pi * (angle + 90) / 180.
def setState0():
for ob in bpy.data.objects.values():
ob.select = False
bpy.context.scene.objects.active = None
def Rodrigues(rotvec):
theta = np.linalg.norm(rotvec)
r = (rotvec/theta).reshape(3, 1) if theta > 0. else rotvec
cost = np.cos(theta)
mat = np.asarray([[0, -r[2], r[1]],
[r[2], 0, -r[0]],
[-r[1], r[0], 0]])
return(cost*np.eye(3) + (1-cost)*r.dot(r.T) + np.sin(theta)*mat)
def rodrigues2bshapes(pose):
# if pose.shape[0]==24:
# rod_rots = np.asarray(pose).reshape(24, 3)
# else:
# rod_rots = np.asarray(pose).reshape(87, 3)
rod_rots = np.asarray(pose).reshape(int(pose.shape[0]/3), 3)
mat_rots = [Rodrigues(rod_rot) for rod_rot in rod_rots]
bshapes = np.concatenate([(mat_rot - np.eye(3)).ravel()
for mat_rot in mat_rots[1:]])
return(mat_rots, bshapes)
# apply trans pose and shape to character
def apply_trans_pose_shape(trans, pose, shape, ob, arm_ob, obname, scene, cam_ob, frame=None):
# transform pose into rotation matrices (for pose) and pose blendshapes
mrots, bsh = rodrigues2bshapes(pose)
# set the location of the first bone to the translation parameter
if obname[:1] == 'n':
part_bones = part_match_custom
# part_bones = part_match_custom_less
arm_ob.pose.bones['pelvis'].location = trans
arm_ob.pose.bones['root'].location = trans
arm_ob.pose.bones['root'].keyframe_insert('location', frame=frame)
else:
part_bones = part_match
arm_ob.pose.bones[obname+'_Pelvis'].location = trans
arm_ob.pose.bones[obname+'_root'].location = trans
arm_ob.pose.bones[obname +'_root'].keyframe_insert('location', frame=frame)
# set the pose of each bone to the quaternion specified by pose
for ibone, mrot in enumerate(mrots):
# bone = arm_ob.pose.bones[obname+'_'+part_match['bone_%02d' % ibone]]
# bone = arm_ob.pose.bones[obname+'_'+part_bones['bone_%02d' % ibone]]
if obname[:1] == 'n':
bone = arm_ob.pose.bones[part_bones['bone_%02d' % ibone]]
else:
bone = arm_ob.pose.bones[obname+'_'+part_bones['bone_%02d' % ibone]]
bone.rotation_quaternion = Matrix(mrot).to_quaternion()
if frame is not None:
bone.keyframe_insert('rotation_quaternion', frame=frame)
bone.keyframe_insert('location', frame=frame)
# apply pose blendshapes
for ibshape, bshape in enumerate(bsh):
ob.data.shape_keys.key_blocks['Pose%03d' % ibshape].value = bshape
if frame is not None:
ob.data.shape_keys.key_blocks['Pose%03d' % ibshape].keyframe_insert(
'value', index=-1, frame=frame)
# apply shape blendshapes
for ibshape, shape_elem in enumerate(shape):
ob.data.shape_keys.key_blocks['Shape%03d' % ibshape].value = shape_elem
if frame is not None:
ob.data.shape_keys.key_blocks['Shape%03d' % ibshape].keyframe_insert(
'value', index=-1, frame=frame)
import os
def read_json(path):
with open(path) as f:
data = json.load(f)
return data
def read_pkl(path_pkl):
with open(path_pkl, 'rb') as pk:
data_pkl = pickle.load(pk)
return data_pkl
def read_smpl(outname):
assert os.path.exists(outname), outname
filename, file_extension = os.path.splitext(outname)
datas = read_json(outname)
# data_pkl = read_pkl(filename+'.pkl')
# datas[0]['pose_new']=data_pkl[0].tolist()
outputs = []
for data in datas:
for key in ['Rh', 'Th', 'poses', 'shapes']:
# for key in ['Rh', 'Th', 'pose_new', 'shapes']:
data[key] = np.array(data[key])
outputs.append(data)
return outputs
def merge_params(param_list, share_shape=True):
output = {}
for key in ['poses', 'shapes', 'Rh', 'Th', 'expression']:
# for key in ['pose_new', 'shapes', 'Rh', 'Th', 'expression']:
if key in param_list[0].keys():
output[key] = np.vstack([v[key] for v in param_list])
if share_shape:
output['shapes'] = output['shapes'].mean(axis=0, keepdims=True)
return output
def load_motions(path):
from glob import glob
filenames = sorted(glob(join(path, '*.json')))
filenames_pkl = sorted(glob(join(path, '*.pkl')))
print('file_json:',filenames,' file_pkl: ',filenames_pkl)
motions = {}
# for filename in filenames[300:900]:
for filename in filenames:
infos = read_smpl(filename)
for data in infos:
pid = data['id']
if pid not in motions.keys():
motions[pid] = []
motions[pid].append(data)
keys = list(motions.keys())
# BUG: not strictly equal: (Rh, Th, poses) != (Th, (Rh, poses))
for pid in motions.keys():
motions[pid] = merge_params(motions[pid])
motions[pid]['poses'][:, :3] = motions[pid]['Rh']
# motions[pid]['pose_new'][:, :3] = motions[pid]['Rh']
return motions
def load_smpl_params(datapath):
motions = load_motions(datapath)
return motions
def init_scene(scene, params, gender='male', angle=0):
# load fbx model
# path_fbx=r'D:\MOCAP\EasyMocap_v2\data\smplx\SMPL_maya'
path_fbx = context.scene.sk_value_prop.sk_smpl_path
# bpy.ops.import_scene.fbx(filepath=join(path_fbx, 'basicModel_%s_lbs_10_207_0_v1.0.2.fbx' % 'm'), axis_forward='-Y', axis_up='-Z', global_scale=100)
# bpy.ops.import_scene.fbx(filepath=path_fbx, axis_forward='-Y', axis_up='-Z', global_scale=100, automatic_bone_orientation=True) #se usar orient, o codigo nao funciona direto.
bpy.ops.import_scene.fbx(filepath=path_fbx, axis_forward='-Y', axis_up='-Z', global_scale=100)
print('success load')
if os.path.basename(path_fbx) == 'basicModel_m_lbs_10_207_0_v1.0.2.fbx' :
obj_gender = 'm'
elif os.path.basename(path_fbx) == 'basicModel_f_lbs_10_207_0_v1.0.2.fbx' :
obj_gender = 'f'
else:
obj_gender = 'n'
if obj_gender == 'n':
for mesh in bpy.data.objects.keys():
if mesh == 'SMPLX-mesh-male':
ob = bpy.data.objects[mesh]
arm_obj = 'SMPLX-male'
if mesh == 'SMPLX-mesh-female':
ob = bpy.data.objects[mesh]
arm_obj = 'SMPLX-female'
if mesh == 'SMPLX-mesh-neutral':
ob = bpy.data.objects[mesh]
arm_obj = 'SMPLX-neutral'
# obname = 'SMPLX-mesh-male'
# ob = bpy.data.objects[obname]
# arm_obj = 'SMPLX-male'
obname = 'n'
else:
obname = '%s_avg' % obj_gender
ob = bpy.data.objects[obname]
arm_obj = 'Armature'
ob.data.use_auto_smooth = False # autosmooth creates artifacts
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_all(action='DESELECT')
cam_ob = ''
ob.data.shape_keys.animation_data_clear()
arm_ob = bpy.data.objects[arm_obj]
arm_ob.animation_data_clear()
return(ob, obname, arm_ob, cam_ob)
params = []
#path_smpl = r'D:\MOCAP\EasyMocap_v2\1_output\20210423_3\smpl'
# path_smpl = r'D:\MOCAP\EasyMocap_v2\1_output\20210424_1\smpl'
path_smpl = self.filepath
scene = bpy.data.scenes['Scene']
obj_gender = 'n'
ob, obname, arm_ob, cam_ob= init_scene(scene, params, obj_gender)
#setState0()
#ob.select = True
#bpy.context.scene.objects.active = ob
obj = bpy.context.window.scene.objects[0]
bpy.context.view_layer.objects.active = ob
# unblocking both the pose and the blendshape limits
for k in ob.data.shape_keys.key_blocks.keys():
bpy.data.shape_keys["Key"].key_blocks[k].slider_min = -10
bpy.data.shape_keys["Key"].key_blocks[k].slider_max = 10
#scene.objects.active = arm_ob
obs = []
for ob in bpy.context.scene.objects:
if ob.type == 'ARMATURE':
obs.append(ob)
#obs
armature = obs[len(obs)-1].name
#bpy.data.objects[armature].select_set(True)
obs[len(obs)-1].select_set(True)
view_layer = bpy.context.view_layer
#Armature_obj = bpy.context.scene.objects[armature]
Armature_obj = obs[len(obs)-1]
#view_layer.objects.active = Armature_obj
view_layer.objects.active = arm_ob
motions = load_smpl_params(path_smpl)
for pid, data in motions.items():
# animation
arm_ob.animation_data_clear()
# cam_ob.animation_data_clear()
# load smpl params:
nFrames = data['poses'].shape[0]
# nFrames = data['pose_new'].shape[0]
for frame in range(nFrames):
# print(frame)
scene.frame_set(frame)
# apply
trans = data['Th'][frame]
shape = data['shapes'][0]
# pose_temp = data['poses'][frame]
# pose_temp = data['pose_new'][frame]
pose = data['poses'][frame]
# pose = data['pose_new'][frame]
# l_index1 = np.concatenate([np.array([0]),np.array([0]),pose_temp[67:68]])
# l_middle1 = np.concatenate([np.array([0]),np.array([0]),pose_temp[68:69]])
# l_pinky1 = np.concatenate([np.array([0]),np.array([0]),pose_temp[69:70]])
# l_ring1 = np.concatenate([np.array([0]),np.array([0]),pose_temp[70:71]])
# l_thumb1 = np.concatenate([np.array([0]),pose_temp[71:72],np.array([0])])
# r_index1 = np.concatenate([np.array([0]),np.array([0]),pose_temp[72:73]])
# r_middle1 = np.concatenate([np.array([0]),np.array([0]),pose_temp[73:74]])
# r_pinky1 = np.concatenate([np.array([0]),np.array([0]),pose_temp[74:75]])
# r_ring1 = np.concatenate([np.array([0]),np.array([0]),pose_temp[75:76]])
# r_thumb1 = np.concatenate([np.array([0]),pose_temp[76:77],np.array([0])])
# l_hand = np.concatenate([l_index1,l_middle1,l_pinky1,l_ring1,l_thumb1])
# r_hand = np.concatenate([r_index1,r_middle1,r_pinky1,r_ring1,r_thumb1])
"""
l_index = np.concatenate([np.array([0]),np.array([0]),pose_temp[66:67],np.array([0]),np.array([0]),pose_temp[66:67],np.array([0]),np.array([0]),pose_temp[66:67]])
l_middle = np.concatenate([np.array([0]),np.array([0]),pose_temp[67:68],np.array([0]),np.array([0]),pose_temp[67:68],np.array([0]),np.array([0]),pose_temp[67:68]])
l_pinky = np.concatenate([np.array([0]),np.array([0]),pose_temp[68:69],np.array([0]),np.array([0]),pose_temp[68:69],np.array([0]),np.array([0]),pose_temp[68:69]])
l_ring = np.concatenate([np.array([0]),np.array([0]),pose_temp[69:70],np.array([0]),np.array([0]),pose_temp[69:70],np.array([0]),np.array([0]),pose_temp[69:70]])
l_thumb = np.concatenate([np.array([0]),pose_temp[70:71],np.array([0]),np.array([0]),pose_temp[70:71],np.array([0]),np.array([0]),pose_temp[71:72],np.array([0])])
r_index = np.concatenate([np.array([0]),np.array([0]),pose_temp[72:73],np.array([0]),np.array([0]),pose_temp[72:73],np.array([0]),np.array([0]),pose_temp[72:73]])
r_middle = np.concatenate([np.array([0]),np.array([0]),pose_temp[73:74],np.array([0]),np.array([0]),pose_temp[73:74],np.array([0]),np.array([0]),pose_temp[73:74]])
r_pinky = np.concatenate([np.array([0]),np.array([0]),pose_temp[74:75],np.array([0]),np.array([0]),pose_temp[74:75],np.array([0]),np.array([0]),pose_temp[74:75]])
r_ring = np.concatenate([np.array([0]),np.array([0]),pose_temp[75:76],np.array([0]),np.array([0]),pose_temp[75:76],np.array([0]),np.array([0]),pose_temp[75:76]])
r_thumb = np.concatenate([np.array([0]),pose_temp[76:77],np.array([0]),np.array([0]),pose_temp[76:77],np.array([0]),np.array([0]),pose_temp[77:77],np.array([0])])
l_hand = np.concatenate([l_index,l_middle,l_pinky,l_ring,l_thumb])
r_hand = np.concatenate([r_index,r_middle,r_pinky,r_ring,r_thumb])
"""
#reorganizing to a better fit to the bones
# pose = np.concatenate([pose_temp[0:66],pose_temp[78:],pose_temp[66:78]])
#somente corpo e cabeca
# pose = np.concatenate([pose_temp[0:66],pose_temp[78:]])
#tentaiva de incluir tratamento para que rodrigues tb cuidasse da mao
# pose = np.concatenate([pose_temp[0:66],pose_temp[78:],l_hand,r_hand])
print('shape: ',data['poses'][frame].shape[0], '- frame:',frame)
# print('shape: ',data['pose_new'][frame].shape[0], '- frame:',frame)
apply_trans_pose_shape(Vector(trans), pose, shape, ob,
arm_ob, obname, scene, cam_ob, frame)
# scene.update()
bpy.context.view_layer.update()
# bpy.ops.export_anim.bvh(filepath=join(params['out'], '{}.bvh'.format(pid)), frame_start=0, frame_end=nFrames-1)
return{'FINISHED'}
class Import_Data_easymocap(Operator, ImportHelper):
bl_idname = "mocap.import_easymocap"
bl_label = "Import data"
bl_description = "Import EasyMOCAP"
filename_ext = ".json"
filter_glob: StringProperty(
default="*.json",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
def execute(self,context):
import os
import json
import bpy
from bpy import context
import math
multiplier = context.scene.sk_value_prop.sk_value
raw_bool = context.scene.sk_value_prop.sk_raw_bool
debug_bool = context.scene.sk_value_prop.sk_debug_bool
# reload_skeleton_bool = context.scene.sk_value_prop.sk_reload_skeleton_bool
limit_rotation_bool = context.scene.sk_value_prop.sk_constraint_limit_rotation_bool
#========================
#EASYMOCAP
#=====================
#path = r'D:\MOCAP\EasyMocap-master\Teste_20210321_1_out\keypoints3d'
path = os.path.dirname(self.filepath)
list_dir = os.listdir(path)
s_list = sorted(list_dir)
data = []
for i in s_list:
with open(path+ os.sep +i,'r') as f:
data.append(json.load(f))
#json.load(f)
x=0
y=1
z=2
name_points = 'Point'
skeleton_import.create_dots(name_points,25)
for item in range(len(data)):
print("frame: ",item)
for limb in range(len(data[item][0]['keypoints3d'])):
# print("limb: ",limb)
bpy.data.objects["Point."+str(1000+limb)[1:]].location[x]=data[item][0]['keypoints3d'][limb][x]
bpy.data.objects["Point."+str(1000+limb)[1:]].location[y]=data[item][0]['keypoints3d'][limb][y]
bpy.data.objects["Point."+str(1000+limb)[1:]].location[z]=data[item][0]['keypoints3d'][limb][z]
#Salva Frame
bpy.data.objects["Point."+str(1000+limb)[1:]].keyframe_insert(data_path="location", frame=item)
#===========
# selectign Scene Collection
scene_collection = bpy.context.view_layer.layer_collection
bpy.context.view_layer.active_layer_collection = scene_collection
bones = [['Bone','Root'],
['Bone.001','Spine'],
['Bone.002','Neck'],
['Bone.003','Face'],
['Bone.004','Arm_L'],
['Bone.005','Forearm_L'],
['Bone.006','Arm_R'],
['Bone.007','Forearm_R'],
['Bone.008','Thigh_L'],
['Bone.009','Leg_L'],
['Bone.010','Foot_L'],
['Bone.011','Thigh_R'],
['Bone.012','Leg_R'],
['Bone.013','Foot_R']
]
skeleton_import.create_bones(bones)
unit = skeleton_import.size_ref_bone('Point.001','Point.008','Point.008')
unit = unit*multiplier
spine_multi = context.scene.sk_value_prop.sk_spine_mulitplier
neck_multi = context.scene.sk_value_prop.sk_neck_mulitplier
head_multi = context.scene.sk_value_prop.sk_head_mulitplier
forearm_multi = context.scene.sk_value_prop.sk_forearm_mulitplier
arm_multi = context.scene.sk_value_prop.sk_arm_mulitplier
tigh_multi = context.scene.sk_value_prop.sk_tigh_mulitplier
leg_multi = context.scene.sk_value_prop.sk_leg_mulitplier
foot_multi = context.scene.sk_value_prop.sk_foot_mulitplier
root_sz =unit/10
spine_sz =unit*3.5*spine_multi
neck_sz =unit*neck_multi
face_sz =unit*head_multi
thigh_sz =unit*3*tigh_multi
leg_sz =unit*2.5*leg_multi
foot_sz =unit*foot_multi #inclinado 45 graud pra frente
arm_sz =unit*1.5*arm_multi
forearm_sz =unit*1.5*forearm_multi
skeleton_import.size_of_bones(unit, root_sz, spine_sz, neck_sz, face_sz, thigh_sz, leg_sz, foot_sz, arm_sz, forearm_sz)
constraints = [
['Root', 'COPY_LOCATION', 'Point.008'],
['Root', 'DAMPED_TRACK', 'Point.001'],
['Root', 'DAMPED_TRACK', 'Point.012', 'TRACK_X', 0.5],
['Root', 'DAMPED_TRACK', 'Point.009', 'TRACK_NEGATIVE_X', 0.5],
['Spine', 'DAMPED_TRACK', 'Point.001'],
['Spine', 'LIMIT_ROTATION', 'LOCAL'],
['Spine', 'LIMIT_ROTATION', 'X', True, -0.349066, 0.349066],
['Spine', 'LIMIT_ROTATION', 'Y', True, -0.698132, 0.698132],
['Spine', 'LIMIT_ROTATION', 'Z', True, -0.174533, 0.174533],
['Neck', 'DAMPED_TRACK', 'Point.018'],
['Neck', 'LIMIT_ROTATION', 'LOCAL'],
['Neck', 'LIMIT_ROTATION', 'X', True, -0.174533, 1.0472],
['Neck', 'LIMIT_ROTATION', 'Y', True, -0.523599, 0.523599],
['Neck', 'LIMIT_ROTATION', 'Z', True, -0.349066, 0.349066],
['Face', 'DAMPED_TRACK', 'Point.000'],
['Face', 'LIMIT_ROTATION', 'LOCAL'],
['Face', 'LIMIT_ROTATION', 'X', True, -0.174533, 0.872665],
['Face', 'LIMIT_ROTATION', 'Y', True, 0, 0],
['Face', 'LIMIT_ROTATION', 'Z', True, -0.523599, 0.523599],
['Arm_L', 'DAMPED_TRACK', 'Point.006'],
['Forearm_L', 'DAMPED_TRACK', 'Point.007'],
['Forearm_L', 'LIMIT_ROTATION', 'LOCAL'],
['Forearm_L', 'LIMIT_ROTATION', 'X', True, 0, 0],
['Forearm_L', 'LIMIT_ROTATION', 'Y', True, 0, 0],
['Forearm_L', 'LIMIT_ROTATION', 'Z', True, -2.53073, -0.191986],
['Arm_R', 'DAMPED_TRACK', 'Point.002'],
['Forearm_R', 'DAMPED_TRACK', 'Point.003'],
['Forearm_R', 'LIMIT_ROTATION', 'LOCAL'],
['Forearm_R', 'LIMIT_ROTATION', 'X', False ],
['Forearm_R', 'LIMIT_ROTATION', 'Y', False ],
['Forearm_R', 'LIMIT_ROTATION', 'Z', True, 0.191986, 2.53073],
['Thigh_L', 'DAMPED_TRACK', 'Point.013'],
['Thigh_L', 'LIMIT_ROTATION', 'LOCAL'],
['Thigh_L', 'LIMIT_ROTATION', 'X', True, -1.76278, 1.3439],
['Thigh_L', 'LIMIT_ROTATION', 'Y', True, 0, 0],
['Thigh_L', 'LIMIT_ROTATION', 'Z', True, -0.785398, 0.174533],
['Leg_L', 'DAMPED_TRACK', 'Point.014'],
['Leg_L', 'LIMIT_ROTATION', 'LOCAL'],
['Leg_L', 'LIMIT_ROTATION', 'X', True, 0.0698132, 2.0944],
['Leg_L', 'LIMIT_ROTATION', 'Y', True, 0, 0],
['Leg_L', 'LIMIT_ROTATION', 'Z', True, 0, 0],
['Foot_L', 'DAMPED_TRACK', 'Point.019'],
['Foot_L', 'LIMIT_ROTATION', 'LOCAL'],
['Foot_L', 'LIMIT_ROTATION', 'X', True, -0.523599, 0.523599],
['Foot_L', 'LIMIT_ROTATION', 'Y', True, 0, 0],
['Foot_L', 'LIMIT_ROTATION', 'Z', True, 0, 0],
['Thigh_R', 'DAMPED_TRACK', 'Point.010'],
['Thigh_R', 'LIMIT_ROTATION', 'LOCAL'],
['Thigh_R', 'LIMIT_ROTATION', 'X', True, -1.76278, 1.3439],
['Thigh_R', 'LIMIT_ROTATION', 'Y', True, 0, 0],
['Thigh_R', 'LIMIT_ROTATION', 'Z', True, -0.174533, 0.785398],
['Leg_R', 'DAMPED_TRACK', 'Point.011'],
['Leg_R', 'LIMIT_ROTATION', 'LOCAL'],
['Leg_R', 'LIMIT_ROTATION', 'X', True, 0.0698132, 2.0944],
['Leg_R', 'LIMIT_ROTATION', 'Y', True, 0, 0],
['Leg_R', 'LIMIT_ROTATION', 'Z', True, 0, 0],
['Foot_R', 'DAMPED_TRACK', 'Point.022'],
['Foot_R', 'LIMIT_ROTATION', 'LOCAL'],
['Foot_R', 'LIMIT_ROTATION', 'X', True, -0.523599, 0.523599],
['Foot_R', 'LIMIT_ROTATION', 'Y', True, 0, 0],
['Foot_R', 'LIMIT_ROTATION', 'Z', True, 0, 0]
]
# add_constraints(constraints)
skeleton_import.add_constraints_track_X(constraints, limit_rotation_bool)
print(len(data))
bpy.context.scene.frame_end = len(data)
if debug_bool == False:
bpy.ops.nla.bake(frame_start=1, frame_end=len(data), visual_keying=True, clear_constraints=True, clear_parents=True, bake_types={'POSE'})
bpy.ops.object.mode_set(mode='OBJECT')
skeleton_import.remove_dots(name_points)
sk_value_prop = context.scene.sk_value_prop
if raw_bool == True:
print('raw_bool True - ',raw_bool)
x_original, y_original, z_original = helper_functions.get_rotations()
sk_value_prop.sk_root_rot_x = math.degrees(x_original)
sk_value_prop.sk_root_rot_y = math.degrees(y_original)
sk_value_prop.sk_root_rot_z = math.degrees(z_original)
#in this case both original and actual is the same, because there was no alteration on the angle
x_actual_deg = math.degrees(x_original)
y_actual_deg = math.degrees(y_original)
z_actual_deg = math.degrees(z_original)
sk_value_prop.sk_root_actual_rot_x = x_actual_deg
sk_value_prop.sk_root_actual_rot_y = y_actual_deg
sk_value_prop.sk_root_actual_rot_z = z_actual_deg
else:
print('raw_bool False - ',raw_bool)
x_deg, y_deg, z_deg = helper_functions.anim_to_origin()
#take the information of the rotation to the panel
print('result x: ',x_deg)
print('result y: ',y_deg)
print('result z: ',z_deg)
sk_value_prop.sk_root_rot_x = x_deg
sk_value_prop.sk_root_rot_y = y_deg
sk_value_prop.sk_root_rot_z = z_deg
return{'FINISHED'}
class Reload_sk_easymocap(Operator):
bl_idname = "mocap.import_easymocap_reload"
bl_label = "Reload Skeleton Easymocap"
bl_description = "Reload SK EasyMOCAP"
def execute(self,context):
bpy.ops.object.mode_set(mode='OBJECT')
multiplier = context.scene.sk_value_prop.sk_value
unit = skeleton_import.size_ref_bone('Point.001','Point.008','Point.008')
unit = unit*multiplier
spine_multi = context.scene.sk_value_prop.sk_spine_mulitplier
neck_multi = context.scene.sk_value_prop.sk_neck_mulitplier
head_multi = context.scene.sk_value_prop.sk_head_mulitplier
forearm_multi = context.scene.sk_value_prop.sk_forearm_mulitplier
arm_multi = context.scene.sk_value_prop.sk_arm_mulitplier
tigh_multi = context.scene.sk_value_prop.sk_tigh_mulitplier
leg_multi = context.scene.sk_value_prop.sk_leg_mulitplier
foot_multi = context.scene.sk_value_prop.sk_foot_mulitplier
root_sz =unit/10
spine_sz =unit*3.5*spine_multi
neck_sz =unit*neck_multi
face_sz =unit*head_multi
thigh_sz =unit*3*tigh_multi
leg_sz =unit*2.5*leg_multi
foot_sz =unit*foot_multi #inclinado 45 graud pra frente
arm_sz =unit*1.5*arm_multi
forearm_sz =unit*1.5*forearm_multi
skeleton_import.size_of_bones(unit, root_sz, spine_sz, neck_sz, face_sz, thigh_sz, leg_sz, foot_sz, arm_sz, forearm_sz)
return{'FINISHED'}
class Import_Data_frankmocap(Operator, ImportHelper):
bl_idname = "mocap.import_frankmocap"
bl_label = "Import data from Frankmocap"
bl_description = "Import FrankMocap"
filename_ext = ".pkl"
filter_glob: StringProperty(
default="*.pkl",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
def execute(self,context):
#"""
#Frnakmocap
#==========================
import math
import bpy
import os
import pickle
import numpy as np
from bpy import context
import joblib
multiplier = context.scene.sk_value_prop.sk_value
raw_bool = context.scene.sk_value_prop.sk_raw_bool
def middle_point(p1,p2,p_middle):
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects[p1].select_set(True)
bpy.data.objects[p2].select_set(True)
bpy.context.view_layer.objects.active = bpy.data.objects[p2]
obs = bpy.context.selected_objects
n = len(obs)
# print('n: ',n)
assert(n)
#scene.cursor.location = sum([o.matrix_world.translation for o in obs], Vector()) / n
bpy.data.objects[p_middle].location = sum([o.matrix_world.translation for o in obs], Vector()) / n
def create_dots(name, amount):
#remove Collection
if bpy.data.collections.find(name) >= 0:
collection = bpy.data.collections.get(name)
#
for obj in collection.objects:
bpy.data.objects.remove(obj, do_unlink=True)
bpy.data.collections.remove(collection)
#cria os pontos nuima collection chamada Points
#=====================================================
collection = bpy.data.collections.new(name)
bpy.context.scene.collection.children.link(collection)
#
layer_collection = bpy.context.view_layer.layer_collection.children[collection.name]
bpy.context.view_layer.active_layer_collection = layer_collection
#
for point in range(amount):
bpy.ops.mesh.primitive_plane_add(enter_editmode=True, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.ops.mesh.merge(type='CENTER')
bpy.ops.object.editmode_toggle()
bpy.context.active_object.name = name+'.'+str(1000+point)[1:]
#=====================================================
def distance(point1, point2) -> float:
#Calculate distance between two points in 3D.
# return math.sqrt((point2[0] - point1[0]) ** 2 + (point2[1] - point1[1]) ** 2 + (point2[2] - point1[2]) ** 2)
return math.sqrt((point2.location[0] - point1.location[0]) ** 2 + (point2.location[1] - point1.location[1]) ** 2 + (point2.location[2] - point1.location[2]) ** 2)
def size_bone(point_name1, point_name2, bone):
p1 = bpy.data.objects[point_name1]
p2 = bpy.data.objects[point_name2]
#edit bones
if bpy.context.active_object.mode == 'EDIT':
bpy.context.object.data.edit_bones[bone].length= distance(p1,p2)
else:
bpy.ops.object.editmode_toggle()
bpy.context.object.data.edit_bones[bone].length= distance(p1,p2)
bpy.ops.object.editmode_toggle()
def create_bones(bones_list):
#===================================
#creating bones
#====================================
bpy.ops.object.armature_add(enter_editmode=True, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1)) #cria armature e primeiro bone
#bpy.ops.object.editmode_toggle()
#bpy.data.armatures['Armature'].edit_bones.active = bpy.context.object.data.edit_bones['Bone']
obs = []
for ob in bpy.context.scene.objects:
if ob.type == 'ARMATURE':
obs.append(ob)
#obs
bpy.ops.armature.select_all(action='DESELECT')
obs[len(obs)-1].data.edit_bones['Bone'].select_tail=True
bpy.ops.armature.bone_primitive_add()#Spine
#Neck
bpy.ops.armature.extrude_move(ARMATURE_OT_extrude={"forked":False}, TRANSFORM_OT_translate={"value":(0.0, 0.0, 0.1)})
#Face
bpy.ops.armature.extrude_move(ARMATURE_OT_extrude={"forked":False}, TRANSFORM_OT_translate={"value":(0.0, 0.0, 0.1)})
bpy.ops.armature.bone_primitive_add()#Arm_L
#Forearm_L
bpy.ops.armature.extrude_move(ARMATURE_OT_extrude={"forked":False}, TRANSFORM_OT_translate={"value":(0.0, 0.0, 0.1)})
bpy.ops.armature.bone_primitive_add()#Arm_R
#Forearm_R
bpy.ops.armature.extrude_move(ARMATURE_OT_extrude={"forked":False}, TRANSFORM_OT_translate={"value":(0.0, 0.0, 0.1)})
bpy.ops.armature.bone_primitive_add()#Thigh_L
#Leg_L
#Foot_L
bpy.ops.armature.extrude_move(ARMATURE_OT_extrude={"forked":False}, TRANSFORM_OT_translate={"value":(0.0, 0.0, 0.1)})
bpy.ops.armature.extrude_move(ARMATURE_OT_extrude={"forked":False}, TRANSFORM_OT_translate={"value":(0.0, 0.0, 0.1)})
bpy.ops.armature.bone_primitive_add()#Thigh_R
#Leg_R
#Foot_R
bpy.ops.armature.extrude_move(ARMATURE_OT_extrude={"forked":False}, TRANSFORM_OT_translate={"value":(0.0, 0.0, 0.1)})
bpy.ops.armature.extrude_move(ARMATURE_OT_extrude={"forked":False}, TRANSFORM_OT_translate={"value":(0.0, 0.0, 0.1)})
for i in range(len(bones_list)):
obs[len(obs)-1].data.edit_bones[bones_list[i][0]].name = bones_list[i][1]
#Hierarquia
bpy.context.object.data.edit_bones["Spine"].parent = bpy.context.object.data.edit_bones["Root"]
bpy.context.object.data.edit_bones["Arm_L"].parent = bpy.context.object.data.edit_bones["Spine"]
bpy.context.object.data.edit_bones["Arm_R"].parent = bpy.context.object.data.edit_bones["Spine"]
bpy.context.object.data.edit_bones["Thigh_L"].parent = bpy.context.object.data.edit_bones["Root"]
bpy.context.object.data.edit_bones["Thigh_R"].parent = bpy.context.object.data.edit_bones["Root"]
bpy.ops.object.editmode_toggle()
def distance(point1, point2) -> float:
#Calculate distance between two points in 3D.
#return math.sqrt((point2[0] - point1[0]) ** 2 + (point2[1] - point1[1]) ** 2 + (point2[2] - point1[2]) ** 2)
return math.sqrt((point2.location[0] - point1.location[0]) ** 2 + (point2.location[1] - point1.location[1]) ** 2 + (point2.location[2] - point1.location[2]) ** 2)
def size_bone(point_name1, point_name2, bone):
p1 = bpy.data.objects[point_name1]
p2 = bpy.data.objects[point_name2]
#edit bones
if bpy.context.active_object.mode == 'EDIT':
bpy.context.object.data.edit_bones[bone].length= distance(p1,p2)
else:
bpy.ops.object.editmode_toggle()
bpy.context.object.data.edit_bones[bone].length= distance(p1,p2)
bpy.ops.object.editmode_toggle()
def size_ref_bone(p1,p2,p_final):
from mathutils import Vector
import bpy
## size of the reference bone (spine)
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects[p1].select_set(True)
bpy.data.objects[p2].select_set(True)
# bpy.context.view_layer.objects.active = bpy.data.objects['Point.034']
bpy.context.view_layer.objects.active = bpy.data.objects[p2]
obs = bpy.context.selected_objects
n = len(obs)
# print('n: ',n)
assert(n)
#scene.cursor.location = sum([o.matrix_world.translation for o in obs], Vector()) / n
#bpy.data.objects[p_middle].location = sum([o.matrix_world.translation for o in obs], Vector()) / n
x_subtract = abs(obs[0].matrix_world.translation.x - obs[1].matrix_world.translation.x)
y_subtract = abs(obs[0].matrix_world.translation.y - obs[1].matrix_world.translation.y)
z_subtract = abs(obs[0].matrix_world.translation.z - obs[1].matrix_world.translation.z)
max(x_subtract, y_subtract, z_subtract) #maior das medidas
unit_def = max(x_subtract, y_subtract, z_subtract)/3
#end of size of reference bone Spine
return unit_def
def size_of_bones(root_size, spine_size, neck_size, face_size, thigh_size, leg_size, foot_size, arm_size, forearm_size):
#==========================================
#selecting and making the armature Active
#selecionando armature
#==========================================
bpy.ops.object.select_all(action='DESELECT')
#bpy.ops.armature.select_all(action='DESELECT')
obs = []
for ob in bpy.context.scene.objects:
if ob.type == 'ARMATURE':
obs.append(ob)
#obs
armature = obs[len(obs)-1].name
#bpy.data.objects[armature].select_set(True)
obs[len(obs)-1].select_set(True)
view_layer = bpy.context.view_layer
#Armature_obj = bpy.context.scene.objects[armature]
Armature_obj = obs[len(obs)-1]
view_layer.objects.active = Armature_obj
#converting to euler rotation
order = 'XYZ'
context = bpy.context
rig_object = context.active_object
for pb in rig_object.pose.bones:
pb.rotation_mode = order
bpy.ops.object.editmode_toggle()
#changing location
#resetting
bpy.context.object.data.edit_bones["Spine"].head.xy=0
bpy.context.object.data.edit_bones["Neck"].head.xy=0
bpy.context.object.data.edit_bones["Face"].head.xy=0
bpy.context.object.data.edit_bones["Arm_L"].head.xy=0
bpy.context.object.data.edit_bones["Forearm_L"].head.xy=0
bpy.context.object.data.edit_bones["Arm_R"].head.xy=0
bpy.context.object.data.edit_bones["Forearm_R"].head.xy=0
bpy.context.object.data.edit_bones["Thigh_L"].head.xy=0
bpy.context.object.data.edit_bones["Leg_L"].head.xy=0
bpy.context.object.data.edit_bones["Foot_L"].head.xy=0
bpy.context.object.data.edit_bones["Thigh_R"].head.xy=0
bpy.context.object.data.edit_bones["Leg_R"].head.xy=0
bpy.context.object.data.edit_bones["Foot_R"].head.xy=0
#tail
bpy.context.object.data.edit_bones["Face"].tail.xy=0
bpy.context.object.data.edit_bones["Neck"].tail.xy=0
bpy.context.object.data.edit_bones["Forearm_L"].tail.xy=0
bpy.context.object.data.edit_bones["Forearm_R"].tail.xy=0
bpy.context.object.data.edit_bones["Foot_L"].tail.xy=0
bpy.context.object.data.edit_bones["Foot_R"].tail.xy=0
bpy.context.object.data.edit_bones["Root"].length = root_size
bpy.context.object.data.edit_bones["Spine"].head.z = unit/2
bpy.context.object.data.edit_bones["Spine"].tail.z = spine_size
bpy.context.object.data.edit_bones["Neck"].tail.z = spine_size + neck_size
bpy.context.object.data.edit_bones["Neck"].tail.y = neck_size/3
bpy.context.object.data.edit_bones["Face"].tail.z = spine_size + neck_size
bpy.context.object.data.edit_bones["Face"].tail.y = face_size*-1
bpy.context.object.data.edit_bones["Arm_L"].head.z= spine_size
bpy.context.object.data.edit_bones["Arm_L"].head.x= unit*3/4
bpy.context.object.data.edit_bones["Forearm_L"].head.z= spine_size
bpy.context.object.data.edit_bones["Forearm_L"].head.x= unit + arm_size
bpy.context.object.data.edit_bones["Forearm_L"].tail.z= spine_size
bpy.context.object.data.edit_bones["Forearm_L"].tail.x= unit + arm_size + forearm_size
bpy.context.object.data.edit_bones["Arm_R"].head.z= spine_size
bpy.context.object.data.edit_bones["Arm_R"].head.x= (unit*3/4)*-1
bpy.context.object.data.edit_bones["Forearm_R"].head.z= spine_size
bpy.context.object.data.edit_bones["Forearm_R"].head.x= (unit + arm_size) *-1
bpy.context.object.data.edit_bones["Forearm_R"].tail.z= spine_size
bpy.context.object.data.edit_bones["Forearm_R"].tail.x= (unit + arm_size + forearm_size) *-1
bpy.context.object.data.edit_bones["Thigh_L"].head.x= unit*3/4
bpy.context.object.data.edit_bones["Thigh_L"].head.z= (unit/5)*-1
bpy.context.object.data.edit_bones["Leg_L"].head.x= unit*3/4
bpy.context.object.data.edit_bones["Leg_L"].head.z= (unit/5 + thigh_size)*-1
bpy.context.object.data.edit_bones["Foot_L"].head.x= unit*3/4
bpy.context.object.data.edit_bones["Foot_L"].head.z= (unit/5 + thigh_size + leg_size)*-1
bpy.context.object.data.edit_bones["Foot_L"].tail.x= unit*3/4
bpy.context.object.data.edit_bones["Foot_L"].tail.z= (unit/5 + thigh_size + leg_size + foot_size/2)*-1
bpy.context.object.data.edit_bones["Foot_L"].tail.y= foot_sz/2*-1
bpy.context.object.data.edit_bones["Thigh_R"].head.x= unit*3/4*-1
bpy.context.object.data.edit_bones["Thigh_R"].head.z= (unit/5)*-1
bpy.context.object.data.edit_bones["Leg_R"].head.x= unit*3/4*-1
bpy.context.object.data.edit_bones["Leg_R"].head.z= (unit/5 + thigh_size)*-1
bpy.context.object.data.edit_bones["Foot_R"].head.x= unit*3/4*-1
bpy.context.object.data.edit_bones["Foot_R"].head.z= (unit/5 + thigh_size + leg_size)*-1
bpy.context.object.data.edit_bones["Foot_R"].tail.x= unit*3/4*-1
bpy.context.object.data.edit_bones["Foot_R"].tail.z= (unit/5 + thigh_size + leg_size + foot_size/2)*-1
bpy.context.object.data.edit_bones["Foot_R"].tail.y= foot_size/2*-1
bpy.ops.object.editmode_toggle()
def add_constraints_track_X(constraints):