forked from yuki-koyama/blender-cli-rendering
-
Notifications
You must be signed in to change notification settings - Fork 1
/
05_composition.py
170 lines (131 loc) · 6.21 KB
/
05_composition.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# blender --background --python 05_composition.py --render-frame 1 -- </path/to/output/image> <resolution_percentage> <num_samples>
import bpy
import sys
import math
import os
working_dir_path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(working_dir_path)
import utils
def set_principled_node_as_rough_blue(principled_node: bpy.types.Node) -> None:
utils.set_principled_node(
principled_node=principled_node,
base_color=(0.1, 0.2, 0.6, 1.0),
metallic=0.5,
specular=0.5,
roughness=0.9,
)
def set_principled_node_as_ceramic(principled_node: bpy.types.Node) -> None:
utils.set_principled_node(
principled_node=principled_node,
base_color=(0.8, 0.8, 0.8, 1.0),
subsurface=0.1,
subsurface_color=(0.9, 0.9, 0.9, 1.0),
subsurface_radius=(1.0, 1.0, 1.0),
metallic=0.2,
specular=0.5,
roughness=0.0,
)
def set_principled_node_as_gold(principled_node: bpy.types.Node) -> None:
utils.set_principled_node(
principled_node=principled_node,
base_color=(1.00, 0.71, 0.22, 1.0),
metallic=1.0,
specular=0.5,
roughness=0.1,
)
def set_principled_node_as_glass(principled_node: bpy.types.Node) -> None:
utils.set_principled_node(principled_node=principled_node,
base_color=(0.95, 0.95, 0.95, 1.0),
metallic=0.0,
specular=0.5,
roughness=0.0,
clearcoat=0.5,
clearcoat_roughness=0.030,
ior=1.45,
transmission=0.98)
def set_scene_objects() -> bpy.types.Object:
left_object, center_object, right_object = utils.create_three_smooth_monkeys()
mat = utils.add_material("Material_Left", use_nodes=True, make_node_tree_empty=True)
nodes = mat.node_tree.nodes
links = mat.node_tree.links
output_node = nodes.new(type='ShaderNodeOutputMaterial')
principled_node = nodes.new(type='ShaderNodeBsdfPrincipled')
set_principled_node_as_glass(principled_node)
links.new(principled_node.outputs['BSDF'], output_node.inputs['Surface'])
left_object.data.materials.append(mat)
mat = utils.add_material("Material_Center", use_nodes=True, make_node_tree_empty=True)
nodes = mat.node_tree.nodes
links = mat.node_tree.links
output_node = nodes.new(type='ShaderNodeOutputMaterial')
principled_node = nodes.new(type='ShaderNodeBsdfPrincipled')
set_principled_node_as_gold(principled_node)
links.new(principled_node.outputs['BSDF'], output_node.inputs['Surface'])
center_object.data.materials.append(mat)
mat = utils.add_material("Material_Right", use_nodes=True, make_node_tree_empty=True)
nodes = mat.node_tree.nodes
links = mat.node_tree.links
output_node = nodes.new(type='ShaderNodeOutputMaterial')
principled_node = nodes.new(type='ShaderNodeBsdfPrincipled')
set_principled_node_as_rough_blue(principled_node)
links.new(principled_node.outputs['BSDF'], output_node.inputs['Surface'])
right_object.data.materials.append(mat)
current_object = utils.create_plane(size=20.0, name="Floor")
mat = utils.add_material("Material_Plane", use_nodes=True, make_node_tree_empty=True)
nodes = mat.node_tree.nodes
links = mat.node_tree.links
output_node = nodes.new(type='ShaderNodeOutputMaterial')
principled_node = nodes.new(type='ShaderNodeBsdfPrincipled')
set_principled_node_as_ceramic(principled_node)
links.new(principled_node.outputs['BSDF'], output_node.inputs['Surface'])
current_object.data.materials.append(mat)
bpy.ops.object.empty_add(location=(0.0, -0.75, 1.0))
focus_target = bpy.context.object
return focus_target
def build_scene_composition(scene: bpy.types.Scene) -> None:
scene.use_nodes = True
utils.clean_nodes(scene.node_tree.nodes)
render_layer_node = scene.node_tree.nodes.new(type="CompositorNodeRLayers")
vignette_node = utils.create_vignette_node(scene.node_tree)
vignette_node.inputs["Amount"].default_value = 0.70
lens_distortion_node = scene.node_tree.nodes.new(type="CompositorNodeLensdist")
lens_distortion_node.inputs["Distort"].default_value = -0.050
lens_distortion_node.inputs["Dispersion"].default_value = 0.080
color_correction_node = scene.node_tree.nodes.new(type="CompositorNodeColorCorrection")
color_correction_node.master_saturation = 1.10
color_correction_node.master_gain = 1.40
glare_node = scene.node_tree.nodes.new(type="CompositorNodeGlare")
glare_node.glare_type = 'GHOSTS'
glare_node.iterations = 2
glare_node.quality = 'HIGH'
composite_node = scene.node_tree.nodes.new(type="CompositorNodeComposite")
scene.node_tree.links.new(render_layer_node.outputs['Image'], vignette_node.inputs['Image'])
scene.node_tree.links.new(vignette_node.outputs['Image'], lens_distortion_node.inputs['Image'])
scene.node_tree.links.new(lens_distortion_node.outputs['Image'], color_correction_node.inputs['Image'])
scene.node_tree.links.new(color_correction_node.outputs['Image'], glare_node.inputs['Image'])
scene.node_tree.links.new(glare_node.outputs['Image'], composite_node.inputs['Image'])
utils.arrange_nodes(scene.node_tree)
# Args
output_file_path = bpy.path.relpath(str(sys.argv[sys.argv.index('--') + 1]))
resolution_percentage = int(sys.argv[sys.argv.index('--') + 2])
num_samples = int(sys.argv[sys.argv.index('--') + 3])
# Parameters
hdri_path = os.path.join(working_dir_path, "assets/HDRIs/green_point_park_2k.hdr")
# Scene Building
scene = bpy.data.scenes["Scene"]
world = scene.world
## Reset
utils.clean_objects()
## Suzannes
focus_target = set_scene_objects()
## Camera
bpy.ops.object.camera_add(location=(0.0, -16.0, 2.0))
camera_object = bpy.context.object
utils.add_track_to_constraint(camera_object, focus_target)
utils.set_camera_params(camera_object.data, focus_target, lens=85, fstop=0.5)
## Lights
utils.build_environment_texture_background(world, hdri_path)
## Composition
build_scene_composition(scene)
# Render Setting
utils.set_output_properties(scene, resolution_percentage, output_file_path)
utils.set_cycles_renderer(scene, camera_object, num_samples)