-
Notifications
You must be signed in to change notification settings - Fork 16
/
hka_export.py
102 lines (73 loc) · 2.47 KB
/
hka_export.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
#!/usr/bin/env python
"""anim.bin exporter for blender 2.78
"""
import os
import bpy
from math import radians
from mathutils import Euler, Matrix, Quaternion, Vector
# import numpy as np
from io_anim_hkx.io.hka import hkaSkeleton, hkaAnimation, hkaPose, Transform
from io_anim_hkx.naming import get_bone_name_for_blender
def export_hkaAnimation(anim, skeleton):
#
# create bone map
#
# map pose_bone name to bone_idx
bone_indices = {}
nbones = len(skeleton.bones)
for i in range(nbones):
bone = skeleton.bones[i]
# blender naming convention
# io_scene_nifに合わせる
p_bone_name = get_bone_name_for_blender(bone.name)
bone_indices[p_bone_name] = i
def detect_armature():
found = None
for ob in bpy.context.selected_objects:
if ob.type == 'ARMATURE':
found = ob
break
return found
def export_pose():
arm_ob = detect_armature()
arm_ob.select = True
anim.numOriginalFrames = 1
anim.duration = 0.033333
del anim.pose[:]
pose = hkaPose()
anim.pose.append(pose)
pose.time = 0.0
for bone in skeleton.bones:
t = bone.local.copy()
pose.transforms.append(t)
for p_bone in arm_ob.pose.bones:
# bone mapに含まれないnameは無視する
if p_bone.name not in bone_indices:
continue
bone_i = bone_indices[p_bone.name]
bone = p_bone.bone # rest bone
if bone.parent:
m = bone.parent.matrix_local.inverted() * bone.matrix_local * p_bone.matrix_basis
else:
m = bone.matrix_local * p_bone.matrix_basis
location, rotation, scale = m.decompose()
t = pose.transforms[bone_i]
t.translation = location
t.rotation = rotation
t.scale = scale.z
export_pose()
# export_motion()
def export_hkafile(skeleton_file, anim_file):
skeleton = hkaSkeleton()
skeleton.load(skeleton_file)
anim = hkaAnimation()
export_hkaAnimation(anim, skeleton)
anim.save(anim_file)
if __name__ == "__main__":
from time import time
start_time = time()
skeleton_file = os.path.join(os.environ['HOME'], "resources/skeleton.bin")
anim_file = "anim.bin"
export_hkafile(skeleton_file, anim_file)
end_time = time()
print('bin export time:', end_time - start_time)