-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathapply_animation.py
62 lines (52 loc) · 2.02 KB
/
apply_animation.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
import bpy
import pickle
import numpy as np
import tqdm
from mathutils import Matrix
import math
INPUT_FILE = 'tmp\\bone_animation_data.pkl'
with open(INPUT_FILE, 'rb') as f:
data = pickle.load(f)
fov = data['fov']
frame_rate = data['frame_rate']
bone_names = data['bone_names']
bone_euler_sequence = data['bone_euler_sequence']
location_sequence = data['location_sequence']
scale = data['scale']
all_bone_names = data['all_bone_names']
root = 'pelvis'
bpy.data.objects['Camera'].location = (0, 0, 0)
bpy.data.objects['Camera'].rotation_euler = (math.pi / 2., 0, 0)
bpy.data.objects['Camera'].data.angle = fov
# set frame rate
bpy.context.scene.render.fps = int(frame_rate)
bpy.context.scene.frame_start = 0
bpy.context.scene.frame_end = len(bone_euler_sequence)
skeleton_objs = list(filter(lambda o: o.type == 'ARMATURE', bpy.data.objects))
assert len(skeleton_objs) == 1, "There should be only one skeleton object"
skeleton = skeleton_objs[0]
skeleton.location = (0, 0, 0)
skeleton.rotation_euler = (-math.pi / 2, 0, 0)
skeleton.scale = (scale, scale, scale)
# apply animation to skeleton
for i in range(len(bone_euler_sequence)):
for j, b in enumerate(bone_names):
bone = skeleton.pose.bones[b]
bone.rotation_mode = 'YXZ'
bone.rotation_euler = bone_euler_sequence[i][j].tolist()
bone.keyframe_insert(data_path='rotation_euler', frame=i)
# global location
x, y, z = location_sequence[i].tolist()
skeleton.location = x, z, -y
skeleton.keyframe_insert(data_path='location', frame=i)
# # add keypoints animation
# # create a sphere for each keypoint
# bpy.ops.mesh.primitive_uv_sphere_add(radius=0.005, location=(0, 0, 0))
# sphere = bpy.context.object
# for j, k in enumerate(keypoints_names):
# anchor = bpy.data.objects.new(k, sphere.data)
# bpy.context.scene.collection.objects.link(anchor)
# for i, (kpts, visib) in enumerate(keypoints):
# if visib[j]:
# anchor.location = kpts[j, :].tolist()
# anchor.keyframe_insert(data_path='location', frame=i)