This repository has been archived by the owner on Feb 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathoptix.py
183 lines (138 loc) · 5.95 KB
/
optix.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
"""
Copyright (C) 2018 Grant Wilk
This file is part of D-NOISE: AI-Acclerated Denoiser.
D-NOISE: AI-Acclerated Denoiser is free software: you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
D-NOISE: AI-Acclerated Denoiser is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with D-NOISE: AI-Acclerated Denoiser. If not, see <https://www.gnu.org/licenses/>.
"""
import bpy
import os
from mathutils import Vector
from . import fmutils
#
# Denoise Functions
#
def denoise(directory, source_name):
"""Runs full denoise or beauty denoise depending on available information"""
if bpy.context.scene.EnableExtraPasses:
normal_name = getnormal(directory)
albedo_name = getalbedo(directory)
fulldenoise(directory, source_name, normal_name, albedo_name, gethdr(), getblend())
else:
beautydenoise(directory, source_name, gethdr(),getblend())
def beautydenoise(directory, source_name, hdr, blend):
"""Runs OptiX standalone denoiser with information for a beauty pass"""
os.chdir(directory)
os.system('.\OptiXDenoiser\Denoiser.exe -i "{0}" -o "{0}" -hdr {1} -b {2}'.format(source_name, hdr, blend))
def fulldenoise(directory, source_name, normal_name, albedo_name, hdr, blend):
"""Runs OptiX standalone denoiser with information for a full denoising pass"""
os.chdir(directory)
convertnormals(directory, normal_name)
os.system('.\OptiXDenoiser\Denoiser.exe -i "{0}" -o "{0}" -n "{1}" -a "{2}" -hdr {3} -b {4}'.format(source_name, normal_name, albedo_name, hdr, blend))
#
# Node Functions
#
def addnodes(output_dir):
"""Adds the D-NOISE extra pass node set to the compositor node tree"""
bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree
# create new render layer node
render_layer = tree.nodes.new(type='CompositorNodeRLayers')
render_layer.label = '[D-NOISE] Render Layers'
render_layer.layer = 'View Layer'
render_layer.location = 0, 0
# create first mix RGB node
mix_emit_diffcol = tree.nodes.new('CompositorNodeMixRGB')
mix_emit_diffcol.label = '[D-NOISE] Add'
mix_emit_diffcol.blend_type = 'ADD'
mix_emit_diffcol.location = 280, -120
mix_emit_diffcol.hide = True
# create first mix RGB node
mix_last_subcol = tree.nodes.new('CompositorNodeMixRGB')
mix_last_subcol.label = '[D-NOISE] Add'
mix_last_subcol.blend_type = 'ADD'
mix_last_subcol.location = 400, -120
mix_last_subcol.hide = True
# create file output node
file_output = tree.nodes.new('CompositorNodeOutputFile')
file_output.label = '[D-NOISE] File Output'
file_output.base_path = output_dir
file_output.file_slots.clear()
file_output.file_slots.new('Normal')
file_output.file_slots.new('Albedo')
file_output.show_options = False
file_output.format.file_format = 'OPEN_EXR'
file_output.format.color_depth = '32'
file_output.location = 520, -100
file_output.hide = True
# link nodes
links = tree.links
links.new(render_layer.outputs['Normal'], file_output.inputs['Normal'])
links.new(render_layer.outputs['Emit'], mix_emit_diffcol.inputs[1])
links.new(render_layer.outputs['DiffCol'], mix_emit_diffcol.inputs[2])
links.new(mix_emit_diffcol.outputs['Image'], mix_last_subcol.inputs[1])
links.new(render_layer.outputs['SubsurfaceCol'], mix_last_subcol.inputs[2])
links.new(mix_last_subcol.outputs['Image'], file_output.inputs['Albedo'])
def cleannodes():
"""Returns true if the D-NOISE extra pass nodes already exist in the compositor"""
bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree
for node in bpy.context.scene.node_tree.nodes:
if "D-NOISE" in node.label:
tree.nodes.remove(node)
#
# Util Functions
#
def getnormal(directory):
"""Returns the file name of the last file with the string 'Normal' in a given directory"""
os.chdir(directory)
files = os.listdir(directory)
normal_filename = None
for file in files:
if "Normal" in file:
normal_filename = file
return normal_filename
def getalbedo(directory):
"""Returns the file name of the last file with the string 'Albedo' in a given directory"""
os.chdir(directory)
files = os.listdir(directory)
albedo_filename = None
for file in files:
if "Albedo" in file:
albedo_filename = file
return albedo_filename
def gethdr():
"""Returns whether or not HDR training data is enabled"""
return 1 if bpy.context.scene.EnableHDRData else 0
def getblend():
"""Returns the float presented by the D-NOISE blend property"""
return bpy.context.scene.DNOISEBlend
#
# NORMAL FUNCTIONS
#
def convertnormals(directory, filename):
"""Carries out the process of converting a world space normal image to a screen space normal image"""
fmutils.load(directory, filename, 'Normal')
bpy.data.images['Normal'].pixels = toscreenspace(bpy.data.images['Normal'])
bpy.data.images['Normal'].save()
bpy.data.images.remove(bpy.data.images['Normal'])
def toscreenspace(image):
"""Converts the pixel data of a world space normal image to screen space normal pixels"""
pixels = list(image.pixels)
camera = bpy.context.scene.camera
camera_rotation = camera.rotation_euler.to_quaternion()
camera_rotation.invert()
for i in range(0, len(pixels), 4):
normal = Vector((pixels[i + 0], pixels[i + 1], pixels[i + 2]))
screen_space_normal = camera_rotation @ normal
pixels[i + 0] = screen_space_normal[0]
pixels[i + 1] = screen_space_normal[1]
pixels[i + 2] = screen_space_normal[2]
return pixels