forked from benrugg/AI-Render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
282 lines (198 loc) · 8.12 KB
/
utils.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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import bpy
import re
import os
import shutil
import tempfile
from . import config
valid_dimensions = [384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024]
file_formats = {"JPEG": "jpg", "BMP": "bmp", "IRIS": "rgb", "PNG": "png", "JPEG2000": "jp2", "TARGA": "tga", "TARGA_RAW": "tga", "CINEON": "cin", "DPX": "dpx", "OPEN_EXR_MULTILAYER": "exr", "OPEN_EXR": "exr", "HDR": "hdr", "TIFF": "tif", "WEBP": "webp"}
def get_addon_preferences(context=None):
if not context:
context = bpy.context
return context.preferences.addons[__package__].preferences
def create_temp_file(prefix, suffix=".png"):
return tempfile.NamedTemporaryFile(prefix=prefix, suffix=suffix).name
def get_filepath_in_package(path, filename="", starting_dir=__file__):
"""Convert a relative path in the add-on package to an absolute path"""
script_path = os.path.dirname(os.path.realpath(starting_dir))
subpath = path + os.sep + filename if path else filename
return os.path.join(script_path, subpath)
def get_absolute_path_for_output_file(path, filename):
"""Convert a relative path in the blend file to an absolute path"""
return os.path.join(os.path.abspath(bpy.path.abspath(path)), filename)
def copy_file(src, dest):
shutil.copy2(src, dest)
def get_workspace_blend_file_filepath():
return get_filepath_in_package("blendfiles", "air_workspace.blend")
def get_preset_style_thumnails_filepath():
return get_filepath_in_package("style_thumbnails")
def get_extension_from_file_format(file_format):
if file_format in file_formats:
return file_formats[file_format]
else:
return ""
def get_current_workspace(context=None):
if not context:
context = bpy.context
if context.window and context.window.workspace:
return context.window.workspace
else:
return None
def activate_workspace(context=None, workspace=None, workspace_id=None):
if not workspace:
workspace = bpy.data.workspaces[workspace_id]
if context and context.window:
context.window.workspace = workspace
else:
bpy.data.window_managers[0].windows[0].workspace = workspace
def view_render_result_in_air_image_editor():
image_editor_area = None
areas = bpy.data.workspaces[config.workspace_id].screens[0].areas
for area in areas:
if area.type == 'IMAGE_EDITOR':
image_editor_area = area
break
if image_editor_area:
image_editor_area.spaces.active.image = bpy.data.images['Render Result']
def get_api_key(context=None):
return get_addon_preferences(context).dream_studio_api_key
def do_use_local_sd(context=None):
return get_addon_preferences(context).is_local_sd_enabled
def local_sd_backend(context=None):
return get_addon_preferences(context).local_sd_backend
def local_sd_url(context=None):
return get_addon_preferences(context).local_sd_url
def local_sd_timeout(context=None):
return get_addon_preferences(context).local_sd_timeout
def get_output_width(scene):
return round(scene.render.resolution_x * scene.render.resolution_percentage / 100)
def get_output_height(scene):
return round(scene.render.resolution_y * scene.render.resolution_percentage / 100)
def are_dimensions_valid(scene):
return (
get_output_width(scene) in valid_dimensions and
get_output_height(scene) in valid_dimensions
)
def are_dimensions_too_large(scene):
if do_use_local_sd():
return False
else:
return get_output_width(scene) * get_output_height(scene) > config.max_image_px_area
def generate_valid_dimensions_tuple_list():
return_tuple = lambda num: (str(num), str(num) + " px", str(num))
return list(map(return_tuple, valid_dimensions))
def has_url(text, strict_match_protocol=False):
# remove markdown *
text = text.replace('*','')
# Anything that isn't a square closing bracket
name_regex = "[^]]+"
# http:// or https:// followed by anything but a closing paren
url_in_markup_regex = "http[s]?://[^)]+"
# first look for markup urls
markup_regex = f"\[({name_regex})]\(\s*({url_in_markup_regex})\s*\)"
urls = re.findall(markup_regex, text, re.IGNORECASE)
if len(urls) > 0:
replacechars = "[]()"
for url in urls:
text = re.sub(markup_regex, "", text)
for ch in replacechars:
text.replace(ch, '')
# if none found, look for url without markup
else:
if strict_match_protocol:
bare_url_regex = r"(https{0,1}:\/\/[A-Za-z0-9\-\._~:\/\?#\[\]@!\$&'\(\)\*\+\,;%=]+)"
else:
bare_url_regex = r"(?:[a-z]{3,9}:\/\/?[\-;:&=\+\$,\w]+?[a-z0-9\.\-]+|[\/a-z0-9]+\.|[\-;:&=\+\$,\w]+@)[a-z0-9\.\-]+(?:(?:\/[\+~%\/\.\w\-_]*)?\??[\-\+=&;%@\.\w_]*#?[\.\!\/\\\w]*)?"
urls = re.findall(bare_url_regex, text, re.IGNORECASE)
for i, url in enumerate(urls):
urls[i] = [url, url]
# return what was found (could be just text)
return urls, text
def label_multiline(layout, text='', icon='NONE', width=-1, max_lines=12, use_urls=True, alignment="LEFT", alert=False):
'''
draw a ui label, but try to split it in multiple lines.
Parameters
----------
layout
text
icon
width width to split by in px
max_lines maximum lines to draw
use_urls - automatically parse urls to buttons
Returns
-------
rows of the text(to add extra elements)
'''
rows = []
if text.strip() == '':
return [layout.row()]
text = text.replace("\r\n", "\n")
if use_urls:
urls, text = has_url(text, strict_match_protocol=True)
else:
urls = []
lines = text.split("\n")
if width > 0:
char_threshold = int(width / 5.7)
else:
char_threshold = 35
line_index = 0
for line in lines:
line_index += 1
while len(line) > char_threshold:
#find line split close to the end of line
i = line.rfind(" ", 0, char_threshold)
#split long words
if i < 1:
i = char_threshold
l1 = line[:i]
row = layout.row()
if alert: row.alert = True
row.alignment = alignment
row.label(text=l1, icon=icon)
rows.append(row)
# set the icon to none after the first row
icon = "NONE"
line = line[i:].lstrip()
line_index += 1
if line_index > max_lines:
break
if line_index > max_lines:
break
row = layout.row()
if alert: row.alert = True
row.alignment = alignment
row.label(text=line, icon=icon)
rows.append(row)
# set the icon to none after the first row
icon = "NONE"
# if we have urls, include them as buttons at the end
if use_urls:
for url in urls:
row = layout.row()
row.operator("wm.url_open", text=url[0], icon="URL").url = url[1]
# return the resulting rows
return rows
def is_installation_valid():
return __package__ == config.package_name
def show_invalid_installation_message(layout, width):
box = layout.box()
box.label(text="Installation Error:")
label_multiline(box, text=f"It looks like this add-on wasn't installed correctly. Please remove it and get a new copy. [Get AI Render]({config.ADDON_DOWNLOAD_URL})", icon="ERROR", alert=True, width=width)