-
Notifications
You must be signed in to change notification settings - Fork 2
/
nodes.py
502 lines (413 loc) · 15.6 KB
/
nodes.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
from comfy.model_management import InterruptProcessingException
from comfy.model_patcher import ModelPatcher
import torch
import os
import hashlib
import folder_paths
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import torchvision.transforms as transforms
import json
class AnyType(str):
def __ne__(self, __value: object) -> bool:
return False
class PackedAxisItem:
def __init__(self, label, value):
self.label = label
self.value = value
class FeedbackNode:
def __init__(self):
self.output_dir = folder_paths.get_temp_directory()
self.type = "temp"
def get_feedback(self, text):
image = self.create_text_image(text)
return {"ui": {"images": self.preview_images([image])}}
def preview_images(self, images, filename_prefix="QQNodes"):
full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(
filename_prefix, self.output_dir, images[0].shape[1], images[0].shape[0])
results = list()
for image in images:
i = 255. * image.cpu().numpy()
img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
file = f"{filename}_{counter:05}_.png"
img.save(os.path.join(full_output_folder, file), compress_level=4)
results.append({
"filename": file,
"subfolder": subfolder,
"type": self.type
})
counter += 1
return results
def create_text_image(self, text, font_size=24, image_size=(256, 256), background_color=(255, 255, 255), text_color=(0, 0, 0), font_path=None):
# Create a new image with the specified background color
image = Image.new('RGB', image_size, background_color)
# Create a font object with the specified size and font file
font = font = ImageFont.truetype(
str(Path(__file__).parent / "static" / "Roboto-Regular.ttf"), size=font_size)
# Create a draw object
draw = ImageDraw.Draw(image)
# Calculate the text position at the center of the image
text_width, text_height = draw.textsize(text, font=font)
text_position = ((image_size[0] - text_width) //
2, (image_size[1] - text_height) // 2)
# Draw the text on the image
draw.text(text_position, text, font=font, fill=text_color)
transform = transforms.ToTensor()
image_tensor = transform(image)
# reshape to shape expexted by preview_images
image_tensor = image_tensor.permute(1, 2, 0)
return image_tensor
class ImageAccumulatorStart(FeedbackNode):
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"images": ("IMAGE",),
"count": ("INT", {"default": 1}),
},
"optional": {
"reset": ("INT", {"default": 1}),
}
}
RETURN_TYPES = ("IMAGE", "IMAGE_ACCUMULATOR_STATUS")
RETURN_NAMES = ("images", "status")
FUNCTION = "run"
CATEGORY = "QQNodes/Image"
image_batch = torch.Tensor()
def run(self, images, count, reset):
if reset == 0:
self.image_batch = torch.Tensor()
total_images = torch.cat((self.image_batch, images))
processed_images = total_images[:count]
remaining_images = total_images[count:]
if len(remaining_images) > 0:
self.image_batch = remaining_images
else:
self.image_batch = processed_images
image_list = [processed_images[i]
for i in range(processed_images.shape[0])]
ui_result = self.preview_images(image_list)
return {"result": (image_list, len(image_list) >= count), "ui": {"images": ui_result}}
class ImageAccumulatorEnd(FeedbackNode):
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"images": ("IMAGE",),
"status": ("IMAGE_ACCUMULATOR_STATUS",),
},
}
RETURN_TYPES = "IMAGE",
FUNCTION = "run"
OUTPUT_NODE = True
CATEGORY = "QQNodes/Image"
def run(self, images, status):
if not status:
raise InterruptProcessingException()
else:
return (images,)
class AnyList:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_a": (AnyType("*"), {"forceInput": True}),
},
"optional": {
"input_b": (AnyType("*"), {"forceInput": True}),
"input_c": (AnyType("*"), {"forceInput": True}),
"input_d": (AnyType("*"), {"forceInput": True}),
"input_e": (AnyType("*"), {"forceInput": True}),
"input_f": (AnyType("*"), {"forceInput": True}),
"input_g": (AnyType("*"), {"forceInput": True}),
}
}
RETURN_TYPES = ("LIST",)
FUNCTION = "run"
CATEGORY = "QQNodes/List"
def run(self, input_a, input_b=None, input_c=None, input_d=None, input_e=None, input_f=None, input_g=None):
input_list = [input_a,]
if input_b:
input_list.append(input_b)
if input_c:
input_list.append(input_c)
if input_d:
input_list.append(input_d)
if input_e:
input_list.append(input_e)
if input_f:
input_list.append(input_f)
if input_g:
input_list.append(input_g)
return (input_list,)
class AnyListIterator:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"counter": ("INT", {"default": 0}),
"list": ("LIST",),
}
}
RETURN_TYPES = "AXIS_VALUE",
FUNCTION = "run"
CATEGORY = "QQNodes/List"
def run(self, counter, list):
return (list[counter % len(list)],)
class AxisPack:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_a": (AnyType("*"), {"forceInput": True}),
},
"optional": {
"input_b": (AnyType("*"), {"forceInput": True}),
"input_c": (AnyType("*"), {"forceInput": True}),
"input_d": (AnyType("*"), {"forceInput": True}),
"input_e": (AnyType("*"), {"forceInput": True}),
"input_f": (AnyType("*"), {"forceInput": True}),
"input_g": (AnyType("*"), {"forceInput": True}),
"label": ("STRING", {"forceInput": False}),
}
}
RETURN_TYPES = ("PACK",)
FUNCTION = "run"
CATEGORY = "QQNodes/XYGrid Axis"
def run(self, input_a, input_b=None, input_c=None, input_d=None, input_e=None, input_f=None, input_g=None, label=""):
input_list = [input_a,]
if input_b:
input_list.append(input_b)
if input_c:
input_list.append(input_c)
if input_d:
input_list.append(input_d)
if input_e:
input_list.append(input_e)
if input_f:
input_list.append(input_f)
if input_g:
input_list.append(input_g)
return (PackedAxisItem(label, input_list),)
class AxisUnpack:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"axis": ("AXIS_VALUE",),
},
}
RETURN_TYPES = tuple("AXIS_VALUE" for _ in range(7))
RETURN_NAMES = tuple("output_" + chr(i) for i in range(ord('a'), ord('a') + 7))
FUNCTION = "run"
CATEGORY = "QQNodes/XYGrid Axis"
def run(self, axis):
padding = [None, ] * (7 - len(axis.value))
return tuple(axis.value + padding)
class LoadLinesFromTextFile:
@classmethod
def INPUT_TYPES(s):
input_dir = folder_paths.get_input_directory()
files = [f for f in os.listdir(input_dir) if os.path.isfile(
os.path.join(input_dir, f)) and f.endswith(".txt")]
return {
"required": {
"file": [sorted(files), ],
},
}
CATEGORY = "QQNodes/Text"
RETURN_TYPES = ("LIST", )
FUNCTION = "load"
lines = []
file_hash = None
def load(self, file):
file_path = folder_paths.get_annotated_filepath(file)
if LoadLinesFromTextFile.getFileHash(file_path) != self.file_hash:
with open(file_path, "r") as f:
self.lines = f.readlines()
self.file_hash = LoadLinesFromTextFile.getFileHash(file_path)
return (self.lines,)
@classmethod
def getFileHash(cls, file_path):
m = hashlib.sha256()
with open(file_path, 'rb') as f:
m.update(f.read())
return m.digest().hex()
@classmethod
def VALIDATE_INPUTS(cls, file):
if not folder_paths.exists_annotated_filepath(file):
return "Invalid text file: {}".format(file)
return True
@classmethod
def IS_CHANGED(cls, file):
file_path = folder_paths.get_annotated_filepath(file)
return cls.getFileHash(file_path)
class TextSplitter:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text": ("STRING", {"default": ""}),
"delimiter": ("STRING", {"default": ","}),
}
}
RETURN_TYPES = ("LIST",)
FUNCTION = "run"
CATEGORY = "QQNodes/Text"
def run(self, text, delimiter):
return (text.split(delimiter),)
class XYGridHelper():
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"row_list": ("LIST",),
"column_list": ("LIST",),
},
"optional": {
"row_prefix": ("STRING", {"default": ""}),
"column_prefix": ("STRING", {"default": ""}),
"page_size": ("INT", {"default": 10}),
"label_length": ("INT", {"default": 50}),
"index": ("QQINDEX", {} )
}
}
RETURN_TYPES = ("AXIS_VALUE", "AXIS_VALUE", "STRING",
"STRING", "INT", "INT", "INT")
RETURN_NAMES = ("row_value", "column_value", "row_annotation", "column_annotation",
"max_columns", "image_accumulator_count", "image_accumulator_reset")
FUNCTION = "run"
CATEGORY = "QQNodes/XYGrid"
@classmethod
def IS_CHANGED(cls, **kwargs):
return float("NaN")
def run(self, row_list, column_list, row_prefix, column_prefix, page_size, label_length, index):
total_grid_images = len(row_list) * len(column_list)
adjusted_index = index % total_grid_images
row_index = adjusted_index // len(column_list) % len(row_list)
page_index = row_index // page_size
images_pr_page = page_size * len(column_list)
row_annotation = ";".join([self.insert_newline_on_word_boundaries(self.format_prefix(row_prefix, self.get_label(x)), label_length) for x in row_list[page_index * page_size : (page_index + 1) * page_size]])
column_annotation = ";".join([self.insert_newline_on_word_boundaries(self.format_prefix(column_prefix, self.get_label(y)), label_length) for y in column_list])
return {"result": (
row_list[row_index],
column_list[adjusted_index % len(column_list)],
row_annotation,
column_annotation,
len(column_list),
min(images_pr_page, total_grid_images - page_index * page_size),
adjusted_index % images_pr_page
), "ui": {"total_images": [total_grid_images]}}
def get_label(self, item):
if isinstance(item, PackedAxisItem):
return item.label
else:
return str(item)
def format_prefix(self, prefix, text):
if prefix:
return f"{prefix}: {text}"
else:
return text
def truncate_string(self, input_string, length=50):
if len(input_string) > length:
return input_string[:length - 3] + '...'
else:
return input_string
def insert_newline_on_word_boundaries(self, input_string, length=50):
# Initialize the result string and the current index
result = ""
current_index = 0
while current_index < len(input_string):
# If the remaining string is shorter than the length, add it to the result and break
if current_index + length >= len(input_string):
result += input_string[current_index:]
break
# Find the nearest space before the next cut-off point
next_cutoff = current_index + length
space_index = input_string.rfind(' ', current_index, next_cutoff)
# If a space is found, and it's not just the first character (avoiding leading spaces)
if space_index > current_index:
# Add the substring up to the space and a newline
result += input_string[current_index:space_index] + '\n'
# Update the current index to the character after the space
current_index = space_index + 1
else:
# If no suitable space is found, just cut at the specified length
result += input_string[current_index:next_cutoff] + '\n'
current_index = next_cutoff
return result
class SliceList:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"list": ("LIST",),
"start": ("INT", {"default": 0}),
"end": ("INT", {"default": 1}),
}
}
RETURN_TYPES = ("LIST",)
FUNCTION = "run"
CATEGORY = "QQNodes/List"
def run(self, list, start, end):
return (list[start:end],)
class AnyToAny:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"any": (AnyType("*"),),
}
}
RETURN_TYPES = (AnyType("*"),)
FUNCTION = "run"
CATEGORY = "QQNodes/Utils"
def run(self, any):
return (any,)
class AxisBase:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"axis": ("AXIS_VALUE",),
}
}
FUNCTION = "run"
CATEGORY = "QQNodes/XYGrid Axis"
def run(self, axis):
return (axis,)
class AxisToAny(AxisBase):
RETURN_TYPES = (AnyType("*"),)
def create_axis_class(name):
class_dict = {
'RETURN_TYPES': (name,),
}
return type(f"AxisTo{name}", (AxisBase,), class_dict)
def load_axis_config_and_create_classes(node_map, config_file):
dir_path = os.path.dirname(os.path.realpath(__file__))
config_path = os.path.join(dir_path, config_file)
with open(config_path, 'r') as f:
config = json.load(f)
if not isinstance(config, list):
raise ValueError("Axis config must be a json list")
for axis_config in config:
cls = create_axis_class(axis_config)
globals()[axis_config] = cls
node_map["Axis To " + axis_config] = cls
NODE_CLASS_MAPPINGS = {
"Any List": AnyList,
"Any List Iterator": AnyListIterator,
"Image Accumulator Start": ImageAccumulatorStart,
"Image Accumulator End": ImageAccumulatorEnd,
"Load Lines From Text File": LoadLinesFromTextFile,
"XY Grid Helper": XYGridHelper,
"Slice List": SliceList,
"Axis Pack": AxisPack,
"Axis Unpack": AxisUnpack,
"Text Splitter": TextSplitter,
"Any To Any": AnyToAny,
"Axis To Any": AxisToAny
}
load_axis_config_and_create_classes(NODE_CLASS_MAPPINGS, "axis-config.json")
load_axis_config_and_create_classes(NODE_CLASS_MAPPINGS, "custom-axis-config.json")