-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patholm_resolutionpicker.py
More file actions
168 lines (132 loc) · 4.6 KB
/
olm_resolutionpicker.py
File metadata and controls
168 lines (132 loc) · 4.6 KB
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
import os
DEBUG_MODE = False
def debug_print(*args, **kwargs):
if DEBUG_MODE:
print(*args, **kwargs)
class OlmResolutionPicker:
RESOLUTION_FILE = os.path.abspath(
os.path.join(os.path.dirname(__file__), "resolutions.txt")
)
@classmethod
def INPUT_TYPES(cls):
options = cls.load_resolutions()
return {
"required": {
"resolution": (
options,
{"default": options[0] if options else "1024x1024"},
),
"draw_preview": ("BOOLEAN", {"default": True}),
"show_checker": ("BOOLEAN", {"default": False}),
"show_image": ("BOOLEAN", {"default": False}),
"swap_dimensions": ("BOOLEAN", {"default": False}),
"custom_width": (
"INT",
{"default": 512, "min": 64, "max": 8192, "step": 1},
),
"custom_height": (
"INT",
{"default": 512, "min": 64, "max": 8192, "step": 1},
),
"divisible_by": (
["1", "2", "4", "8", "16", "32", "64"],
{"default": "64"},
),
}
}
@staticmethod
def load_resolutions():
debug_print(
"[OlmResolutionPicker] Resolution file: ",
OlmResolutionPicker.RESOLUTION_FILE,
)
if not os.path.exists(OlmResolutionPicker.RESOLUTION_FILE):
debug_print(
"[OlmResolutionPicker] Resolution file missing. Using fallback defaults."
)
return ["512x512", "1024x1024"]
with open(OlmResolutionPicker.RESOLUTION_FILE, "r") as f:
lines = f.readlines()
cleaned = []
for line in lines:
line = line.strip()
if not line:
continue
if line.startswith("//"):
continue
if line.startswith("--"):
header = line[2:].strip()
cleaned.append(f"-- {header} --")
continue
if ":" in line:
res_part, label = line.split(":", 1)
res_part = res_part.strip()
label = label.strip()
else:
res_part = line
label = ""
if "x" in res_part:
try:
w, h = map(int, res_part.lower().split("x"))
if 64 <= w <= 8192 and 64 <= h <= 8192:
if label:
cleaned.append(f"{res_part}: {label}")
else:
cleaned.append(res_part)
except ValueError:
continue
if not cleaned:
cleaned = ["512x512"]
if "Custom" not in cleaned:
cleaned.append("Custom")
return cleaned
RETURN_TYPES = ("INT", "INT")
RETURN_NAMES = ("width", "height")
FUNCTION = "pick"
CATEGORY = "Utilities"
def pick(
self,
resolution,
draw_preview,
show_checker,
show_image,
swap_dimensions,
custom_width=512,
custom_height=512,
divisible_by="64",
):
def round_to_multiple(value, multiple):
return max(multiple, round(value / multiple) * multiple)
if resolution.strip().lower() == "custom":
try:
snap = int(divisible_by)
except:
snap = 64
width = round_to_multiple(custom_width, snap)
height = round_to_multiple(custom_height, snap)
width = max(64, min(width, 8192))
height = max(64, min(height, 8192))
else:
if resolution.startswith("--"):
debug_print(
f"[OlmResolutionPicker] Ignored header: '{resolution}', fallback to 1024x1024."
)
return (1024, 1024)
base_res = resolution.split(":")[0].strip()
try:
width, height = map(int, base_res.lower().split("x"))
except Exception as e:
debug_print(
f"[OlmResolutionPicker] Failed to parse resolution '{resolution}': {e}"
)
return (1024, 1024)
if swap_dimensions:
width, height = height, width
return (width, height)
WEB_DIRECTORY = "./js"
NODE_CLASS_MAPPINGS = {
"OlmResolutionPicker": OlmResolutionPicker,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"OlmResolutionPicker": "Olm Resolution Picker",
}