forked from mottosso/video2mocap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_maya.py
327 lines (255 loc) · 9.07 KB
/
export_maya.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
from __future__ import absolute_import
import os
import sys
from absl import flags
# ----------------------------------------------------------------------------
flags.DEFINE_string("json_path", None, "Path containing json data exported by HMR")
flags.DEFINE_string("output_dir", None, "Output dir for characters")
# ----------------------------------------------------------------------------
def bind(group_data, skeleton_data):
"""
Bind the group data to the skeleton data using constraints. Doing this
will not only take translation into account but also a proper rotation
which is needed for a decent result.
:param dict group_data:
:param dict skeleton_data:
"""
# point constraint hip joint between left and right
cmds.pointConstraint(
[group_data["l_hip"], group_data["r_hip"]],
group_data["hip"],
maintainOffset=False
)
# parent constraint spine joint between hip and neck
constraint = cmds.parentConstraint(
[group_data["hip"], group_data["neck"]],
group_data["spine"],
maintainOffset=False,
skipRotate="none"
)[0]
# set aliases to position spine closer to the hip
aliases = cmds.parentConstraint(
constraint,
query=True,
weightAliasList=True
)
cmds.setAttr("{}.{}".format(constraint, aliases[0]), 0.8)
cmds.setAttr("{}.{}".format(constraint, aliases[1]), 0.2)
# create point constraints
for key, group in group_data.iteritems():
joint = skeleton_data.get(key)
if not joint:
continue
cmds.pointConstraint(group, joint, maintainOffset=False)
# aim constraints
# body
cmds.aimConstraint(
group_data.get("neck"),
skeleton_data.get("hip"),
aimVector=[0, 1, 0],
upVector=[1, 0, 0],
worldUpVector=[0, 0, 1],
worldUpType="object",
worldUpObject=group_data.get("l_hip")
)
cmds.aimConstraint(
group_data.get("head"),
skeleton_data.get("neck"),
aimVector=[0, 1, 0],
upVector=[0, 0, 1],
worldUpVector=[0, 0, -1],
worldUpType="objectrotation",
worldUpObject=group_data.get("neck")
)
# limbs
for side, aimDirection in zip(["l", "r"], [1, -1]):
# leg
for leg1, leg2 in zip(LEG_HIERARCHY[:-1], LEG_HIERARCHY[1:]):
joint = skeleton_data.get("{}_{}".format(side, leg1))
target = group_data.get("{}_{}".format(side, leg2))
up_name = LEG_HIERARCHY[:]
up_name.remove(leg1)
up_name.remove(leg2)
up = group_data.get("{}_{}".format(side, up_name[0]))
cmds.aimConstraint(
target,
joint,
aimVector=[0, -1, 0],
upVector=[0, 0, -1],
worldUpVector=[0, 0, 1],
worldUpType="object",
worldUpObject=up
)
# arm
for arm1, arm2 in zip(ARM_HIERARCHY[:-1], ARM_HIERARCHY[1:]):
joint = skeleton_data.get("{}_{}".format(side, arm1))
target = group_data.get("{}_{}".format(side, arm2))
up_name = ARM_HIERARCHY[:]
up_name.remove(arm1)
up_name.remove(arm2)
up = group_data.get("{}_{}".format(side, up_name[0]))
cmds.aimConstraint(
target,
joint,
aimVector=[aimDirection, 0, 0],
upVector=[0, 0, 1],
worldUpVector=[0, 0, 1],
worldUpType="object",
worldUpObject=up
)
# need to do more testing to confidently bind the wrist, ankles and head.
# At the moment the results seem inaccurate.
# ----------------------------------------------------------------------------
def get_center_matrix(matrices):
"""
Get a matrix that can be used to transform other matrices to make sure on
the start frame the center point between the ankles is at 0, 0, 0 in world
space.
:param dict matrices:
:return: Center matrix
:rtype: OpenMaya.MMatrix
"""
# get ankle matrices
l_ankle = OpenMaya.MVector(matrices.get("l_ankle")[12:15])
r_ankle = OpenMaya.MVector(matrices.get("r_ankle")[12:15])
center = (l_ankle + r_ankle) * -0.5
# compile matrix
return OpenMaya.MMatrix(
[
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
center.x, center.y, center.z, 1.0
]
)
def get_scale_matrix():
"""
The scale matrix is used to scale the character to be 170 cm tall. The
uniform scale can be adjusted in maya to make it work for different sized
characters.
:return: Scale matrix
:rtype: OpenMaya.MMatrix
"""
scale = 170 / 1.5
return OpenMaya.MMatrix(
[
1.0 * scale, 0.0, 0.0, 0.0,
0.0, -1.0 * scale, 0.0, 0.0,
0.0, 0.0, -1.0 * scale, 0.0,
0.0, 0.0, 0.0, 1.0
]
)
# ----------------------------------------------------------------------------
def animate(group_data, skeleton_data, mocap_data):
"""
Loop over the mocap data and key frame all of the groups. After all of the
keys are set the skeleton itself will be baked and the groups and formerly
created constraints will removed so only the desired skeleton and hik are
left in the scene
:param dict group_data:
:param dict skeleton_data:
:param list mocap_data:
"""
# get center matrix
data = mocap.get_first_valid_frame(mocap_data)
center_matrix = get_center_matrix(data.get("matrices_3d"))
# scale an rotate matrix
scale_matrix = get_scale_matrix()
# bake groups
for i, frame in enumerate(mocap_data):
if not frame:
logger.debug("No Frame Data: {}".format(i + 1))
continue
for name, group in group_data.iteritems():
# get matrix
matrix = frame.get("matrices_3d").get(name)
if not matrix:
continue
matrix = OpenMaya.MMatrix(frame.get("matrices_3d").get(name))
# set matrix
cmds.currentTime(i + 1)
cmds.xform(
group,
worldSpace=True,
matrix=matrix * center_matrix * scale_matrix
)
cmds.setKeyframe(group)
# get attributes
attributes = [
"{}.{}{}".format(joint, attribute, channel)
for joint in skeleton_data.values()
for attribute in ["translate", "rotate", "scale"]
for channel in ["X", "Y", "Z"]
]
# get constraints
constraints = []
for joint in skeleton_data.values():
constraint = cmds.listConnections(joint, type="constraint") or []
constraints.extend(constraint)
# bake joints
cmds.bakeResults(
attributes,
t=(1, len(mocap_data)),
)
# cleanup
cmds.delete(group_data.values() + constraints)
# ----------------------------------------------------------------------------
def process(path, output_dir):
"""
Convert the stored mocap json file to animated maya human ik skeletons.
Each character will be exported as a separate maya file.
:param str path:
:param str output_dir:
"""
# load mocap data
mocap_data = io.read_json(path)
# extract character data
characters = mocap.extract_people_sequence(mocap_data)
# loop people
for i, character in enumerate(characters):
# open a new scene
cmds.file(newFile=True, force=True)
# extract frames
frames = character.get("frames")
# create groups
valid_frame = mocap.get_first_valid_frame(frames)
character_groups = mocap.create_groups(valid_frame)
# create skeleton
character_skeleton = skeleton.create_skeleton()
# create hik
hik.create_hik(character_skeleton)
# bind
bind(character_groups, character_skeleton)
# query character height
animate(character_groups, character_skeleton, frames)
# fix euler rotations
anim.applyEulerFilter(character_skeleton.values())
# save file
output_path = os.path.join(
output_dir,
"character_{0:02d}.ma".format(i+1)
)
cmds.file(rename=output_path)
cmds.file(save=True, type="mayaAscii")
# log save
logger.info("Saved Character: {}".format(output_path))
# ----------------------------------------------------------------------------
if __name__ == '__main__':
# initialize maya standalone
import maya.standalone
maya.standalone.initialize()
# add to path so the script imports the right utils
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
from maya import cmds, mel
from maya.api import OpenMaya
from utils import io, hik, anim, mocap, logging, skeleton
from utils.path import *
# convert data
config = flags.FLAGS
config(sys.argv)
# create logger
logger = logging.get_maya_logger(config.output_dir)
# process data
process(config.json_path, config.output_dir)