-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathanim2js.py
More file actions
141 lines (117 loc) · 4.58 KB
/
anim2js.py
File metadata and controls
141 lines (117 loc) · 4.58 KB
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
# coding=utf-8
from __future__ import print_function
import sys
import json
import ruamel.yaml
from ruamel.yaml.error import YAMLError
import uuid
import optparse
yaml = ruamel.yaml.YAML()
class ClipTrack:
def __init__(self):
self.name = ""
self.times = []
self.values = []
self.type = ""
@staticmethod
def get_three_js_track_type(curve_type):
return {
"scale": "vector",
"quaternion": "quaternion",
"position": "vector",
}.get(curve_type, "")
def parse_unity_curve(self, curve, curve_type):
self.type = self.get_three_js_track_type(curve_type)
self.name = curve['path'].split('/')[-1] + '.' + curve_type
for cc in curve['curve']['m_Curve']:
self.times.append(cc['time'])
if curve_type == "quaternion":
self.values.append(cc['value']['x'])
self.values.append(-cc['value']['y'])
self.values.append(-cc['value']['z'])
self.values.append(cc['value']['w'])
elif curve_type == "position":
self.values.append(-cc['value']['x'])
self.values.append(cc['value']['y'])
self.values.append(cc['value']['z'])
else:
self.values.append(cc['value']['x'])
self.values.append(cc['value']['y'])
self.values.append(cc['value']['z'])
return self
def to_dict(self):
return {
"name": self.name,
"times": self.times,
"values": self.values,
"type": self.type
}
class AnimationClip:
def __init__(self):
self.name = ""
self.duration = 0
self.tracks = []
self.uuid = str(uuid.uuid1()).upper()
@staticmethod
def get_safe_name(raw_name):
valid = set(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_")
return filter(lambda x: x in valid, raw_name)
def parse_unity_anim(self, anim_data):
self.duration = anim_data['AnimationClip']['m_AnimationClipSettings']['m_StopTime']\
- anim_data['AnimationClip']['m_AnimationClipSettings']['m_StartTime']
self.name = self.get_safe_name(anim_data['AnimationClip']['m_Name'])
for curve in anim_data['AnimationClip']['m_ScaleCurves']:
self.tracks.append(ClipTrack().parse_unity_curve(curve, "scale"))
for curve in anim_data['AnimationClip']['m_RotationCurves']:
self.tracks.append(
ClipTrack().parse_unity_curve(curve, "quaternion"))
for curve in anim_data['AnimationClip']['m_PositionCurves']:
self.tracks.append(
ClipTrack().parse_unity_curve(curve, "position"))
return self
def to_dict(self):
tracks = []
for tr in self.tracks:
tracks.append(tr.to_dict())
return {
"name": self.name,
"duration": self.duration,
"uuid": self.uuid,
"tracks": tracks,
}
def load_anim(anim_fn):
with open(anim_fn, 'r') as stream:
try:
datamap = yaml.load(stream)
datamap = dict(datamap)
except YAMLError as exc:
print(exc)
exit(-1)
return datamap
def main():
hstr = '%prog [options] AnimationClipFile1.anim [AnimationClipFile2.anim [...]]'
parser = optparse.OptionParser(
hstr, description='A simple tool to convert Unity .anim AnimationClips to Three.JS AnimationClips. After converted, you can use them like \"THREE.AnimationClip.parse(anim_AnimationClipName())\" in your JS script.')
parser.add_option('-o', '--outfile',
action="store", dest="out_file",
help="where to save the generated .js", default="animations.js")
parser.add_option('-p', '--prefix',
action="store", dest="prefix",
help="prefix of JS function to get the AnimationClip, default value is \"anim_\"", default="anim_")
options, args = parser.parse_args()
if not args:
print(parser.format_help())
exit(0)
f = open(options.out_file, 'w')
for in_file in args:
print("[*] Parsing {}".format(in_file), end=' -> ')
anim_data = load_anim(in_file)
clip = AnimationClip().parse_unity_anim(anim_data)
f.write("function " + options.prefix + clip.name + "() {return")
f.write(json.dumps(clip.to_dict()).replace(' ', ''))
f.write('}\n')
print(options.prefix + clip.name + "()")
f.close()
if __name__ == '__main__':
main()