-
Notifications
You must be signed in to change notification settings - Fork 7
/
bvh.py
284 lines (226 loc) · 9.16 KB
/
bvh.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
import numpy as np
import re
from transforms3d.euler import euler2mat, mat2euler
class BvhJoint:
def __init__(self, name, parent):
self.name = name
self.parent = parent
self.offset = np.zeros(3)
self.channels = []
self.children = []
def add_child(self, child):
self.children.append(child)
def __repr__(self):
return self.name
def position_animated(self):
return any([x.endswith('position') for x in self.channels])
def rotation_animated(self):
return any([x.endswith('rotation') for x in self.channels])
class Bvh:
def __init__(self):
self.joints = {}
self.root = None
self.keyframes = None
self.frames = 0
self.fps = 0
def _parse_hierarchy(self, text):
lines = re.split('\\s*\\n+\\s*', text)
joint_stack = []
for line in lines:
words = re.split('\\s+', line)
instruction = words[0]
if instruction == "JOINT" or instruction == "ROOT":
parent = joint_stack[-1] if instruction == "JOINT" else None
joint = BvhJoint(words[1], parent)
self.joints[joint.name] = joint
if parent:
parent.add_child(joint)
joint_stack.append(joint)
if instruction == "ROOT":
self.root = joint
elif instruction == "CHANNELS":
for i in range(2, len(words)):
joint_stack[-1].channels.append(words[i])
elif instruction == "OFFSET":
for i in range(1, len(words)):
joint_stack[-1].offset[i - 1] = float(words[i])
elif instruction == "End":
joint = BvhJoint(joint_stack[-1].name + "_end", joint_stack[-1])
joint_stack[-1].add_child(joint)
joint_stack.append(joint)
self.joints[joint.name] = joint
elif instruction == '}':
joint_stack.pop()
def _add_pose_recursive(self, joint, offset, poses):
pose = joint.offset + offset
poses.append(pose)
for c in joint.children:
self._add_pose_recursive(c, pose, poses)
def plot_hierarchy(self):
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D
poses = []
self._add_pose_recursive(self.root, np.zeros(3), poses)
pos = np.array(poses)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(pos[:, 0], pos[:, 2], pos[:, 1])
ax.set_xlim(-30, 30)
ax.set_ylim(-30, 30)
ax.set_zlim(-30, 30)
plt.show()
def parse_motion(self, text):
lines = re.split('\\s*\\n+\\s*', text)
frame = 0
for line in lines:
if line == '':
continue
words = re.split('\\s+', line)
if line.startswith("Frame Time:"):
self.fps = round(1 / float(words[2]))
continue
if line.startswith("Frames:"):
self.frames = int(words[1])
continue
if self.keyframes is None:
self.keyframes = np.empty((self.frames, len(words)), dtype=np.float32)
for angle_index in range(len(words)):
self.keyframes[frame, angle_index] = float(words[angle_index])
frame += 1
def parse_string(self, text):
hierarchy, motion = text.split("MOTION")
self._parse_hierarchy(hierarchy)
self.parse_motion(motion)
def _extract_rotation(self, frame_pose, index_offset, joint):
local_rotation = np.zeros(3)
for channel in joint.channels:
if channel.endswith("position"):
continue
if channel == "Xrotation":
local_rotation[0] = frame_pose[index_offset]
elif channel == "Yrotation":
local_rotation[1] = frame_pose[index_offset]
elif channel == "Zrotation":
local_rotation[2] = frame_pose[index_offset]
else:
raise Exception(f"Unknown channel {channel}")
index_offset += 1
local_rotation = np.deg2rad(local_rotation)
M_rotation = np.eye(3)
for channel in joint.channels:
if channel.endswith("position"):
continue
if channel == "Xrotation":
euler_rot = np.array([local_rotation[0], 0., 0.])
elif channel == "Yrotation":
euler_rot = np.array([0., local_rotation[1], 0.])
elif channel == "Zrotation":
euler_rot = np.array([0., 0., local_rotation[2]])
else:
raise Exception(f"Unknown channel {channel}")
M_channel = euler2mat(*euler_rot)
M_rotation = M_rotation.dot(M_channel)
return M_rotation, index_offset
def _extract_position(self, joint, frame_pose, index_offset):
offset_position = np.zeros(3)
for channel in joint.channels:
if channel.endswith("rotation"):
continue
if channel == "Xposition":
offset_position[0] = frame_pose[index_offset]
elif channel == "Yposition":
offset_position[1] = frame_pose[index_offset]
elif channel == "Zposition":
offset_position[2] = frame_pose[index_offset]
else:
raise Exception(f"Unknown channel {channel}")
index_offset += 1
return offset_position, index_offset
def _recursive_apply_frame(self, joint, frame_pose, index_offset, p, r, M_parent, p_parent):
if joint.position_animated():
offset_position, index_offset = self._extract_position(joint, frame_pose, index_offset)
else:
offset_position = np.zeros(3)
if len(joint.channels) == 0:
joint_index = list(self.joints.values()).index(joint)
p[joint_index] = p_parent + M_parent.dot(joint.offset)
r[joint_index] = mat2euler(M_parent)
return index_offset
if joint.rotation_animated():
M_rotation, index_offset = self._extract_rotation(frame_pose, index_offset, joint)
else:
M_rotation = np.eye(3)
M = M_parent.dot(M_rotation)
position = p_parent + M_parent.dot(joint.offset) + offset_position
rotation = np.rad2deg(mat2euler(M))
joint_index = list(self.joints.values()).index(joint)
p[joint_index] = position
r[joint_index] = rotation
for c in joint.children:
index_offset = self._recursive_apply_frame(c, frame_pose, index_offset, p, r, M, position)
return index_offset
def frame_pose(self, frame):
p = np.empty((len(self.joints), 3))
r = np.empty((len(self.joints), 3))
frame_pose = self.keyframes[frame]
M_parent = np.zeros((3, 3))
M_parent[0, 0] = 1
M_parent[1, 1] = 1
M_parent[2, 2] = 1
self._recursive_apply_frame(self.root, frame_pose, 0, p, r, M_parent, np.zeros(3))
return p, r
def all_frame_poses(self):
p = np.empty((self.frames, len(self.joints), 3))
r = np.empty((self.frames, len(self.joints), 3))
for frame in range(len(self.keyframes)):
p[frame], r[frame] = self.frame_pose(frame)
return p, r
def _plot_pose(self, p, r, fig=None, ax=None):
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D
if fig is None:
fig = plt.figure()
if ax is None:
ax = fig.add_subplot(111, projection='3d')
ax.cla()
ax.scatter(p[:, 0], p[:, 2], p[:, 1])
ax.set_xlim(-30, 30)
ax.set_ylim(-30, 30)
ax.set_zlim(-1, 59)
plt.draw()
plt.pause(0.001)
def plot_frame(self, frame, fig=None, ax=None):
p, r = self.frame_pose(frame)
self._plot_pose(p, r, fig, ax)
def joint_names(self):
return self.joints.keys()
def parse_file(self, path):
with open(path, 'r') as f:
self.parse_string(f.read())
def plot_all_frames(self):
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for i in range(self.frames):
self.plot_frame(i, fig, ax)
def __repr__(self):
return f"BVH {len(self.joints.keys())} joints, {self.frames} frames"
if __name__ == '__main__':
# create Bvh parser
anim = Bvh()
# parser file
anim.parse_file("example.bvh")
# draw the skeleton in T-pose
anim.plot_hierarchy()
# extract single frame pose: axis0=joint, axis1=positionXYZ/rotationXYZ
p, r = anim.frame_pose(0)
# extract all poses: axis0=frame, axis1=joint, axis2=positionXYZ/rotationXYZ
all_p, all_r = anim.all_frame_poses()
# print all joints, their positions and orientations
for _p, _r, _j in zip(p, r, anim.joint_names()):
print(f"{_j}: p={_p}, r={_r}")
# draw the skeleton for the given frame
anim.plot_frame(22)
# show full animation
anim.plot_all_frames()