-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathblender_optix_denoiser.py
301 lines (245 loc) · 11.5 KB
/
blender_optix_denoiser.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
bl_info = {
'name': 'OptiX denoising for Blender 2.80.0',
'version': (1, 1, 1),
'blender': (2, 80, 0),
'category': 'Render'
}
import ntpath
import os
import subprocess
import numpy
import bpy
import bpy.utils.previews
custom_icons = None
script_path = None
def retrieve_all_paths():
global script_path
script_path = os.path.abspath(__file__)
def load_icons():
global custom_icons
custom_icons = bpy.utils.previews.new()
icons_dir = os.path.join(os.path.dirname(script_path), "icons")
print("Icon dir: " + icons_dir)
custom_icons.load("optix_icon", os.path.join(icons_dir, "optix_logo.png"), 'IMAGE')
print("OptiX logo path:" + os.path.join(icons_dir, "optix_logo.png"))
def denoise_animation_frame(noisy_frame_path = ""):
denoiser_path = os.path.abspath(os.path.join(os.path.dirname(script_path), "Denosier_v2.1")) + "\Denoiser.exe"
file_name = os.path.basename(noisy_frame_path)
file_directory = noisy_frame_path[:-len(file_name)]
denoised_frame_path = file_directory + "DENOISED_" + file_name
print(denoised_frame_path)
args = [denoiser_path, '-i', noisy_frame_path, "-o", denoised_frame_path]
subprocess.call(args)
print("Denoised image " + noisy_frame_path)
return [denoised_frame_path, file_name]
#
# SHOWS A MESSAGE BOX UNDER THE CURSOR.
#
def show_message_box(message = "", title = "OptiX Denoising", icon = 'INFO'):
def draw(self, context):
self.layout.label(text=message)
bpy.context.window_manager.popup_menu(draw, title = title, icon = icon)
#
# OPERATOR TO DENOISE AN ALREADY **RENDERED** IMAGE.
#
class denoise_render_result_operator(bpy.types.Operator):
bl_idname = 'optix_denoising.denoise_render_result'
bl_label = 'Denoise Render Result'
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
denoiser_path = os.path.abspath(os.path.join(os.path.dirname(script_path), "Denosier_v2.1")) + "\Denoiser.exe"
#print(denoiser_path)
denoising_temp_image_path = os.path.abspath(os.path.dirname(script_path)) + "\denoising_temp.png"
#print(denoising_temp_image_path)
denoised_temp_image_path = os.path.abspath(os.path.dirname(script_path)) + "\denoised_temp.png"
#print(denoised_temp_image_path)
try:
if bpy.data.images["Render Result"] == None:
show_message_box("There is no render result to denoise. Either render manually and then denoise or use the \"Render and denoise\" button.", "OptiX Denoising", 'ERROR')
return {"FINISHED"}
except:
show_message_box("There is no render result to denoise. Either render manually and then denoise or use the \"Render and denoise\" button.", "OptiX Denoising", 'ERROR')
return {"FINISHED"}
bpy.data.images["Render Result"].save_render(denoising_temp_image_path)
args = [denoiser_path, '-i', denoising_temp_image_path, "-o", denoised_temp_image_path]
subprocess.call(args)
print("DENOISED!")
denoised_img = bpy.ops.image.open(filepath=denoised_temp_image_path)
for area in bpy.context.screen.areas :
if area.type == 'IMAGE_EDITOR':
for img in bpy.data.images:
if img.name == "denoised_temp.png":
area.spaces.active.image = img
show_message_box("Successfully denoised the image!")
return {"FINISHED"}
#
# OPERATOR TO RENDER AND THEN DENOISE THE RENDERED IMAGE.
#
class render_and_denoise_operator(bpy.types.Operator):
bl_idname = 'optix_denoising.render_and_denoise_operator'
bl_label = 'Render And Denoise'
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
# Show an image viewer
image_viewer_area = None
# First try to find an already open image viewer
for area in bpy.context.screen.areas :
if area.type == 'IMAGE_EDITOR':
image_viewer_area = area
break
# If there is not, try to find a 3D view and change its type to an image viewer
if image_viewer_area == None:
for area in bpy.context.screen.areas :
if area.type == 'VIEW_3D':
area.type = 'IMAGE_EDITOR'
image_viewer_area = area
break
# If there is neither an open image viewer nor a 3D view, then switch the current area to the image editor
if image_viewer_area == None:
bpy.context.area.type = 'IMAGE_EDITOR'
image_viewer_area = bpy.context.area
# First render
bpy.ops.render.render()
# Then denoise
denoiser_path = os.path.abspath(os.path.join(os.path.dirname(script_path), "Denosier_v2.1")) + "\Denoiser.exe"
#print(denoiser_path)
denoising_temp_image_path = os.path.abspath(os.path.dirname(script_path)) + "\denoising_temp.png"
#print(denoising_temp_image_path)
denoised_temp_image_path = os.path.abspath(os.path.dirname(script_path)) + "\denoised_temp.png"
#print(denoised_temp_image_path)
bpy.data.images["Render Result"].save_render(denoising_temp_image_path)
args = [denoiser_path, '-i', denoising_temp_image_path, "-o", denoised_temp_image_path]
subprocess.call(args)
print("DENOISED!")
denoised_img = bpy.ops.image.open(filepath=denoised_temp_image_path)
for img in bpy.data.images:
if img.name == "denoised_temp.png":
image_viewer_area.spaces.active.image = img
show_message_box("Successfully rendered and denoised the image!")
return {"FINISHED"}
#
# OPERATOR TO RENDER THE FULL ANIMATION AND THEN DENOISE IT.
#
class render_and_denoise_animation_operator(bpy.types.Operator):
bl_idname = 'optix_denoising.render_and_denoise_animation_operator'
bl_label = 'Render And Denoise Animation'
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
# Check if the animation will be saved. If not, cancel, for access to the file will be needed to denoise
if bpy.context.scene.render.filepath == "/tmp\\" or bpy.context.scene.render.filepath == None or bpy.context.scene.render.filepath == "":
show_message_box("You must specify where to save the animation for the denoising to work.", "OptiX Denoising", 'ERROR')
return {"FINISHED"}
# Show an image viewer
image_viewer_area = None
# First try to find an already open image viewer
for area in bpy.context.screen.areas:
if area.type == 'IMAGE_EDITOR':
image_viewer_area = area
break
# If there is not, try to find a 3D view and change its type to an image viewer
if image_viewer_area == None:
for area in bpy.context.screen.areas :
if area.type == 'VIEW_3D':
area.type = 'IMAGE_EDITOR'
image_viewer_area = area
break
# If there is neither an open image viewer nor a 3D view, then switch the current area to the image editor
if image_viewer_area == None:
bpy.context.area.type = 'IMAGE_EDITOR'
image_viewer_area = bpy.context.area
# Render the animation
bpy.ops.render.render(animation=True)
for img_idx in range(bpy.context.scene.frame_start, bpy.context.scene.frame_end + 1):
frame_path = bpy.context.scene.render.frame_path(frame=img_idx)
print("Denoising \"" + frame_path + "\"...")
denoise_animation_frame(frame_path)
show_message_box("Successfully rendered and denoised the animation!")
return {"FINISHED"}
#
# OPERATOR TO DENOISE A FULL, RENDERED ANIMATION.
#
class denoise_rendered_animation_operator(bpy.types.Operator):
bl_idname = 'optix_denoising.denoise_rendered_animation_operator'
bl_label = 'Denoise Rendered Animation'
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
# Check if the animation has been saved. If not, cancel, for access to the file will be needed to denoise
if bpy.context.scene.render.filepath == "/tmp\\" or bpy.context.scene.render.filepath == None or bpy.context.scene.render.filepath == "":
show_message_box("You must specify the animation output for the denoising to work.", "OptiX Denoising", 'ERROR')
return {"FINISHED"}
# Denoise the animation
for img_idx in range(bpy.context.scene.frame_start, bpy.context.scene.frame_end + 1):
frame_path = bpy.context.scene.render.frame_path(frame=img_idx)
denoised_img_path_and_filename = denoise_animation_frame(frame_path)
# end for
show_message_box("Successfully denoised the rendered animation!")
return {"FINISHED"}
#
# ADDON PANEL THAT DISPLAYS THE OPTIX DENOISING RELATED BUTTONS.
#
class optix_denoising_panel(bpy.types.Panel):
bl_idname = "optix_denoising.renderingpanel"
bl_label = "OptiX Denoising"
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "render"
def draw_header(self, context):
self.layout.label(icon_value=custom_icons["optix_icon"].icon_id)
def draw(self, context):
layout = self.layout
row1 = layout.row()
row1.operator("optix_denoising.denoise_render_result", text="Denoise render result")
row2 = layout.row()
row2.operator("optix_denoising.render_and_denoise_operator", text="Render and denoise")
row3 = layout.row()
row3.operator("optix_denoising.render_and_denoise_animation_operator", text="Render animation and denoise")
row4 = layout.row()
row4.operator("optix_denoising.denoise_rendered_animation_operator", text="Denoise rendered animation")
def register() :
retrieve_all_paths()
load_icons()
try:
bpy.utils.register_class(denoise_render_result_operator)
except:
print("Unable to register the class \"denoise_render_result_operator\"")
try:
bpy.utils.register_class(render_and_denoise_operator)
except:
print("Unable to register the class \"render_and_denoise_operator\"")
try:
bpy.utils.register_class(denoise_rendered_animation_operator)
except:
print("Unable to register the class \"denoise_rendered_animation_operator\"")
try:
bpy.utils.register_class(render_and_denoise_animation_operator)
except:
print("Unable to register the class \"render_and_denoise_animation_operator\"")
try:
bpy.utils.register_class(optix_denoising_panel)
except:
print("Unable to register the class \"optix_denoising_panel\"")
def unregister() :
global custom_icons
bpy.utils.previews.remove(custom_icons)
try:
bpy.utils.unregister_class(denoise_render_result_operator)
except:
print("Unable to unregister the class \"denoise_render_result_operator\"")
try:
bpy.utils.unregister_class(render_and_denoise_operator)
except:
print("Unable to unregister the class \"render_and_denoise_operator\"")
try:
bpy.utils.unregister_class(denoise_rendered_animation_operator)
except:
print("Unable to unregister the class \"denoise_rendered_animation_operator\"")
try:
bpy.utils.unregister_class(render_and_denoise_animation_operator)
except:
print("Unable to unregister the class \"render_and_denoise_animation_operator\"")
try:
bpy.utils.unregister_class(optix_denoising_panel)
except:
print("Unable to unregister the class \"optix_denoising_panel\"")
if __name__ == "__main__" :
register()