This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathMultiAreaConditioning.py
181 lines (135 loc) · 5.18 KB
/
MultiAreaConditioning.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
# Made by Davemane42#0042 for ComfyUI
# 02/04/2023
import torch
from nodes import MAX_RESOLUTION
class MultiAreaConditioning:
def __init__(self) -> None:
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"conditioning0": ("CONDITIONING", ),
"conditioning1": ("CONDITIONING", )
},
"hidden": {"extra_pnginfo": "EXTRA_PNGINFO", "unique_id": "UNIQUE_ID"},
}
RETURN_TYPES = ("CONDITIONING", "INT", "INT")
RETURN_NAMES = (None, "resolutionX", "resolutionY")
FUNCTION = "doStuff"
CATEGORY = "Davemane42"
def doStuff(self, extra_pnginfo, unique_id, **kwargs):
c = []
values = []
resolutionX = 512
resolutionY = 512
for node in extra_pnginfo["workflow"]["nodes"]:
if node["id"] == int(unique_id):
values = node["properties"]["values"]
resolutionX = node["properties"]["width"]
resolutionY = node["properties"]["height"]
break
k = 0
for arg in kwargs:
if k > len(values): break;
if not torch.is_tensor(kwargs[arg][0][0]): continue;
x, y = values[k][0], values[k][1]
w, h = values[k][2], values[k][3]
# If fullscreen
if (x == 0 and y == 0 and w == resolutionX and h == resolutionY):
for t in kwargs[arg]:
c.append(t)
k += 1
continue
if x+w > resolutionX:
w = max(0, resolutionX-x)
if y+h > resolutionY:
h = max(0, resolutionY-y)
if w == 0 or h == 0: continue;
for t in kwargs[arg]:
n = [t[0], t[1].copy()]
n[1]['area'] = (h // 8, w // 8, y // 8, x // 8)
n[1]['strength'] = values[k][4]
n[1]['min_sigma'] = 0.0
n[1]['max_sigma'] = 99.0
c.append(n)
k += 1
return (c, resolutionX, resolutionY)
class ConditioningUpscale():
def __init__(self) -> None:
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"conditioning": ("CONDITIONING", ),
"scalar": ("INT", {"default": 2, "min": 1, "max": 100, "step": 0.5}),
},
}
RETURN_TYPES = ("CONDITIONING",)
CATEGORY = "Davemane42"
FUNCTION = 'upscale'
def upscale(self, conditioning, scalar):
c = []
for t in conditioning:
n = [t[0], t[1].copy()]
if 'area' in n[1]:
n[1]['area'] = tuple(map(lambda x: ((x*scalar + 7) >> 3) << 3, n[1]['area']))
c.append(n)
return (c, )
class ConditioningStretch():
def __init__(self) -> None:
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"conditioning": ("CONDITIONING", ),
"resolutionX": ("INT", {"default": 512, "min": 64, "max": MAX_RESOLUTION, "step": 64}),
"resolutionY": ("INT", {"default": 512, "min": 64, "max": MAX_RESOLUTION, "step": 64}),
"newWidth": ("INT", {"default": 512, "min": 64, "max": MAX_RESOLUTION, "step": 64}),
"newHeight": ("INT", {"default": 512, "min": 64, "max": MAX_RESOLUTION, "step": 64}),
#"scalar": ("INT", {"default": 2, "min": 1, "max": 100, "step": 0.5}),
},
}
RETURN_TYPES = ("CONDITIONING",)
CATEGORY = "Davemane42"
FUNCTION = 'upscale'
def upscale(self, conditioning, resolutionX, resolutionY, newWidth, newHeight, scalar=1):
c = []
for t in conditioning:
n = [t[0], t[1].copy()]
if 'area' in n[1]:
newWidth *= scalar
newHeight *= scalar
#n[1]['area'] = tuple(map(lambda x: ((x*scalar + 32) >> 6) << 6, n[1]['area']))
x = ((n[1]['area'][3]*8)*newWidth/resolutionX) // 8
y = ((n[1]['area'][2]*8)*newHeight/resolutionY) // 8
w = ((n[1]['area'][1]*8)*newWidth/resolutionX) // 8
h = ((n[1]['area'][0]*8)*newHeight/resolutionY) // 8
n[1]['area'] = tuple(map(lambda x: (((int(x) + 7) >> 3) << 3), [h, w, y, x]))
c.append(n)
return (c, )
class ConditioningDebug():
def __init__(self) -> None:
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"conditioning": ("CONDITIONING", ),
}
}
RETURN_TYPES = ()
FUNCTION = "debug"
OUTPUT_NODE = True
CATEGORY = "Davemane42"
def debug(self, conditioning):
print("\nDebug")
for i, t in enumerate(conditioning):
print(f"{i}:")
if "area" in t[1]:
print(f"\tx{t[1]['area'][3]*8} y{t[1]['area'][2]*8} \n\tw{t[1]['area'][1]*8} h{t[1]['area'][0]*8} \n\tstrength: {t[1]['strength']}")
else:
print(f"\tFullscreen")
return (None, )