-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathlogics.py
756 lines (579 loc) · 21.5 KB
/
logics.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
import sys
import time
import execution
import impact.impact_server
from server import PromptServer
from impact.utils import any_typ
import impact.core as core
import re
import nodes
import traceback
class ImpactCompare:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"cmp": (['a = b', 'a <> b', 'a > b', 'a < b', 'a >= b', 'a <= b', 'tt', 'ff'],),
"a": (any_typ, ),
"b": (any_typ, ),
},
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic"
RETURN_TYPES = ("BOOLEAN", )
def doit(self, cmp, a, b):
if cmp == "a = b":
return (a == b, )
elif cmp == "a <> b":
return (a != b, )
elif cmp == "a > b":
return (a > b, )
elif cmp == "a < b":
return (a < b, )
elif cmp == "a >= b":
return (a >= b, )
elif cmp == "a <= b":
return (a <= b, )
elif cmp == 'tt':
return (True, )
else:
return (False, )
class ImpactNotEmptySEGS:
@classmethod
def INPUT_TYPES(cls):
return {"required": {"segs": ("SEGS",)}}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic"
RETURN_TYPES = ("BOOLEAN", )
def doit(self, segs):
return (segs[1] != [], )
class ImpactConditionalBranch:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"cond": ("BOOLEAN",),
"tt_value": (any_typ,{"lazy": True}),
"ff_value": (any_typ,{"lazy": True}),
},
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic"
RETURN_TYPES = (any_typ, )
def check_lazy_status(self, cond, tt_value=None, ff_value=None):
if cond and tt_value is None:
return ["tt_value"]
if not cond and ff_value is None:
return ["ff_value"]
def doit(self, cond, tt_value=None, ff_value=None):
if cond:
return (tt_value,)
else:
return (ff_value,)
class ImpactConditionalBranchSelMode:
@classmethod
def INPUT_TYPES(cls):
if not core.is_execution_model_version_supported():
required_inputs = {
"cond": ("BOOLEAN",),
"sel_mode": ("BOOLEAN", {"default": True, "label_on": "select_on_prompt", "label_off": "select_on_execution"}),
}
else:
required_inputs = {
"cond": ("BOOLEAN",),
}
return {
"required": required_inputs,
"optional": {
"tt_value": (any_typ,),
"ff_value": (any_typ,),
},
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic"
RETURN_TYPES = (any_typ, )
def doit(self, cond, tt_value=None, ff_value=None, **kwargs):
print(f'tt={tt_value is None}\nff={ff_value is None}')
if cond:
return (tt_value,)
else:
return (ff_value,)
class ImpactConvertDataType:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {"required": {"value": (any_typ,)}}
RETURN_TYPES = ("STRING", "FLOAT", "INT", "BOOLEAN")
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic"
@staticmethod
def is_number(string):
pattern = re.compile(r'^[-+]?[0-9]*\.?[0-9]+$')
return bool(pattern.match(string))
def doit(self, value):
if self.is_number(str(value)):
num = value
else:
if str.lower(str(value)) != "false":
num = 1
else:
num = 0
return (str(value), float(num), int(float(num)), bool(float(num)), )
class ImpactIfNone:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {},
"optional": {"signal": (any_typ,), "any_input": (any_typ,), }
}
RETURN_TYPES = (any_typ, "BOOLEAN")
RETURN_NAMES = ("signal_opt", "bool")
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic"
def doit(self, signal=None, any_input=None):
if any_input is None:
return (signal, False, )
else:
return (signal, True, )
class ImpactLogicalOperators:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"operator": (['and', 'or', 'xor'],),
"bool_a": ("BOOLEAN", {"forceInput": True}),
"bool_b": ("BOOLEAN", {"forceInput": True}),
},
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic"
RETURN_TYPES = ("BOOLEAN", )
def doit(self, operator, bool_a, bool_b):
if operator == "and":
return (bool_a and bool_b, )
elif operator == "or":
return (bool_a or bool_b, )
else:
return (bool_a != bool_b, )
class ImpactConditionalStopIteration:
@classmethod
def INPUT_TYPES(cls):
return {
"required": { "cond": ("BOOLEAN", {"forceInput": True}), },
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic"
RETURN_TYPES = ()
OUTPUT_NODE = True
def doit(self, cond):
if cond:
PromptServer.instance.send_sync("stop-iteration", {})
return {}
class ImpactNeg:
@classmethod
def INPUT_TYPES(cls):
return {
"required": { "value": ("BOOLEAN", {"forceInput": True}), },
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic"
RETURN_TYPES = ("BOOLEAN", )
def doit(self, value):
return (not value, )
class ImpactInt:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"value": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}),
},
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic"
RETURN_TYPES = ("INT", )
def doit(self, value):
return (value, )
class ImpactFloat:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"value": ("FLOAT", {"default": 1.0, "min": -3.402823466e+38, "max": 3.402823466e+38}),
},
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic"
RETURN_TYPES = ("FLOAT", )
def doit(self, value):
return (value, )
class ImpactValueSender:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"value": (any_typ, ),
"link_id": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}),
},
"optional": {
"signal_opt": (any_typ,),
}
}
OUTPUT_NODE = True
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic"
RETURN_TYPES = (any_typ, )
RETURN_NAMES = ("signal", )
def doit(self, value, link_id=0, signal_opt=None):
PromptServer.instance.send_sync("value-send", {"link_id": link_id, "value": value})
return (signal_opt, )
class ImpactIntConstSender:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"signal": (any_typ, ),
"value": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}),
"link_id": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}),
},
}
OUTPUT_NODE = True
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic"
RETURN_TYPES = ()
def doit(self, signal, value, link_id=0):
PromptServer.instance.send_sync("value-send", {"link_id": link_id, "value": value})
return {}
class ImpactValueReceiver:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"typ": (["STRING", "INT", "FLOAT", "BOOLEAN"], ),
"value": ("STRING", {"default": ""}),
"link_id": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}),
},
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic"
RETURN_TYPES = (any_typ, )
def doit(self, typ, value, link_id=0):
if typ == "INT":
return (int(value), )
elif typ == "FLOAT":
return (float(value), )
elif typ == "BOOLEAN":
return (value.lower() == "true", )
else:
return (value, )
class ImpactImageInfo:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"value": ("IMAGE", ),
},
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic/_for_test"
RETURN_TYPES = ("INT", "INT", "INT", "INT")
RETURN_NAMES = ("batch", "height", "width", "channel")
def doit(self, value):
return (value.shape[0], value.shape[1], value.shape[2], value.shape[3])
class ImpactLatentInfo:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"value": ("LATENT", ),
},
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic/_for_test"
RETURN_TYPES = ("INT", "INT", "INT", "INT")
RETURN_NAMES = ("batch", "height", "width", "channel")
def doit(self, value):
shape = value['samples'].shape
return (shape[0], shape[2] * 8, shape[3] * 8, shape[1])
class ImpactMinMax:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"mode": ("BOOLEAN", {"default": True, "label_on": "max", "label_off": "min"}),
"a": (any_typ,),
"b": (any_typ,),
},
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic/_for_test"
RETURN_TYPES = ("INT", )
def doit(self, mode, a, b):
if mode:
return (max(a, b), )
else:
return (min(a, b),)
class ImpactQueueTrigger:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"signal": (any_typ,),
"mode": ("BOOLEAN", {"default": True, "label_on": "Trigger", "label_off": "Don't trigger"}),
}
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic/_for_test"
RETURN_TYPES = (any_typ,)
RETURN_NAMES = ("signal_opt",)
OUTPUT_NODE = True
def doit(self, signal, mode):
if(mode):
PromptServer.instance.send_sync("impact-add-queue", {})
return (signal,)
class ImpactQueueTriggerCountdown:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"count": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
"total": ("INT", {"default": 10, "min": 1, "max": 0xffffffffffffffff}),
"mode": ("BOOLEAN", {"default": True, "label_on": "Trigger", "label_off": "Don't trigger"}),
},
"optional": {"signal": (any_typ,),},
"hidden": {"unique_id": "UNIQUE_ID"}
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic/_for_test"
RETURN_TYPES = (any_typ, "INT", "INT")
RETURN_NAMES = ("signal_opt", "count", "total")
OUTPUT_NODE = True
def doit(self, count, total, mode, unique_id, signal=None):
if (mode):
if count < total - 1:
PromptServer.instance.send_sync("impact-node-feedback",
{"node_id": unique_id, "widget_name": "count", "type": "int", "value": count+1})
PromptServer.instance.send_sync("impact-add-queue", {})
if count >= total - 1:
PromptServer.instance.send_sync("impact-node-feedback",
{"node_id": unique_id, "widget_name": "count", "type": "int", "value": 0})
return (signal, count, total)
class ImpactSetWidgetValue:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"signal": (any_typ,),
"node_id": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
"widget_name": ("STRING", {"multiline": False}),
},
"optional": {
"boolean_value": ("BOOLEAN", {"forceInput": True}),
"int_value": ("INT", {"forceInput": True}),
"float_value": ("FLOAT", {"forceInput": True}),
"string_value": ("STRING", {"forceInput": True}),
}
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic/_for_test"
RETURN_TYPES = (any_typ,)
RETURN_NAMES = ("signal_opt",)
OUTPUT_NODE = True
def doit(self, signal, node_id, widget_name, boolean_value=None, int_value=None, float_value=None, string_value=None, ):
kind = None
if boolean_value is not None:
value = boolean_value
kind = "BOOLEAN"
elif int_value is not None:
value = int_value
kind = "INT"
elif float_value is not None:
value = float_value
kind = "FLOAT"
elif string_value is not None:
value = string_value
kind = "STRING"
else:
value = None
if value is not None:
PromptServer.instance.send_sync("impact-node-feedback",
{"node_id": node_id, "widget_name": widget_name, "type": kind, "value": value})
return (signal,)
class ImpactNodeSetMuteState:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"signal": (any_typ,),
"node_id": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
"set_state": ("BOOLEAN", {"default": True, "label_on": "active", "label_off": "mute"}),
}
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic/_for_test"
RETURN_TYPES = (any_typ,)
RETURN_NAMES = ("signal_opt",)
OUTPUT_NODE = True
def doit(self, signal, node_id, set_state):
PromptServer.instance.send_sync("impact-node-mute-state", {"node_id": node_id, "is_active": set_state})
return (signal,)
class ImpactSleep:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"signal": (any_typ,),
"seconds": ("FLOAT", {"default": 0.5, "min": 0, "max": 3600}),
}
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic/_for_test"
RETURN_TYPES = (any_typ,)
RETURN_NAMES = ("signal_opt",)
OUTPUT_NODE = True
def doit(self, signal, seconds):
time.sleep(seconds)
return (signal,)
error_skip_flag = False
try:
import cm_global
def filter_message(str):
global error_skip_flag
if "IMPACT-PACK-SIGNAL: STOP CONTROL BRIDGE" in str:
return True
elif error_skip_flag and "ERROR:root:!!! Exception during processing !!!\n" == str:
error_skip_flag = False
return True
else:
return False
cm_global.try_call(api='cm.register_message_collapse', f=filter_message)
except Exception as e:
print(f"[WARN] ComfyUI-Impact-Pack: `ComfyUI` or `ComfyUI-Manager` is an outdated version.")
pass
def workflow_to_map(workflow):
nodes = {}
links = {}
for link in workflow['links']:
links[link[0]] = link[1:]
for node in workflow['nodes']:
nodes[str(node['id'])] = node
return nodes, links
class ImpactRemoteBoolean:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"node_id": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
"widget_name": ("STRING", {"multiline": False}),
"value": ("BOOLEAN", {"default": True, "label_on": "True", "label_off": "False"}),
}}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic/_for_test"
RETURN_TYPES = ()
OUTPUT_NODE = True
def doit(self, **kwargs):
return {}
class ImpactRemoteInt:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"node_id": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
"widget_name": ("STRING", {"multiline": False}),
"value": ("INT", {"default": 0, "min": -0xffffffffffffffff, "max": 0xffffffffffffffff}),
}}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic/_for_test"
RETURN_TYPES = ()
OUTPUT_NODE = True
def doit(self, **kwargs):
return {}
class ImpactControlBridge:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"value": (any_typ,),
"mode": ("BOOLEAN", {"default": True, "label_on": "Active", "label_off": "Stop/Mute/Bypass"}),
"behavior": (["Stop", "Mute", "Bypass"], ),
},
"hidden": {"unique_id": "UNIQUE_ID", "prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"}
}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Logic"
RETURN_TYPES = (any_typ,)
RETURN_NAMES = ("value",)
OUTPUT_NODE = True
DESCRIPTION = ("When behavior is Stop and mode is active, the input value is passed directly to the output.\n"
"When behavior is Mute/Bypass and mode is active, the node connected to the output is changed to active state.\n"
"When behavior is Stop and mode is Stop/Mute/Bypass, the workflow execution of the current node is halted.\n"
"When behavior is Mute/Bypass and mode is Stop/Mute/Bypass, the node connected to the output is changed to Mute/Bypass state.")
@classmethod
def IS_CHANGED(self, value, mode, behavior="Stop", unique_id=None, prompt=None, extra_pnginfo=None):
if behavior == "Stop":
return value, mode, behavior
else:
# NOTE: extra_pnginfo is not populated for IS_CHANGED.
# so extra_pnginfo is useless in here
try:
workflow = core.current_prompt['extra_data']['extra_pnginfo']['workflow']
except:
print(f"[Impact Pack] core.current_prompt['extra_data']['extra_pnginfo']['workflow']")
return 0
nodes, links = workflow_to_map(workflow)
next_nodes = []
for link in nodes[unique_id]['outputs'][0]['links']:
node_id = str(links[link][2])
impact.utils.collect_non_reroute_nodes(nodes, links, next_nodes, node_id)
return next_nodes
def doit(self, value, mode, behavior="Stop", unique_id=None, prompt=None, extra_pnginfo=None):
global error_skip_flag
if core.is_execution_model_version_supported:
from comfy_execution.graph import ExecutionBlocker
else:
print("[Impact Pack] ImpactControlBridge: ComfyUI is outdated. The 'Stop' behavior cannot function properly.")
if behavior == "Stop":
if mode:
return (value, )
else:
return (ExecutionBlocker(None), )
else:
workflow_nodes, links = workflow_to_map(extra_pnginfo['workflow'])
active_nodes = []
mute_nodes = []
bypass_nodes = []
for link in workflow_nodes[unique_id]['outputs'][0]['links']:
node_id = str(links[link][2])
next_nodes = []
impact.utils.collect_non_reroute_nodes(workflow_nodes, links, next_nodes, node_id)
for next_node_id in next_nodes:
node_mode = workflow_nodes[next_node_id]['mode']
if node_mode == 0:
active_nodes.append(next_node_id)
elif node_mode == 2:
mute_nodes.append(next_node_id)
elif node_mode == 4:
bypass_nodes.append(next_node_id)
if mode:
# active
should_be_active_nodes = mute_nodes + bypass_nodes
if len(should_be_active_nodes) > 0:
PromptServer.instance.send_sync("impact-bridge-continue", {"node_id": unique_id, 'actives': list(should_be_active_nodes)})
nodes.interrupt_processing()
elif behavior == "Mute" or behavior == True:
# mute
should_be_mute_nodes = active_nodes + bypass_nodes
if len(should_be_mute_nodes) > 0:
PromptServer.instance.send_sync("impact-bridge-continue", {"node_id": unique_id, 'mutes': list(should_be_mute_nodes)})
nodes.interrupt_processing()
else:
# bypass
should_be_bypass_nodes = active_nodes + mute_nodes
if len(should_be_bypass_nodes) > 0:
PromptServer.instance.send_sync("impact-bridge-continue", {"node_id": unique_id, 'bypasses': list(should_be_bypass_nodes)})
nodes.interrupt_processing()
return (value, )
class ImpactExecutionOrderController:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"signal": (any_typ,),
"value": (any_typ,),
}}
FUNCTION = "doit"
CATEGORY = "ImpactPack/Util"
RETURN_TYPES = (any_typ, any_typ)
RETURN_NAMES = ("signal", "value")
def doit(self, signal, value):
return signal, value
original_handle_execution = execution.PromptExecutor.handle_execution_error
def handle_execution_error(**kwargs):
execution.PromptExecutor.handle_execution_error(**kwargs)