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 pathfmutils.py
218 lines (163 loc) · 7 KB
/
fmutils.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
"""
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
import zipfile
import shutil
#
# EXTERNAL FILE MANAGEMENT
#
def save(directory, filename, image):
"""Saves a Blender image file to an external directory"""
if image.name == 'Render Result':
image.save_render(filepath=os.path.join(directory, filename))
else:
shutil.copyfile(image.filepath, os.path.join(directory, filename))
def load(directory, filename, imagekey):
"""Loads an external image into a Blender image file called 'D-NOISE Export'"""
if 'D-NOISE Export' in bpy.data.images:
bpy.data.images.remove(bpy.data.images['D-NOISE Export'])
bpy.data.images.load(filepath=os.path.join(directory, filename))
bpy.data.images[filename].name = imagekey
fileformat = os.path.splitext(filename)[1]
setcolorspace(imagekey, fileformat)
def clean(directory, fileformat):
"""Deletes all files of a given format from a directory"""
os.chdir(directory)
for file in os.listdir(directory):
if fileformat in file:
os.remove(file)
def deepclean(directory, format_dict):
"""Deletes all files of a given set of formats from a directory"""
for key in format_dict:
clean(directory, format_dict[key])
def getmostrecent(directory):
"""Returns the file name of the most recently edited file"""
os.chdir(directory)
render_files = sorted(os.listdir(os.getcwd()), key=os.path.getmtime)
return render_files[-1]
def unzip(directory, filename):
"""Unzips a .zip file to a local directory and deletes it when its extracted"""
file_dir = os.path.join(directory, filename)
with zipfile.ZipFile(file_dir, 'r') as zip_ref:
zip_ref.extractall("")
os.chdir(directory)
os.remove(filename)
def removeoptixbin(directory):
"""Removes all remnants of the OptiX binaries."""
os.chdir(directory)
if os.path.exists("DNOISE_OptiXBinaries.zip"):
os.remove("DNOISE_OptiXBinaries.zip")
if os.path.isdir("OptiXDenoiser"):
shutil.rmtree(os.path.join(directory, "OptiXDenoiser"))
#
# FILE PATH FUNCTIONS
#
def fixfilepath(path):
"""Unifies expandlocal and truncate to convert incompatible file directories to proper ones"""
path = exapandlocal(path)
path = truncate(path)
return path
def truncate(path):
"""Returns a file path with all data removed after the last slash -- e.g. C:\\myfile\\word becomes C:\\myfile\\"""
if not path[-1] == "\\":
truncated = path[:path.rfind("\\")]
else:
truncated = path
return truncated
def truncateext(filename):
"""Returns the file extension of a filename"""
truncated = filename[filename.rfind(".")+1:]
return truncated
def exapandlocal(path):
"""Replaces the // at the beginning of a local file path with the full file path"""
if path[:2] == "//":
expanded = bpy.path.abspath(path)
else:
expanded = path
return expanded
#
# INTERNAL IMAGE MANAGEMENT
#
def setactiveimage(imagekey, space=None):
"""Decides whether to run setactiveimage_context or setactiveimage_nocontext based on space data"""
if space is not None:
setactiveimage_context(imagekey, space)
else:
setactiveimage_nocontext(imagekey)
def setactiveimage_context(imagekey, space):
"""Sets the given image as the active image of the specified image editor space"""
if imageexists(imagekey):
space.image = bpy.data.images[imagekey]
def setactiveimage_nocontext(imagekey):
"""Sets the given image as the active image of any image editor displaying the render result"""
for window in bpy.data.window_managers['WinMan'].windows:
for area in window.screen.areas:
if area.type == 'IMAGE_EDITOR' and (area.spaces[0].image is None or area.spaces[0].image.name == 'Render Result'):
area.spaces[0].image = bpy.data.images[imagekey]
def setcolorspace(imagekey, fileformat):
"""Sets the colorspace settings of the specified Blender image"""
if imageexists(imagekey):
linearformats = ['OPEN_EXR', 'OPEN_EXR_MULTILAYER', 'HDR']
if fileformat in linearformats:
bpy.data.images[imagekey].use_view_as_render = True
# try-except to prevent custom OCIOs from throwing errors
try:
bpy.data.images[imagekey].colorspace_settings.name = 'Linear'
except:
pass
else:
try:
bpy.data.images[imagekey].colorspace_settings.name = 'sRGB'
except:
pass
def imageexists(imagekey):
"""Returns true if the given image key exists in Blender"""
if imagekey not in bpy.data.images:
print(">> D-NOISE ERROR: image {} does not exist in bpy.data.images".format(imagekey))
return imagekey in bpy.data.images
def getextension(file_format, extension_dict):
"""Checks to make sure the given file format is in the specified extension dictionary, if not, default to PNG"""
if file_format in extension_dict:
file_extension = extension_dict[file_format]
else:
print(">> D-NOISE ERROR: File output format {0} is not supported by D-NOISE. Defaulting to 'PNG'.".format(file_format))
bpy.context.scene.render.image_settings.file_format = 'PNG'
file_extension = extension_dict['PNG']
return file_extension
#
# RENDER LAYER FUNCTIONS
#
def enablepasses():
"""Enables the passes required by D-NOISE for extra pass denoising"""
bpy.context.scene.view_layers[0].use_pass_diffuse_color = True
bpy.context.scene.view_layers[0].use_pass_subsurface_color = True
bpy.context.scene.view_layers[0].use_pass_emit = True
bpy.context.scene.view_layers[0].use_pass_normal = True
def disablepasses():
"""Disables the passes required by D-NOISE for extra pass denoising"""
bpy.context.scene.view_layers[0].use_pass_diffuse_color = False
bpy.context.scene.view_layers[0].use_pass_subsurface_color = False
bpy.context.scene.view_layers[0].use_pass_emit = False
bpy.context.scene.view_layers[0].use_pass_normal = False
#
# Force Update Function
#
def forceUIUpdate(area_type):
"""Forces the UI to update in all areas of a specified type"""
for window in bpy.data.window_managers['WinMan'].windows:
for area in window.screen.areas:
if area.type == area_type:
area.tag_redraw()