File size: 2,216 Bytes
578b71c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
import sys

import imageio.v2 as imageio

sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from toolbox import SceneHandler, rgba_to_rgb
from toolbox.camera import get_camera_positions_on_sphere

scener = SceneHandler()

# Initialize the render engine
scener.init_render_engine("BLENDER_EEVEE")

# Initialize the camera
cam_default = scener.init_camera()

# Import the object, preprocess it and normalize the scene into [-1, 1]
scener.import_object(
    "vertex_colored",
    "assets/objects/vertex_colored.obj",
    # vertex_color=(0.52941, 0.80784, 0.92157),
)
scener.preprocess_objs()
scener.normalize_scene(range=1.0)

# Set the environment light
# scener.set_env_light(env_light=1.0)
scener.set_env_light(env_path="assets/env_textures/brown_photostudio_02_1k.exr")

# Prepare camera layout and add to the scene
distance = 1.5
cam_positions, cam_mats, eles, azis = get_camera_positions_on_sphere(
    center=(0, 0, 0), radius=distance, elevations=[30], num_per_layer=120
)
for camera_matrix in cam_mats:
    scener.add_camera(camera_matrix)

# Set rendering output and render
output_dir = "outputs"
width, height = 512, 512
scener.set_output(
    output_dir=output_dir,
    width=width,
    height=height,
    output_types=["color"],
)
scener.render()

# Prepare and save meta info
fov_x = cam_default.data.angle_x
meta_info = {
    "distance": distance,
    "fov_x": fov_x,
    "width": 512,
    "height": 512,
    "locations": [],
}
for i in range(len(cam_positions)):
    index = "{0:04d}".format(i)
    meta_info["locations"].append(
        {
            "index": index,
            "elevation": eles[i],
            "azimuth": azis[i],
            "transform_matrix": cam_mats[i].tolist(),
        }
    )
with open(os.path.join(output_dir, "meta.json"), "w") as f:
    json.dump(meta_info, f, indent=4)

# Save rendered color images to video
rgb_images = []
for f in sorted(os.listdir(output_dir)):
    if f.startswith("render_"):
        img = imageio.imread(os.path.join(output_dir, f))
        rgb_images.append(rgba_to_rgb(img))
video_path = os.path.join(output_dir, "render.mp4")
imageio.mimsave(video_path, rgb_images, fps=24)
print(f"Video saved to {video_path}")