|
a |
|
b/app/BVHAnimation.py |
|
|
1 |
from matplotlib import pyplot as plt |
|
|
2 |
from matplotlib.widgets import Slider |
|
|
3 |
import numpy as np |
|
|
4 |
|
|
|
5 |
from resources.pymo.pymo.preprocessing import MocapParameterizer |
|
|
6 |
|
|
|
7 |
|
|
|
8 |
class BVHAnimation: |
|
|
9 |
def __init__(self): |
|
|
10 |
self.bvh_data = None |
|
|
11 |
|
|
|
12 |
def animate(self): |
|
|
13 |
BVHAnimation.init_plot(BVHAnimation.positions(self.bvh_data)[0]) |
|
|
14 |
|
|
|
15 |
@staticmethod |
|
|
16 |
def positions(bvh_data): |
|
|
17 |
# convert rotations to positions for each joint |
|
|
18 |
mp = MocapParameterizer('position') |
|
|
19 |
return mp.fit_transform([bvh_data]) |
|
|
20 |
|
|
|
21 |
@staticmethod |
|
|
22 |
def init_plot(positions): |
|
|
23 |
fig = plt.figure(figsize=(8, 8)) |
|
|
24 |
ax = fig.add_subplot(111, projection='3d') |
|
|
25 |
|
|
|
26 |
axcolor = 'lightgoldenrodyellow' |
|
|
27 |
axframe = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor) |
|
|
28 |
|
|
|
29 |
bvh_number_frames = positions.values.shape[0] |
|
|
30 |
sframe = Slider(axframe, 'Frame', 1, bvh_number_frames, valinit=1, valstep=1) |
|
|
31 |
|
|
|
32 |
joints_to_draw = positions.skeleton.keys() |
|
|
33 |
lines = [] |
|
|
34 |
points = [] |
|
|
35 |
|
|
|
36 |
df = positions.values |
|
|
37 |
|
|
|
38 |
for joint in joints_to_draw: |
|
|
39 |
parent_x = df['%s_Xposition' % joint][0] |
|
|
40 |
parent_y = df['%s_Yposition' % joint][0] |
|
|
41 |
parent_z = df['%s_Zposition' % joint][0] |
|
|
42 |
# ^ In mocaps, Y is the up-right axis |
|
|
43 |
|
|
|
44 |
points.append(ax.plot([parent_x], |
|
|
45 |
[parent_y], |
|
|
46 |
[parent_z], |
|
|
47 |
linestyle="", c='b', marker='o')) |
|
|
48 |
|
|
|
49 |
children_to_draw = [c for c in positions.skeleton[joint]['children'] if c in joints_to_draw] |
|
|
50 |
|
|
|
51 |
for c in children_to_draw: |
|
|
52 |
child_x = df['%s_Xposition' % c][0] |
|
|
53 |
child_y = df['%s_Yposition' % c][0] |
|
|
54 |
child_z = df['%s_Zposition' % c][0] |
|
|
55 |
# ^ In mocaps, Y is the up-right axis |
|
|
56 |
|
|
|
57 |
lines.append( |
|
|
58 |
ax.plot([parent_x, child_x], [parent_y, child_y], [parent_z, child_z], 'k-', lw=2, c='black')) |
|
|
59 |
|
|
|
60 |
def update(_): |
|
|
61 |
"""Update function for the slider""" |
|
|
62 |
frame = int(sframe.val) - 1 |
|
|
63 |
index1 = 0 |
|
|
64 |
index2 = 0 |
|
|
65 |
|
|
|
66 |
for up_joint in joints_to_draw: |
|
|
67 |
up_parent_x = df['%s_Xposition' % up_joint][frame] |
|
|
68 |
up_parent_y = df['%s_Yposition' % up_joint][frame] |
|
|
69 |
up_parent_z = df['%s_Zposition' % up_joint][frame] |
|
|
70 |
# ^ In mocaps, Y is the up-right axis |
|
|
71 |
|
|
|
72 |
points[index1][0].set_data(np.array([up_parent_x]), np.array([up_parent_y])) |
|
|
73 |
points[index1][0].set_3d_properties([up_parent_z]) |
|
|
74 |
index1 += 1 |
|
|
75 |
|
|
|
76 |
up_children_to_draw = [c for c in positions.skeleton[up_joint]['children'] if c in joints_to_draw] |
|
|
77 |
|
|
|
78 |
for c in up_children_to_draw: |
|
|
79 |
up_child_x = df['%s_Xposition' % c][frame] |
|
|
80 |
up_child_y = df['%s_Yposition' % c][frame] |
|
|
81 |
up_child_z = df['%s_Zposition' % c][frame] |
|
|
82 |
# ^ In mocaps, Y is the up-right axis |
|
|
83 |
|
|
|
84 |
lines[index2][0].set_data(np.array([[up_parent_x, up_child_x], [up_parent_y, up_child_y]])) |
|
|
85 |
lines[index2][0].set_3d_properties([up_parent_z, up_child_z]) |
|
|
86 |
index2 += 1 |
|
|
87 |
|
|
|
88 |
fig.canvas.draw_idle() |
|
|
89 |
|
|
|
90 |
# update the plot when changing the slider value |
|
|
91 |
sframe.on_changed(update) |
|
|
92 |
|
|
|
93 |
# ax.set_xlim3d([-200.0, 200.0]) |
|
|
94 |
ax.set_xlabel('X') |
|
|
95 |
|
|
|
96 |
# ax.set_ylim3d([-200.0, 200.0]) |
|
|
97 |
ax.set_ylabel('Y') |
|
|
98 |
|
|
|
99 |
# ax.set_zlim3d([-200.0, 200.0]) |
|
|
100 |
ax.set_zlabel('Z') |
|
|
101 |
|
|
|
102 |
ax.view_init(elev=135, azim=-90) |
|
|
103 |
|
|
|
104 |
plt.show() |
|
|
105 |
|
|
|
106 |
|
|
|
107 |
bvh_animation = BVHAnimation() |