-
Notifications
You must be signed in to change notification settings - Fork 20
/
01_cornell_box.py
100 lines (91 loc) · 3.52 KB
/
01_cornell_box.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
import argparse
import numpy as np
from blendify import scene
from blendify.colors import UniformColors
from blendify.materials import PrincipledBSDFMaterial, GlossyBSDFMaterial
def main(args):
# Attach blender file with scene (walls, floor and ceiling)
scene.attach_blend("./assets/cornell_box.blend")
# Add camera to the scene
scene.set_perspective_camera(
args.resolution, fov_x=np.deg2rad(39.1), rotation=(-0.707, -0.707, 0.0, 0.0),
translation=[-0.278, -0.800, 0.273]
)
# Add light to the scene
scene.lights.add_area(
shape="square", size=5, strength=40, cast_shadows=False,
rotation=(-0.707, -0.707, 0.0, 0.0), translation=[-0.278, -1, 0.273]
)
# Fill up the scene with objects
# Add Sphere 1
sphere_1_material = PrincipledBSDFMaterial(
specular=0.5,
sheen=0.0,
sheen_tint=0.4,
ior=1.46,
transmission=1.0,
transmission_roughness=0.0,
alpha=0.7,
clearcoat=1.0,
clearcoat_roughness=0.0,
)
sphere_1_color = UniformColors((1.0, 153/255, 102/255))
sphere_1 = scene.renderables.add_sphere_nurbs(
0.08, sphere_1_material, sphere_1_color, translation=[-0.22, 0.05, 0.08]
)
# Add Sphere 2
sphere_2_material = PrincipledBSDFMaterial(
metallic=1.0,
specular=0.5,
roughness=0.07,
sheen_tint=0.4,
clearcoat=0.2,
clearcoat_roughness=0.4
)
sphere_2_color = UniformColors((1.0, 1.0, 1.0))
sphere_2 = scene.renderables.add_sphere_nurbs(
0.08, sphere_2_material, sphere_2_color, translation=[-0.12, 0.25, 0.08]
)
# Add Cylinder
cylinder_material = GlossyBSDFMaterial(
roughness=0.5
)
cylinder_color = UniformColors((102/255, 102/255, 1.0))
cylinder = scene.renderables.add_cylinder_mesh(
0.08, 0.3, cylinder_material, cylinder_color, translation=[-0.32, 0.25, 0.15]
)
# Add Circle
circle_material = PrincipledBSDFMaterial(
metallic=1.0,
specular=0.5,
roughness=0.05,
sheen_tint=0.8,
clearcoat=0.8,
clearcoat_roughness=0.1
)
circle_color = UniformColors((1.0, 1.0, 1.0))
circle = scene.renderables.add_circle_mesh(
0.17, circle_material, circle_color,
rotation=[0.720, 0.262, 0.604, -0.220], translation=[-0.43, 0.32, 0.18]
)
# Render the scene
scene.render(filepath=args.path, use_gpu=not args.cpu, samples=args.n_samples)
# Optionally save blend file with the scene
if args.output_blend is not None:
scene.export(args.output_blend)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Blendify example 01: Cornell Box.")
# Paths to output files
parser.add_argument("-p", "--path", type=str, default="./01_cornell_box.png",
help="Path to the resulting image")
parser.add_argument("-o", "--output-blend", type=str, default=None,
help="Path to the resulting blend file")
# Rendering parameters
parser.add_argument("-n", "--n-samples", default=256, type=int,
help="Number of paths to trace for each pixel in the render (default: 256)")
parser.add_argument("-res", "--resolution", default=(1024, 1024), nargs=2, type=int,
help="Rendering resolution, (default: (1024, 1024))")
parser.add_argument("--cpu", action="store_true",
help="Use CPU for rendering (by default GPU is used)")
arguments = parser.parse_args()
main(arguments)