-
Notifications
You must be signed in to change notification settings - Fork 18
/
blender_render.py
135 lines (95 loc) · 3.44 KB
/
blender_render.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
# Copyright 2020 by Patrik Jonell.
# All rights reserved.
# This file is part of the GENEA visualizer,
# and is released under the GPLv3 License. Please see the LICENSE
# file that should have been included as part of this package.
import bpy
from bpy import context
import os
import time
import tempfile
from pathlib import Path
import sys
argv = sys.argv
argv = argv[argv.index("--") + 1 :]
bvh_file_name = argv[0]
bpy.data.objects.remove(bpy.data.objects["Cube"], do_unlink=True)
bpy.ops.import_scene.fbx(
filepath="/queue/gesturer_mesh.fbx",
global_scale=1,
automatic_bone_orientation=True,
axis_up="Y",
)
scene = context.scene
fbx_model = scene.objects["Armature"]
camera = bpy.data.objects["Camera"]
camera.location = (0, -1.54, 0.42)
camera.rotation_euler = (1.57, 0.0, 0)
lamp = bpy.data.objects["Light"]
lamp.location = (0.0, -6, 0)
if not fbx_model.animation_data:
fbx_model.animation_data_create()
fbx_model.animation_data.action = None
mat = bpy.data.materials["Material"]
def fix_obj(parent_obj):
for obj in parent_obj.children:
fix_obj(obj)
parent_obj.rotation_euler.x = 0
if parent_obj.name in ["pCube0", "pCube1", "pCube2"]:
parent_obj.location.y = -13
if parent_obj.name == "pCube3":
parent_obj.location.y = -10
if parent_obj.name == "pCube5":
parent_obj.location.y = -9.5
if "materials" in dir(parent_obj.data):
if parent_obj.data.materials:
parent_obj.data.materials[0] = mat
else:
parent_obj.data.materials.append(mat)
fix_obj(fbx_model)
old_objs = set(scene.objects)
res = bpy.ops.import_anim.bvh(filepath=bvh_file_name, global_scale=0.01,)
# use_fps_scale=True, update_scene_fps=True, update_scene_duration=True,
(bvh_obj,) = set(context.scene.objects) - old_objs
for pb in fbx_model.pose.bones:
ct = pb.constraints.new("COPY_ROTATION")
ct.owner_space = "WORLD"
ct.target_space = "WORLD"
ct.name = pb.name
ct.target = bvh_obj
ct.subtarget = pb.name
action = bvh_obj.animation_data.action
total_frames = action.frame_range.y
f = action.frame_range.x
# add a keyframe to each frame of new rig
while f < total_frames:
scene.frame_set(f)
for pb in fbx_model.pose.bones:
m = fbx_model.convert_space(pose_bone=pb, matrix=pb.matrix, to_space="LOCAL")
if pb.rotation_mode == "QUATERNION":
pb.rotation_quaternion = m.to_quaternion()
pb.keyframe_insert("rotation_quaternion", frame=f)
else:
pb.rotation_euler = m.to_euler(pb.rotation_mode)
pb.keyframe_insert("rotation_euler", frame=f)
# pb.location = m.to_translation()
pb.keyframe_insert("location", frame=f)
f += 1
print(f"total_frames {total_frames}", flush=True)
bpy.context.scene.frame_end = total_frames
tmp_dir = Path(tempfile.mkdtemp()) / "video"
render = bpy.context.scene.render
render.filepath = str(tmp_dir)
# Set render engine (this one seems to be the fastest)
render.engine = "BLENDER_WORKBENCH"
# Set output format
render.image_settings.file_format = "FFMPEG"
render.ffmpeg.format = "MPEG4"
# Set the codec
render.ffmpeg.codec = "H264"
# Set the output resolution
render.resolution_x = int(os.environ["RENDER_RESOLUTION_X"])
render.resolution_y = int(os.environ["RENDER_RESOLUTION_Y"])
render.fps = int(os.environ["RENDER_FPS"])
bpy.ops.render.render(animation=True, write_still=False)
print("output_file", str(list(tmp_dir.parent.glob("*"))[0]), flush=True)