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
|
import array
from os import name
import bpy
from bpy.props import *
from io_anm import anm_types
import operator
from mathutils import Matrix, Euler, Vector, Quaternion
BONE_DEF_LAYER = 29
MOBILITY_LAYER = 25
WEAPON_DEF_LAYER = 24
WEAPON_VIEW_LAYER = 16
NUM_LAYERS = 32
def Get4ByteIntArray(): # Creates an array with 4-byte components,
var = array.array('l') # regardless of whether that means 'l' or 'i'
if var.itemsize != 4:
var = array.array('i')
return var
def AddConnectingBones(arm_data): # Add a bone between disconnected parent-child pairs,
for bone in arm_data.edit_bones: # such as the head and the ears.
if not bone.parent or \
bone.name == "root" or \
bone.parent.name == "root" or \
bone.layers[BONE_DEF_LAYER] == False or \
bone.parent.layers[BONE_DEF_LAYER] == False or \
bone.head == bone.parent.tail:
continue
bpy.ops.armature.bone_primitive_add(name="connector")
new_bone = arm_data.edit_bones[-1]
new_bone.head = bone.parent.tail
new_bone.tail = bone.head
new_bone.parent = bone.parent
for i in range(NUM_LAYERS):
new_bone.layers[i] = False
new_bone.layers[BONE_DEF_LAYER] = True
bone.parent = new_bone
bpy.context.view_layer.update()
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')
class BoneNameTranslator():
def __init__(self, arm_data):
bone_labels = {} # {int bone_id, bool exists}
self.to_bone_map = {} # {string desc_label, string bone_id_label}
self.from_bone_map = {} # {string bone_id_label, string desc_label}
self.num_weapons = 0
for bone in arm_data.bones: # Handle bones that are already named "Bone_#"
if bone.name == "root" or bone.layers[BONE_DEF_LAYER] == False:
continue
if bone.name.split("_")[0] == "Bone":
bone_id = int(bone.name.split("_")[-1])
bone_labels[bone_id] = True
self.to_bone_map[bone.name] = bone.name
self.from_bone_map[bone.name] = bone.name
unused_bone_id = 0
for bone in arm_data.bones: # Rename and record all skeleton bones
if bone.name == "root" or bone.layers[BONE_DEF_LAYER] == False:
continue
if bone.name.split("_")[0] != "Bone":
while unused_bone_id in bone_labels:
unused_bone_id = unused_bone_id + 1
self.from_bone_map["Bone_"+str(unused_bone_id)] = bone.name
self.to_bone_map[bone.name] = "Bone_"+str(unused_bone_id)
bone.name = "Bone_"+str(unused_bone_id)
unused_bone_id = unused_bone_id+1
unused_weapon_id = 0
for bone in arm_data.bones: # Rename and record all weapon bones
if bone.name == "root" or \
bone.layers[WEAPON_DEF_LAYER] == False or \
bone.name.split('.')[0] == "connector":
continue
if bone.name.split("_")[0] != "Weap":
while unused_weapon_id in bone_labels:
unused_weapon_id = unused_weapon_id + 1
self.from_bone_map["Weap_"+str(unused_weapon_id)] = bone.name
self.to_bone_map[bone.name] = "Weap_"+str(unused_weapon_id)
bone.name = "Weap_"+str(unused_weapon_id)
unused_weapon_id += 1
self.num_weapons += 1
def ToBoneName(self,name):
return self.to_bone_map.get(name)
def FromBoneName(self,name):
return self.from_bone_map.get(name)
def to_array(array, idarray): #Populate an array with elements from a list or dictionary
for item in idarray:
array.append(item)
def GetInitialBoneMatZ(edit_bone): #Get initial bone matrix that is facing the z axis
z_vec = (edit_bone.tail - edit_bone.head).normalized()
x_vec = Vector((0,0,1))
y_vec = x_vec.cross(z_vec).normalized()
x_vec = y_vec.cross(z_vec).normalized()
return [x_vec[0], x_vec[2], -x_vec[1], 0,
y_vec[0], y_vec[2], -y_vec[1], 0,
z_vec[0], z_vec[2], -z_vec[1], 0,
0,0,0,1]
def GetInitialBoneMat(edit_bone): #Get initial bone matrix that is facing the y axis (or z if needed)
z_vec = (edit_bone.tail - edit_bone.head).normalized()
y_vec = Vector((0,1,0))
if abs(z_vec.dot(y_vec)) > 0.95:
return GetInitialBoneMatZ(edit_bone)
x_vec = z_vec.cross(y_vec).normalized()
y_vec = z_vec.cross(x_vec).normalized()
return [x_vec[0], x_vec[2], -x_vec[1], 0,
y_vec[0], y_vec[2], -y_vec[1], 0,
z_vec[0], z_vec[2], -z_vec[1], 0,
0,0,0,1]
initial_bone_mats = {} # { string name, float mat[16] }
def CalcInitialBoneMats(data): # Calculate initial bone matrices if they are not already provided
for bone in data.edit_bones:
if not bone.get("mat"):
initial_bone_mats[bone.name] = GetInitialBoneMat(bone)
else:
initial_bone_mats[bone.name] = bone["mat"]
def GetBoneMatrixRotation(arm_data, num, rotations, rotation_modes):
bone = arm_data.bones["Bone_"+str(num)]
if rotation_modes[num] == 'QUATERNION':
matrix = Quaternion(rotations[num]).normalized().to_matrix()
else:
matrix = Euler(rotations[num][0:3],rotation_modes[num]).to_matrix()
# Convert matrix to bone space
matrix = bone.matrix_local.to_3x3() @ matrix @ bone.matrix_local.to_3x3().inverted_safe()
# Convert matrix to parent space by applying initial bone rotation
if initial_bone_mats[bone.name]:
mat = array.array('f')
to_array(mat, initial_bone_mats[bone.name])
initial_matrix = Matrix(([mat[0], -mat[2], mat[1], mat[3]],
[mat[4], -mat[6], mat[5], mat[7]],
[mat[8], -mat[10], mat[9], mat[11]],
[mat[12], -mat[14], mat[13], mat[15]]))
initial_matrix.transpose()
matrix = matrix.to_4x4() @ initial_matrix
else:
print("Could not find: " + bone.name)
# Convert matrix to world space by recursively applying all parent rotations
parent = bone.parent
if not parent or parent.name == "root" or parent.layers[BONE_DEF_LAYER] == False or bone.layers[BONE_DEF_LAYER] == False:
return matrix
if initial_bone_mats.get(parent.name):
mat = array.array('f')
to_array(mat, initial_bone_mats[parent.name])
parent_initial_matrix = Matrix(([mat[0], -mat[2], mat[1], mat[3]],
[mat[4], -mat[6], mat[5], mat[7]],
[mat[8], -mat[10], mat[9], mat[11]],
[mat[12], -mat[14], mat[13], mat[15]]))
parent_initial_matrix.transpose()
parent_num = int(parent.name.split("_")[-1])
parent_matrix = GetBoneMatrixRotation(arm_data, parent_num, rotations, rotation_modes)
matrix = parent_matrix @ parent_initial_matrix.inverted_safe() @ matrix
return matrix
def GetBoneMatrixTranslation(arm_data, num, rotations, rotation_modes):
bone = arm_data.bones["Bone_"+str(num)]
if rotation_modes[num] == 'QUATERNION':
matrix = Quaternion(rotations[num]).normalized().to_matrix()
else:
matrix = Euler(rotations[num][0:3],rotation_modes[num]).to_matrix()
matrix.resize_4x4()
matrix = bone.matrix_local @ matrix
# Convert matrix to world space by recursively applying all parent rotations
parent = bone.parent
if not parent or parent.name == "root" or parent.layers[BONE_DEF_LAYER] == False or bone.layers[BONE_DEF_LAYER] == False:
return matrix
parent_num = int(parent.name.split("_")[-1])
parent_matrix = GetBoneMatrixTranslation(arm_data, parent_num, rotations, rotation_modes) @ parent.matrix_local.inverted_safe()
matrix = parent_matrix @ matrix
return matrix
def GetNumJoints(arm_obj):
num = 0
for bone in arm_obj.pose.bones:
for constraint in bone.constraints:
if constraint.type == 'LIMIT_ROTATION' and constraint.owner_space == 'LOCAL':
num += 1
return num
def KeyframeFromPose(arm_obj, rotations, rotation_modes, translation, num_joints):
arm_data = arm_obj.data
matrices = []
keyframe = anm_types.Keyframe()
for num in range(len(rotations)):
# Get rotation matrix, then set translation
matrix = GetBoneMatrixRotation(arm_data, num, rotations, rotation_modes)
trans_matrix = GetBoneMatrixTranslation(arm_data, num, rotations, rotation_modes)
bone = arm_data.bones["Bone_"+str(num)]
matrix[3] = trans_matrix @ Vector((0,bone.length*0.5,0,1));
#matrix[3] = matrix[3] + Vector((translation[1], -translation[0], translation[2], 0))
matrix[3] = matrix[3] + Vector((translation[0], translation[1], translation[2], 0))
'''bpy.ops.object.add(type='EMPTY', location=(matrix[3][0]+3,matrix[3][1],matrix[3][2]))
ob = bpy.context.view_layer.objects.active
ob.empty_draw_type = 'ARROWS'
ob.empty_draw_size = 0.1
ob.rotation_mode = 'QUATERNION'
ob.rotation_quaternion = matrix.rotation_part().to_quaternion()'''
mat = array.array('f')
mat.fromlist([matrix[0][0], matrix[2][0], -matrix[1][0], matrix[0][3],
matrix[0][1], matrix[2][1], -matrix[1][1], matrix[1][3],
matrix[0][2], matrix[2][2], -matrix[1][2], matrix[2][3],
matrix[3][0], matrix[3][2], -matrix[3][1], matrix[3][3]])
keyframe.mats.append(mat)
return keyframe
class KeyframeInfo:
def __init__(self):
self.weap_relative_weight = {}
self.weap_relative_id = {}
self.weap_translations = {}
self.weap_rotations = {}
self.mobility_translation = {}
self.mobility_rotation = {}
self.rotations = {}
self.rotation_modes = {}
self.weights = {}
self.translation = array.array('f')
self.translation.fromlist([0,0,0])
self.translation_offset = array.array('f')
self.translation_offset.fromlist([0,0,0])
self.ik_bones = []
self.shape_keys = []
self.status_keys = []
self.events = []
def __repr__(self):
string = ""
for bone_id in self.rotations:
string = string + "Bone " + str(bone_id) + ": \n"
string = string + str(self.rotations[bone_id])
string = string + "\n"
return string
old_trans = {} # {string, Vector()} Initial translation of each bone
# We are at a specific frame, so read back the bone matrix info
def BakeBoneConstraints(bone,obj,old_obj,parent_matrix):
pose_bone = old_obj.pose.bones[bone.name]
inv_bone_matrix = bone.matrix_local.inverted_safe()
inv_parent_matrix = parent_matrix.inverted_safe()
matrix_local = inv_bone_matrix @ inv_parent_matrix @ pose_bone.matrix
# Set keyframes for local translation and rotation values
new_pose_bone = obj.pose.bones[bone.name]
new_pose_bone.location = matrix_local.to_translation()
new_pose_bone.keyframe_insert("location")
if new_pose_bone.rotation_mode == 'QUATERNION':
new_pose_bone.rotation_quaternion = matrix_local.to_quaternion().normalized()
new_pose_bone.keyframe_insert("rotation_quaternion")
else:
new_pose_bone.rotation_euler = matrix_local.to_euler(pose_bone.rotation_mode)
new_pose_bone.keyframe_insert("rotation_euler")
matrix = parent_matrix @ bone.matrix_local @ new_pose_bone.matrix_basis @ inv_bone_matrix
if bone.name != "root" and bone.layers[BONE_DEF_LAYER] == True:
mat = pose_bone.matrix
new_trans = mat.to_translation()
new_pose_bone.location = new_trans - (old_trans[bone.name]*2)
new_pose_bone.keyframe_insert("location")
if bone.layers[WEAPON_DEF_LAYER] == True or bone.name == "mobility":
old_pose_bone = old_obj.pose.bones[bone.name]
new_pose_bone = obj.pose.bones[bone.name]
new_pose_bone.location = (old_pose_bone.matrix[0][3],old_pose_bone.matrix[1][3],old_pose_bone.matrix[2][3])
new_pose_bone.keyframe_insert("location")
new_pose_bone.rotation_quaternion = old_pose_bone.matrix.to_quaternion().normalized()
new_pose_bone.keyframe_insert("rotation_quaternion")
for child in bone.children:
BakeBoneConstraints(child,obj,old_obj,matrix)
#stretch_amount = 600
stretch_amount = 1
# Walk through each keyframe and bake all constraints
def BakeConstraints(obj):
action = obj.animation_data.action
times = {}
for fcurve in action.fcurves:
for key in fcurve.keyframe_points:
key.co[0] = int(key.co[0]*stretch_amount)
key.handle_left[0] = int(key.handle_left[0]*stretch_amount)
key.handle_right[0] = int(key.handle_right[0]*stretch_amount)
times[key.co[0]] = True
sorted_times = []
for time in times:
sorted_times.append(time)
sorted_times.sort()
bpy.ops.object.duplicate()
new_obj = bpy.context.view_layer.objects.active
# Go to each frame in sequence so Blender calculates the bone matrices
#frame = bpy.context.scene.frame_current
frame = 0
for time in sorted_times:
num = int(time)
bpy.context.scene.frame_set(num)
BakeBoneConstraints(obj.data.bones["root"], obj, new_obj, Matrix())
bpy.context.scene.frame_set(frame)
bpy.ops.object.delete()
bpy.context.view_layer.objects.active = obj
action.user_clear()
for fcurve in action.fcurves:
for key in fcurve.keyframe_points:
key.co[0] /= stretch_amount
key.handle_left[0] /= stretch_amount
key.handle_right[0] /= stretch_amount
bpy.ops.object.mode_set(mode='EDIT')
for bone in obj.data.edit_bones:
if bone.layers[BONE_DEF_LAYER] == False:
continue
while bone.parent and bone.parent.layers[BONE_DEF_LAYER] == False and bone.parent.parent:
bone.parent = bone.parent.parent
bpy.context.view_layer.update()
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')
def GetAnimation(filepath):
old_arm_obj = bpy.context.view_layer.objects.active
if old_arm_obj.type != 'ARMATURE':
print("PHXBN armature must be selected")
return
#Create a copy of the armature object so we don't mess up the original
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.duplicate()
arm_obj = bpy.context.view_layer.objects.active
#Add connecting bones between parent-child pairs that are not connected
bpy.ops.object.mode_set(mode='EDIT')
AddConnectingBones(arm_obj.data)
#Rename relevant bones to Bone_0, Bone_1, etc
tbr = BoneNameTranslator(arm_obj.data)
#txt_file = open(filepath+".txt", "w")
#for num in range(100):
# name = "Bone_"+str(num)
# if tbr.from_bone_map.get(name):
# txt_file.write(name+": "+tbr.from_bone_map.get(name)+'\n')
#Get initial root translation of each bone
for bone in arm_obj.data.edit_bones:
old_trans[bone.name] = bone.head - arm_obj.data.edit_bones["root"].head
#Get initial bone rotation of each bone (using OG Bullet physics orientations)
CalcInitialBoneMats(arm_obj.data)
#Bake the constraints into the bone matrices
bpy.ops.object.mode_set(mode='OBJECT')
BakeConstraints(arm_obj)
action = arm_obj.animation_data.action
if not action:
print("Armature must have an action")
return
#Create animation data structure and start extracting the easy data
data = anm_types.ANMdata()
if action.get("Looping"):
data.looping = action["Looping"]
else:
data.looping = False
if action.get("Start"):
data.start = action["Start"]
else:
data.start = int(bpy.context.scene.frame_start*1000/bpy.context.scene.render.fps)
if action.get("End"):
data.end = action["End"]
else:
data.end = int(bpy.context.scene.frame_end*1000/bpy.context.scene.render.fps)
if action.get("Version"):
data.version = action["Version"]
else:
data.version = 4
keyframes = {}
# Record all of the times that have keyframes on them
pose_bones = arm_obj.pose.bones
for fcurve in action.fcurves:
for keyframe in fcurve.keyframe_points:
time = round(keyframe.co[0]*1000/bpy.context.scene.render.fps)
if not time in keyframes:
keyframes[time] = KeyframeInfo()
# Get an ordered list of all the keyframe times
keyframe_times = []
for time in keyframes:
keyframe_times.append(time)
keyframe_times.sort()
# Sample each fcurve at each keyframe time
for fcurve in action.fcurves:
quote_split = fcurve.data_path.split("\"")
if len(quote_split) < 2:
continue
event_split = fcurve.data_path.split("event_")
if len(event_split) == 2:
event_name = event_split[1].split("\"")[0]
for keyframe in fcurve.keyframe_points:
time = round(keyframe.co[0]*1000/bpy.context.scene.render.fps)
key_info = keyframes[time]
event = []
event.append(event_name)
bone_name = quote_split[1]
bone_id = int(bone_name.split("_")[-1])
event.append(bone_id)
key_info.events.append(event)
continue
weight_split = fcurve.data_path.split("weight")
if len(weight_split) == 2:
for time in keyframes:
key_info = keyframes[time]
bone_name = quote_split[1]
bone_id = int(bone_name.split("_")[-1])
key_info.weights[bone_id] = fcurve.evaluate(time/1000*bpy.context.scene.render.fps)
continue
shape_split = fcurve.data_path.split("shape_")
if len(shape_split) == 2:
shape_name = shape_split[1].split("\"")[0]
for time in keyframes:
key_info = keyframes[time]
shape_key = []
shape_key.append(shape_name)
shape_key.append(fcurve.evaluate(time/1000*bpy.context.scene.render.fps))
key_info.shape_keys.append(shape_key)
continue
status_split = fcurve.data_path.split("status_")
if len(status_split) == 2:
status_name = status_split[1].split("\"")[0]
for time in keyframes:
key_info = keyframes[time]
status = []
status.append(status_name)
status.append(fcurve.evaluate(time/1000*bpy.context.scene.render.fps))
key_info.status_keys.append(status)
continue
if quote_split[1] == "root":
if fcurve.data_path.split(".")[-1] == "location":
array_index = fcurve.array_index
for time in keyframes:
key_info = keyframes[time]
key_info.translation[array_index] = fcurve.evaluate(time/1000*bpy.context.scene.render.fps)
continue
bone_name =quote_split[1]
if bone_name.split("_")[0] == "Weap":
weap_id = int(bone_name.split("_")[-1])
array_index = fcurve.array_index
path_end = fcurve.data_path.split(".")[-1]
if path_end == "location":
print("Handling weap location "+str(weap_id))
for time in keyframe_times:
val = fcurve.evaluate(time/1000*bpy.context.scene.render.fps)
key_info = keyframes[time]
if not weap_id in key_info.weap_translations:
key_info.weap_translations[weap_id] = [0,0,0]
key_info.weap_translations[weap_id][array_index] = val
continue
if path_end == "influence":
name = quote_split[3]
constraint = pose_bones[bone_name].constraints[name]
if constraint.type == 'CHILD_OF':
#print("Child of detected")
#print(fcurve.data_path)
#print(constraint.target)
#print(constraint.subtarget)
for time in keyframe_times:
val = fcurve.evaluate(time/1000*bpy.context.scene.render.fps)
key_info = keyframes[time]
key_info.weap_relative_id[weap_id] = int(constraint.subtarget.split("_")[-1])
key_info.weap_relative_weight[weap_id] = val
#print(str(time) + " " + str(key_info.weap_relative_id[weap_id]) + " " + str(key_info.weap_relative_weight[weap_id]))
continue
if path_end != "rotation_quaternion":
continue
for time in keyframe_times:
val = fcurve.evaluate(time/1000*bpy.context.scene.render.fps)
key_info = keyframes[time]
if not weap_id in key_info.weap_rotations:
key_info.weap_rotations[weap_id] = [0,0,0,0]
key_info.weap_rotations[weap_id][array_index] = val
if bone_name == "mobility":
array_index = fcurve.array_index
path_end = fcurve.data_path.split(".")[-1]
if path_end == "location":
print("Handling mobility location "+str(array_index))
for time in keyframe_times:
val = fcurve.evaluate(time/1000*bpy.context.scene.render.fps)
key_info = keyframes[time]
key_info.mobility_translation[array_index] = val
continue
if path_end == "rotation_quaternion":
print("Handling mobility rotation "+str(array_index))
for time in keyframe_times:
val = fcurve.evaluate(time/1000*bpy.context.scene.render.fps)
key_info = keyframes[time]
key_info.mobility_rotation[array_index] = val
if not bone_name.split("_")[0] == "Bone":
continue
bone_id = int(bone_name.split("_")[-1])
array_index = fcurve.array_index
path_end = fcurve.data_path.split(".")[-1]
if path_end == "influence":
name = quote_split[3]
constraint = pose_bones["Bone_"+str(bone_id)].constraints[name]
if constraint.type != 'IK' or constraint.target:
continue
ik_bone_data = []
ik_bone_data.append(name)
ik_bone_data.append(bone_id)
for time in keyframe_times:
if fcurve.evaluate(time/1000*bpy.context.scene.render.fps)>0.5 :
key_info = keyframes[time]
key_info.ik_bones.append(ik_bone_data)
if path_end == "location":
if pose_bones["Bone_"+str(bone_id)].parent.name == "root":
for time in keyframe_times:
key_info = keyframes[time]
key_info.translation_offset[array_index] = fcurve.evaluate(time/1000*bpy.context.scene.render.fps)
continue
if path_end != "rotation_euler" and path_end != "rotation_quaternion":
continue
for time in keyframe_times:
key_info = keyframes[time]
key_info.rotation_modes[bone_id] = pose_bones["Bone_"+str(bone_id)].rotation_mode
if not bone_id in key_info.rotations:
key_info.rotations[bone_id] = []
while array_index >= len(key_info.rotations[bone_id]):
key_info.rotations[bone_id].append(0)
key_info.rotations[bone_id][array_index] = fcurve.evaluate(time/1000*bpy.context.scene.render.fps)
num_joints = GetNumJoints(arm_obj)
if not arm_obj.data.layers[WEAPON_VIEW_LAYER]:
print("Not exporting weapons because weapon layer is hidden")
tbr.num_weapons = 0
print("Num weapons: "+str(tbr.num_weapons))
for time in keyframe_times:
rotations = keyframes[time].rotations
rotation_modes = keyframes[time].rotation_modes
keyframes[time].translation[0] = keyframes[time].translation_offset[0]
keyframes[time].translation[1] = keyframes[time].translation_offset[1]
keyframes[time].translation[2] = keyframes[time].translation_offset[2]
translation = keyframes[time].translation
# Get bone matrices from joint rotations and root translation
keyframe = KeyframeFromPose(arm_obj, rotations, rotation_modes, translation, num_joints)
keyframe.time = time
if len(keyframes[time].mobility_translation) == 3 and \
len(keyframes[time].mobility_rotation) == 4:
m_translation = keyframes[time].mobility_translation
m_rotation = keyframes[time].mobility_rotation
quat = Quaternion()
quat[0] = m_rotation[0]
quat[1] = m_rotation[1]
quat[2] = m_rotation[2]
quat[3] = m_rotation[3]
matrix = quat.to_matrix().to_4x4()
matrix[3][0] = m_translation[0]
matrix[3][1] = m_translation[1]
matrix[3][2] = m_translation[2]
mat = array.array('f')
mat.fromlist([matrix[0][0], matrix[0][2], -matrix[0][1], matrix[0][3],
matrix[1][0], matrix[1][2], -matrix[1][1], matrix[1][3],
matrix[2][0], matrix[2][2], -matrix[2][1], matrix[2][3],
matrix[3][0], matrix[3][2], -matrix[3][1], matrix[3][3]])
keyframe.mobility_mat = mat
for i in range(len(rotations)):
if i in keyframes[time].weights:
keyframe.weights.append(keyframes[time].weights[i])
else:
keyframe.weights.append(0.0)
for i in range(tbr.num_weapons):
w_translation = keyframes[time].weap_translations[i]
rotation = keyframes[time].weap_rotations[i]
quat = Quaternion()
quat[0] = rotation[0]
quat[1] = rotation[1]
quat[2] = rotation[2]
quat[3] = rotation[3]
matrix = quat.to_matrix().to_4x4()
matrix[3][0] = w_translation[0]
matrix[3][1] = w_translation[1] - 0.03
matrix[3][2] = w_translation[2] - 0.69
mat = array.array('f')
mat.fromlist([matrix[0][0], matrix[0][2], -matrix[0][1], matrix[0][3],
matrix[1][0], matrix[1][2], -matrix[1][1], matrix[1][3],
matrix[2][0], matrix[2][2], -matrix[2][1], matrix[2][3],
matrix[3][0], matrix[3][2], -matrix[3][1], matrix[3][3]])
keyframe.weapon_mats.append(mat)
if i in keyframes[time].weap_relative_id:
keyframe.weap_relative_ids.append(keyframes[time].weap_relative_id[i])
else:
keyframe.weap_relative_ids.append(-1)
if i in keyframes[time].weap_relative_weight:
keyframe.weap_relative_weights.append(keyframes[time].weap_relative_weight[i])
else:
keyframe.weap_relative_weights.append(0.0)
for shape_key_data in keyframes[time].shape_keys:
shape_key = anm_types.ShapeKey()
shape_key.string = shape_key_data[0]
shape_key.weight = shape_key_data[1]
keyframe.shape_keys.append(shape_key)
for status_key_data in keyframes[time].status_keys:
status_key = anm_types.StatusKey()
status_key.string = status_key_data[0]
status_key.weight = status_key_data[1]
keyframe.status_keys.append(status_key)
for event_data in keyframes[time].events:
event = anm_types.Event()
event.string = event_data[0]
event.which_bone = event_data[1]
keyframe.events.append(event)
for ik_bone_data in keyframes[time].ik_bones:
#print("Adding an IK bone")
ik_bone = anm_types.IKBone()
ik_bone.string = ik_bone_data[0]
bone_id = ik_bone_data[1]
constraint = pose_bones["Bone_"+str(bone_id)].constraints[ik_bone.string]
path_length = constraint.chain_count
ik_bone.bone_path.append(bone_id)
index = bone_id
for i in range(path_length-1):
index = int(pose_bones["Bone_"+str(index)].parent.name.split("_")[-1])
ik_bone.bone_path.append(index)
ik_bone.bone_path.reverse()
# Questionable change here (.head to .bone.head_local)
vec = pose_bones["Bone_"+str(ik_bone.bone_path[0])].bone.head_local
ik_bone.start.fromlist([vec[0],vec[1],vec[2]])
# Questionable change here (.tail to .bone.tail_local)
vec = pose_bones["Bone_"+str(ik_bone.bone_path[-1])].bone.tail_local
ik_bone.end.fromlist([vec[0],vec[1],vec[2]])
keyframe.ik_bones.append(ik_bone)
data.keyframes.append(keyframe)
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action = 'DESELECT')
bpy.context.view_layer.objects.active = arm_obj
# Replaced
#bpy.context.view_layer.objects.active.select = True
arm_obj.select_set(True)
bpy.ops.object.delete()
bpy.context.view_layer.objects.active = old_arm_obj
action.user_clear()
#data.keyframes.sort(key=operator.attrgetter('time'))
#print(data.keyframes)
return data
def WriteFloat(file, val):
int_loader = array.array('f')
int_loader.append(val)
int_loader.tofile(file)
def WriteInt(file, val):
int_loader = Get4ByteIntArray()
int_loader.append(val)
int_loader.tofile(file)
def WriteBool(file, val):
bool_loader = array.array('B')
bool_loader.append(val)
bool_loader.tofile(file)
def WriteANM(filepath, data):
#print(data.keyframes[0])
file = open(filepath, "wb")
data.version = 10
num_keyframes = len(data.keyframes)
WriteInt(file, data.version)
WriteBool(file, False)
WriteBool(file, data.looping)
WriteInt(file, int(data.start))
WriteInt(file, int(data.end))
WriteInt(file, num_keyframes)
'''print("\nItem sizes:")
print(array.array('B').itemsize)
print(array.array('l').itemsize)
print(array.array('h').itemsize)
print(array.array('i').itemsize)
print(array.array('L').itemsize)
print(array.array('f').itemsize)'''
'''
print("\n")
print(data.version)
print(data.looping)
print(data.start)
print(data.end)
print(num_keyframes)'''
for i in range(num_keyframes):
keyframe = data.keyframes[i]
WriteInt(file, keyframe.time)
num_weights = len(keyframe.weights)
WriteInt(file, num_weights)
keyframe.weights.tofile(file)
num_bone_mats = len(keyframe.mats)
WriteInt(file, num_bone_mats)
for j in range(num_bone_mats):
keyframe.mats[j].tofile(file)
num_weapon_mats = len(keyframe.weapon_mats)
WriteInt(file, num_weapon_mats)
for j in range(num_weapon_mats):
keyframe.weapon_mats[j].tofile(file)
WriteInt(file,keyframe.weap_relative_ids[j])
WriteFloat(file,keyframe.weap_relative_weights[j])
if len(keyframe.mobility_mat) > 0:
WriteBool(file, True)
keyframe.mobility_mat.tofile(file)
else :
WriteBool(file, False)
num_events = len(keyframe.events)
WriteInt(file, num_events)
for event in keyframe.events:
WriteInt(file, event.which_bone)
string_array = array.array("B")
string_array.frombytes(event.string.encode("utf-8"))
WriteInt(file, len(string_array))
string_array.tofile(file)
num_ik_bones = len(keyframe.ik_bones)
WriteInt(file, num_ik_bones)
for ik_bone in keyframe.ik_bones:
ik_bone.start.tofile(file)
ik_bone.end.tofile(file)
WriteInt(file, len(ik_bone.bone_path))
ik_bone.bone_path.tofile(file)
string_array = array.array("B")
string_array.frombytes(ik_bone.string.encode("utf-8"))
WriteInt(file, len(string_array))
string_array.tofile(file)
num_shape_keys = len(keyframe.shape_keys)
WriteInt(file, num_shape_keys)
for shape_key in keyframe.shape_keys:
WriteFloat(file, shape_key.weight)
string_array = array.array("B")
string_array.frombytes(shape_key.string.encode("utf-8"))
WriteInt(file, len(string_array))
string_array.tofile(file)
num_status_keys = len(keyframe.status_keys)
WriteInt(file, num_status_keys)
for status_key in keyframe.status_keys:
WriteFloat(file, status_key.weight)
string_array = array.array("B")
string_array.frombytes(status_key.string.encode("utf-8"))
WriteInt(file, len(string_array))
string_array.tofile(file)
def Save(filepath):
data = GetAnimation(filepath)
if not data:
return
WriteANM(filepath, data)
# Helper class (Allows 'self.file')
from bpy_extras.io_utils import ExportHelper
class ANMExporter(bpy.types.Operator, ExportHelper):
'''Save Phoenix Bones armature'''
bl_idname = "export_mesh.anm"
bl_label = "Export ANM"
# Added to make the ExportHelper work
filename_ext = ".anm"
#filepath = StringProperty(name="File Path", description="Filepath used for exporting the ANM file", maxlen= 1024, default= "")
#filepath = bpy.props.StringProperty(subtype="FILEPATH ")
check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'})
def execute(self, context):
#print("file save path: ")
#print(self.filepath)
Save(self.properties.filepath)
return {'FINISHED'}
def invoke(self, context, event):
wm = bpy.types.WindowManager
wm.fileselect_add(self)
return {'RUNNING_MODAL'}
#Save("C:\\Users\\Nadhif\\Desktop\\hahaEXPORT.anm")
#Save("C:\\Users\\David\\Desktop\\export.anm")
#Save("Desktop/export.anm")
#BakeConstraints(bpy.context.view_layer.objects.active)
#Save("C:\\Users\\David\\Desktop\\WolfireSVN\\Project\\Data\\Animations\\r_bigdogswordattackover.anm")
|