-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnodes.py
More file actions
1308 lines (1006 loc) · 54.4 KB
/
nodes.py
File metadata and controls
1308 lines (1006 loc) · 54.4 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
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import subprocess
import inspect
import importlib
import re
import json
DEBUG_MODE = False
CLASS_WARNING_STRING = """
>>>>>> WARNING <<<<<<
You have not changed the class name of 'SilverRunPythonCode'.
This means bad actors can make you run malicious code by infiltrating this node into a workflow that you might download and run without checking if it contains this node with malicious code in it.
To solve this security flaw you need to do the following:
- Create a ~10 length random string with (a-Z,0-9) characters (ex: f8Rca4Cx)
- Add the random string as suffix to the class name in the following lines in \custom_nodes\ComfyUI-RunPythonCode\ nodes.py:
- class SilverRunPythonCode: (line 808)
- "SILVER.SilverRunPythonCode": SilverRunPythonCode, (line 1220)
- "SILVER.SilverRunPythonCode": "[Silver] Run/Execute Python Code", (line 1230)
Ex:
- class SilverRunPythonCodef8Rca4Cx:
- "SILVER.SilverRunPythonCodef8Rca4Cx": SilverRunPythonCodef8Rca4Cx,
- "SILVER.SilverRunPythonCodef8Rca4Cx": "[Silver] Run/Execute Python Code",
With this, the security of this node is no longer a concern when downloading and running shared workflows - unless the bad actor somehow guesses your suffix correctly.
Avoid ever sharing an output with metadata/workflow with someone else if it contains your run python node in it because that will reveal its class name.
-------------------------------------\n\n\n
"""
SHARING_DATA_INFO_STRING = """
---------- Sharing Data in "Run Python Code" Nodes ----------
Your "Run Python Code" node offers two ways to share Python variables, functions, and objects: shared_globals and shared_locals.
shared_globals (Universal Access)
What it is: A global container for anything you want any "Run Python Code" node in your ComfyUI session to access, regardless of connections or workflow.
How it works: At the start of your code, shared_globals is an empty dictionary. Its purpose is to assign data into it that will then become globally available.
To assign a global item:
def my_utility_function():
return "Hello from global!"
shared_globals['my_func'] = my_utility_function
shared_globals['version'] = 1.0
To access a global item in another "Run Python Code" node: Once assigned in one node, you MUST directly use the item by its name in any other node:
# In a different node, after the first one has run:
print(my_func()) # Outputs: Hello from global!
print(version) # Outputs: 1.0
To remove a global item use the 'remove_global_item(key: str) -> bool' function:
success = remove_global_item("item_name") # item will still be available in the scope of the node until the end of its execution
Key Point: If you update a global item, the change is reflected everywhere in subsequent executions.
shared_locals (Connected Node Access)
What it is: A container specifically for passing data between directly connected "Run Python Code" nodes.
How it works: If a previous "Run Python Code" node is connected to this node's shared_locals input, this dictionary will contain any items that node decided to share. If no node is connected, shared_locals will be empty.
To assign a local item for the next connected node:
def process_data(data):
return data + "_processed"
shared_locals['processor'] = process_data
shared_locals['intermediate_result'] = "some_data"
To access a local item from the previous connected node:
# In the next connected node:
result = processor("my_input") # Uses 'processor' from the previous node
print(intermediate_result) # Uses 'intermediate_result' from the previous node
Key Point: shared_locals only travels along the specific connection between nodes. It doesn't affect other parts of your workflow or persist globally.
"""
# --- Global Registries ---
_EXEC_GLOBALS = {}
_GLOBAL_FUNCTIONS_REGISTRY = {}
_GLOBAL_FUNCTION_DISPLAY_STRINGS = []
_GLOBAL_OBJECTS_REGISTRY = {}
_GLOBAL_OBJECT_DISPLAY_STRINGS = []
TOP_MODULE_IMPORTS = {}
TOP_MODULE_IMPORT_STRING = ""
EXTRA_MODULE_IMPORT_STRING = ""
SHARED_GLOBALS = {}
def global_func(func):
"""
A decorator to mark functions as 'global_func' and register them.
Functions decorated with @global_func will automatically be added to
_GLOBAL_FUNCTIONS_REGISTRY.
"""
if not callable(func):
raise TypeError(f"Decorator 'global_func' can only be applied to functions, got {type(func)}")
_GLOBAL_FUNCTIONS_REGISTRY[func.__name__] = func
# Use inspect.signature to get the function's signature
signature = inspect.signature(func)
# Format it as a string: "function_name(param1: type, ...) -> return_type"
# We include the function name here for clarity in the display string
_GLOBAL_FUNCTION_DISPLAY_STRINGS.append(f"{func.__name__}{signature}\n")
return func
def global_obj(name, obj):
"""
Registers a top-level object as 'global_obj'.
This function should be called immediately after the object's definition.
"""
if not isinstance(name, str):
raise TypeError("The 'name' argument for global_object must be a string.")
_GLOBAL_OBJECTS_REGISTRY[name] = obj
# Get the type name (e.g., 'int', 'str', 'list')
type_name = type(obj).__name__
_GLOBAL_OBJECT_DISPLAY_STRINGS.append(f"{name} ({type_name})\n")
return obj # Return the object so you can still assign it normally
@global_func
def remove_global_item(key: str) -> bool:
"""
Removes an entry from the global SHARED_GLOBALS dictionary.
Args:
key (str): The key of the item to be removed.
Returns:
A bool that represents removal success
"""
global SHARED_GLOBALS
if key in SHARED_GLOBALS:
del SHARED_GLOBALS[key]
print(f"[SilverRunPythonCode] Successfully removed '{key}' from SHARED_GLOBALS.")
return True
print(f"[SilverRunPythonCode] Warning: Key '{key}' not found in SHARED_GLOBALS. Nothing removed.")
return False
NODE_FILE = global_obj("NODE_FILE", os.path.abspath(__file__)) # leaving this just as an example on how to decorate variables from within the script
import math
import random
import copy
import traceback
from pathlib import Path
from typing import TypedDict, Dict, Tuple
import numpy as np
import torch
import PIL
from PIL import Image, ImageOps, ImageFont, ImageDraw
import comfy
import folder_paths
_packages_to_check = [
("cv2", "cv2"),
("diffusers", "diffusers"),
("ffmpeg", "ffmpeg"),
("ffmpegcv", "ffmpegcv"),
("kornia", "kornia"),
("mediapipe", "mediapipe"),
("mmcv", "mmcv"),
("moviepy", "moviepy"),
("natsort", "natsort"),
("ninja", "ninja"),
("onnx", "onnx"),
("onnxruntime", "onnxruntime"),
("pandas", "pd"),
("pydub", "pydub"),
("rembg", "rembg"),
("requests", "requests"),
("safetensors", "safetensors"),
("scipy", "scipy"),
("sklearn", "sklearn"),
("tokenizers", "tokenizers"),
("torchaudio", "torchaudio"),
("tqdm", "tqdm"),
("transformers", "transformers"),
("translators", "translators"),
("transparent_background", "transparent_background"),
("ultralytics", "ultralytics"),
("yaml", "yaml"),
]
_import_status_lines = []
for actual_module_name, desired_alias in _packages_to_check:
try:
module = importlib.import_module(actual_module_name)
# Assign the imported module to the desired alias in the global namespace
globals()[desired_alias] = module
_import_status_lines.append("import " + actual_module_name + ("" if actual_module_name == desired_alias else " as " + desired_alias) + "\n")
except:
pass
EXTRA_MODULE_IMPORT_STRING = "".join(_import_status_lines)
def get_top_level_imports(script_lines):
"""
Detects top-level imports, and parses them into a dictionary.
The dictionary's keys are the import lines as they appear in the script.
The values are dictionaries where keys are the imported names (or aliases)
and values are the actual imported modules or objects.
"""
top_level_imports = {}
i = 0
while i < len(script_lines):
line = script_lines[i].strip()
# Skip empty lines and comments
if not line or line.startswith('#'):
i += 1
continue
# Handle 'import module' or 'import module as alias' or 'import module1, module2'
# This regex is modified to capture the *entire import list* in group 1,
# then we'll parse that list. It now correctly handles trailing comments.
match_simple_import = re.match(r'^\s*import\s+([a-zA-Z_][a-zA-Z0-9_]*(?:\s+as\s+[a-zA-Z_][a-zA-Z0-9_]*)?(?:\s*,\s*[a-zA-Z_][a-zA-Z0-9_]*(?:\s+as\s+[a-zA-Z_][a-zA-Z0-9_]*)?)*)\s*(?:#.*)?$', line)
if match_simple_import:
# The full import line, including the "import" keyword
full_import_line = match_simple_import.group(0).strip() # Use group(0) for the whole match
# Extract just the modules part from the matched line (e.g., "os, math as m")
modules_part = match_simple_import.group(1)
modules = {}
parts = modules_part.split(',')
for part in parts:
part = part.strip()
if not part: # Handle empty parts from extra commas like "import os,, math"
continue
module_name = part
alias = None
if ' as ' in part:
module_name, alias = part.split(' as ')
module_name = module_name.strip()
alias = alias.strip()
# The actual name to attempt importing (e.g., 'os', 'math')
actual_import_name = module_name
try:
# Attempt to import the module
imported_module = importlib.import_module(actual_import_name)
# Store it under its alias or original name
modules[alias if alias else actual_import_name] = imported_module
except ImportError:
print(f"Warning: Could not import module '{actual_import_name}'. It will not be available in SilverRunPythonCode.")
pass # Continue processing other imports
except Exception as e:
print(f"Warning: An unexpected error occurred while importing '{actual_import_name}': {e}")
pass
if modules: # Only add to top_level_imports if something was successfully imported
top_level_imports[full_import_line] = modules
i += 1
continue
# Handle 'from module import name, name2' or 'from module import (name, name2)'
# This regex handles single-line and multi-line 'from import' statements.
# It's now more robust for trailing comments.
match_from_import_start = re.match(r'^\s*(from\s+([a-zA-Z_][a-zA-Z0-9_.]*)\s+import\s+.*)\s*(?:#.*)?$', line)
if match_from_import_start:
module_name_base = match_from_import_start.group(2) # The module being imported from (e.g., 'PIL', 'os.path')
current_import_line_parts = [match_from_import_start.group(1)] # Store the raw import part for full_import_line
# Basic check for multi-line continuation (improved)
# Check if the line itself ends with a comma, backslash, or is within parentheses
is_multiline_continuation = line.strip().endswith(',') or \
line.strip().endswith('\\') or \
('(' in line and ')' not in line) # Starts with ( but not ends with )
j = i + 1
while j < len(script_lines) and is_multiline_continuation:
next_line = script_lines[j].strip()
if not next_line or next_line.startswith('#'):
break # Stop if we hit an empty line or comment
current_import_line_parts.append(next_line)
# Update continuation flag based on the new current line
is_multiline_continuation = next_line.endswith(',') or \
next_line.endswith('\\') or \
('(' in "".join(current_import_line_parts) and ')' not in "".join(current_import_line_parts))
# Break if current line ends the parentheses block
if next_line.endswith(')') and '(' in "".join(current_import_line_parts):
is_multiline_continuation = False # We've found the closing parenthesis
j += 1
full_import_line_raw = "\n".join(current_import_line_parts)
full_import_line_display = " ".join([p.strip() for p in current_import_line_parts]) # For display key
# Attempt to parse the full import line using ast (more robust than regex for internal parts)
# If ast fails, fallback to your regex parsing, or simplify.
# For simplicity with the current regex approach, let's keep the regex parsing,
# but ensure we strip comments from line parts.
# Reconstruct the import clause by joining parts and stripping comments
import_clause_full = " ".join([re.sub(r'#.*$', '', p).strip() for p in current_import_line_parts])
import_clause = import_clause_full[import_clause_full.find("import") + len("import"):].strip()
# Remove parentheses for parsing if present
if import_clause.startswith('(') and import_clause.endswith(')'):
import_clause = import_clause[1:-1].strip()
imported_names = {}
names = import_clause.split(',')
for name_part in names:
name_part = name_part.strip()
if not name_part:
continue
original_name = name_part
alias = None
if ' as ' in name_part:
original_name, alias = name_part.split(' as ')
original_name = original_name.strip()
alias = alias.strip()
try:
module = importlib.import_module(module_name_base)
if hasattr(module, original_name):
if alias:
imported_names[alias] = getattr(module, original_name)
else:
imported_names[original_name] = getattr(module, original_name)
else:
print(f"Warning: Object '{original_name}' not found in module '{module_name_base}'.")
except (ImportError, AttributeError) as e:
print(f"Warning: Could not import '{original_name}' from '{module_name_base}': {e}. It will not be available in SilverRunPythonCode.")
pass # Handle cases where module/attribute might not be available or not found
except Exception as e:
print(f"Warning: An unexpected error occurred while importing '{original_name}' from '{module_name_base}': {e}")
pass
if imported_names:
top_level_imports[full_import_line_display] = imported_names
i = j # Move the index past the multi-line import
continue
i += 1
return top_level_imports
script_lines = []
with open(NODE_FILE, 'r', encoding='utf-8') as f:
script_lines = f.readlines()
TOP_MODULE_IMPORTS = get_top_level_imports(script_lines)
TOP_MODULE_IMPORT_STRING = "\n".join([f"{key}" for key in TOP_MODULE_IMPORTS])
class AnyType(str):
def __ne__(self, __value: object) -> bool:
return False
# Our any instance wants to be a wildcard string
any = AnyType("*")
# Tensor to PIL
@global_func
def tensor2pil(image):
return Image.fromarray(np.clip(255. * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8))
# PIL to Tensor
@global_func
def pil2tensor(image):
return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)
# Numpy to PIL
@global_func
def numpy2pil(image):
return Image.fromarray(np.clip(255. * image.squeeze(), 0, 255).astype(np.uint8))
@global_func
def loadPil(path):
return Image.open(path)
@global_func
def dynamic_prompts(prompt: str, seed: int, line_suffix: str = "", single_line_output: bool = True, remove_whitespaces: bool = True, remove_empty_tags: bool = True, wildcard_dir: str = "") -> str:
# Updated _fix_prompt signature and logic
def _fix_prompt(
prompt: str,
line_suffix: str,
single_line_output: bool,
remove_whitespaces: bool,
remove_empty_tags: bool,
) -> str:
"""
Processes the prompt by:
1. Removing comments.
2. Applying line suffix and optionally trimming (based on remove_whitespaces).
3. Combining lines (based on single_line_output).
4. Applying default prompt cleaning (e.g., ",," -> ",").
5. Optionally removing empty tags (based on remove_empty_tags).
Args:
prompt (str): The initial string.
line_suffix (str): String to append to each line.
single_line_output (bool): If True, joins lines with a space; otherwise, joins with a newline.
remove_whitespaces (bool): If True, strips lines and removes empty ones.
remove_empty_tags (bool): If True, removes redundant separators like ' , ,' or ' , .'
Returns:
str: The modified string.
"""
# --- Start of Modified Preprocessing Code ---
cleaned_lines = []
lines = prompt.splitlines()
for line in lines:
# Find the index of the first '#' character (comment delimiter)
comment_start_index = line.find('#')
if comment_start_index != -1:
line_without_comment = line[:comment_start_index]
else:
line_without_comment = line
# Apply trimming if remove_whitespaces is True
trimmed_line = line_without_comment.strip() if remove_whitespaces else line_without_comment
if remove_whitespaces:
while (" " in trimmed_line):
trimmed_line = trimmed_line.replace(" ", " ")
# Apply the specified line_suffix
if trimmed_line:
# Only add suffix if the line is not empty after stripping
final_line = trimmed_line + line_suffix
# Only add non-empty lines to the cleaned list
cleaned_lines.append(final_line)
# Convert the cleaned lines back into a single/multi-line string
# Join with " " for single line output, or "\n" for multi-line output
joiner = " " if single_line_output else "\n"
prompt = joiner.join(cleaned_lines)
# --- End of Modified Preprocessing Code ---
# Default cleaning replacements
replacements = {}
replacements[" ,"] = ","
replacements[", "] = ", "
replacements[" ."] = "."
replacements[". "] = ". "
replacements[".,"] = "."
replacements[",."] = ","
replacements[",,"] = ","
replacements[".."] = "."
empty_tag_replacements = [".,", ",.", ",,", ".."]
# Sort replacements by key length in descending order
sorted_replacements = sorted(replacements.items(), key=lambda item: len(item[0]), reverse=True)
# The replacement loop runs until no changes are made.
while True:
replacement_made_in_pass = False
current_prompt_state = prompt
for old_substring, new_substring in sorted_replacements:
if not remove_empty_tags and old_substring in empty_tag_replacements:
continue
temp_prompt = current_prompt_state
pattern = re.compile(re.escape(old_substring), re.IGNORECASE)
replacements_to_make_in_this_pass = []
for match in pattern.finditer(temp_prompt):
start, end = match.span()
# Check if this match is inside any <...> tag
tag_start_index = temp_prompt.rfind('<', 0, start)
if tag_start_index != -1:
tag_end_index = temp_prompt.find('>', tag_start_index)
if tag_end_index != -1 and tag_end_index > start:
continue
replacements_to_make_in_this_pass.append((start, end, new_substring))
# Apply replacements from right to left
for start, end, new_sub in sorted(replacements_to_make_in_this_pass, key=lambda x: x[0], reverse=True):
current_prompt_state = current_prompt_state[:start] + new_sub + current_prompt_state[end:]
replacement_made_in_pass = True
if not replacement_made_in_pass:
break
prompt = current_prompt_state
# --- Logic for remove_empty_tags ---
if remove_empty_tags:
temp_prompt = prompt
# Simple cleanup of spacing before running the final delimiter removal
temp_prompt = temp_prompt.replace(", ", ",").replace(" ,", ",").replace(" .", ".").replace(". ", ".")
temp_prompt = temp_prompt.replace(",", ", ")
temp_prompt = re.sub(r'\.(?!\d)', '. ', temp_prompt) # replaces '.' -> '. ' Only if there is no immediate digit after the dot
# Use a loop to remove sequences of a delimiter, optional space, and another delimiter.
while True:
initial_len = len(temp_prompt)
# Replace pattern (separator, optional space, separator) with a single separator
# e.g., ', , ' -> ', '
temp_prompt = re.sub(r'([.,])\s*([.,])', r'\1 ', temp_prompt)
if len(temp_prompt) == initial_len:
break
# Final cleaning of delimiters (e.g. 'cat,, dog' -> 'cat, dog')
temp_prompt = temp_prompt.replace(",,", ",").replace("..", ".")
prompt = temp_prompt
prompt = prompt.strip()
# The existing loop to remove leading/trailing delimiters/spaces
while prompt.startswith(",") or prompt.startswith(".") or prompt.startswith(" ") or prompt.endswith(",") or prompt.endswith(" "):
try:
if prompt.startswith(",") or prompt.startswith(".") or prompt.startswith(" "):
prompt = prompt[1:].strip() # Strip again after removing
if prompt.endswith(",") or prompt.endswith(" "):
prompt = prompt[:-1].strip() # Strip again after removing
except:
break
return prompt
def _process_wildcards(prompt: str, wildcard_dir: str, seed: int) -> str:
"""
Replaces substrings like '__something__' in the prompt with the content of
the corresponding '.txt' file.
If the file contains multiple lines:
1. Empty lines and comment lines (#...) are ignored.
2. One line is randomly selected and returned.
This ensures that only one item (which may contain further dynamic syntax) is
substituted, regardless of whether combination syntax is present in the file.
Args:
prompt (str): The input string potentially containing wildcard substrings.
wildcard_dir (str): The directory to search for wildcard '.txt' files.
seed (int): An integer seed for the random number generator.
Returns:
str: The prompt string with wildcards replaced by a single selected line.
"""
if wildcard_dir is None or not os.path.isdir(wildcard_dir):
return prompt
# Seed the random number generator for wildcard selection
random.seed(seed)
# Regex to find '__something__' or '__something.txt__'
pattern = re.compile(r'__(.+?)__')
def case_insensitive_resolve(base_dir: str, path_parts: list[str]) -> str | None:
"""
Resolves a nested path inside base_dir in a case-insensitive way.
Only lists contents one level at a time (no recursion).
Returns absolute path to the file if found, else None.
"""
current_dir = base_dir
for part in path_parts[:-1]:
try:
entries = os.listdir(current_dir)
except OSError:
return None
match = next((e for e in entries if e.lower() == part.lower() and
os.path.isdir(os.path.join(current_dir, e))), None)
if not match:
return None
current_dir = os.path.join(current_dir, match)
# Last part should be a file (case-insensitive match for .txt)
target_file = path_parts[-1]
try:
entries = os.listdir(current_dir)
except OSError:
return None
for e in entries:
base_name, ext = os.path.splitext(e)
if ext.lower() == '.txt' and base_name.lower() == target_file.lower():
return os.path.join(current_dir, e)
return None
def replace_match(match):
wildcard_name = match.group(1).strip()
if wildcard_name.lower().endswith('.txt'):
wildcard_name = wildcard_name[:-4]
# Normalize separators and split into parts
normalized = re.sub(r'[\\/]+', '/', wildcard_name)
parts = [p for p in normalized.split('/') if p]
if not parts:
return match.group(0)
# Resolve path case-insensitively
filepath = case_insensitive_resolve(wildcard_dir, parts)
if not filepath or not os.path.isfile(filepath):
return match.group(0)
try:
with open(filepath, 'r', encoding='utf-8') as f:
file_content = f.read()
# Filter lines (ignore empty and comment lines)
lines = []
for line in file_content.splitlines():
trimmed = line.strip()
if trimmed and not trimmed.startswith('#'):
comment_idx = trimmed.find('#')
if comment_idx != -1:
trimmed = trimmed[:comment_idx].strip()
if trimmed:
lines.append(trimmed)
if not lines:
return ""
# Choose one random valid line
return random.choice(lines)
except Exception as e:
print(f"Error reading file {filepath}: {e}")
return match.group(0)
return pattern.sub(replace_match, prompt)
def _process_combinations(prompt: str, seed: int) -> str:
"""
Replaces substrings enclosed in '{...}' with a randomly selected choice
from their pipe-separated contents.
"""
# Seed the random number generator
random.seed(seed)
pattern = re.compile(r'{([^}{]*)}')
while True:
match = pattern.search(prompt)
if not match:
break
start, end = match.span()
choices_str = match.group(1)
# --- Parse choices and weights ---
processed_lines = []
for line in choices_str.splitlines(): # Remove comments and empty lines inside combinations ---
stripped = line.strip()
if not stripped or stripped.startswith('#'): # Ignore full-line comments completely
continue
comment_idx = stripped.find('#') # Remove inline comments
if comment_idx != -1:
stripped = stripped[:comment_idx]
processed_lines.append(stripped)
recombined = "\n".join(processed_lines)
raw_choices_list = [c.strip() for c in recombined.split('|')]
weighted_choices = []
unweighted_choices = []
total_defined_weight = 0.0
for item in raw_choices_list:
if '::' in item:
try:
weight_str, choice_text = item.split('::', 1)
weight = float(weight_str)
if not (0 <= weight <= 1):
raise ValueError("Weight must be between 0 and 1.")
weighted_choices.append((choice_text, weight))
total_defined_weight += weight
except ValueError:
unweighted_choices.append(item)
else:
unweighted_choices.append(item)
if total_defined_weight > 1.0:
for i in range(len(weighted_choices)):
choice, weight = weighted_choices[i]
weighted_choices[i] = (choice, weight / total_defined_weight)
total_defined_weight = 1.0
remaining_weight = 1.0 - total_defined_weight
if unweighted_choices:
if remaining_weight < 0:
remaining_weight = 0
equal_share_for_unweighted = remaining_weight / len(unweighted_choices)
for choice_text in unweighted_choices:
weighted_choices.append((choice_text, equal_share_for_unweighted))
# --- Perform selection ---
selected_choice = ""
if not weighted_choices:
selected_choice = ""
else:
choices_list = [item[0] for item in weighted_choices]
weights_list = [item[1] for item in weighted_choices]
selected_choice = random.choices(choices_list, weights=weights_list, k=1)[0]
# Replace the matched inner block with the selected choice
prompt = prompt[:start] + selected_choice + prompt[end:]
return prompt
# --- Main function body: Fix applied here ---
max_proccess_count = 30
while max_proccess_count > 0:
has_wildcards = "__" in prompt
has_combinations = "{" in prompt or "}" in prompt
if not has_wildcards and not has_combinations:
break # Exit the loop if no more dynamic content is found
# Process wildcards recursively (NO _fix_prompt call here)
if has_wildcards:
max_subproccess_count = 10
while max_subproccess_count > 0:
if "__" in prompt:
prompt = _process_wildcards(prompt, wildcard_dir, seed)
else:
break
max_subproccess_count -= 1
# Process combinations recursively (NO _fix_prompt call here)
if has_combinations:
max_subproccess_count = 30
while max_subproccess_count > 0:
if "{" in prompt or "}" in prompt:
prompt = _process_combinations(prompt, seed)
else:
break
max_subproccess_count -= 1
max_proccess_count -= 1
# 1. FINAL CLEANING: Run _fix_prompt ONCE on the fully resolved string
prompt = _fix_prompt(
prompt=prompt,
line_suffix=line_suffix,
single_line_output=single_line_output,
remove_whitespaces=remove_whitespaces,
remove_empty_tags=remove_empty_tags
)
return prompt
class SilverAnyBridge:
@classmethod
def INPUT_TYPES(cls):
return {
"optional": {
"any0": (any, ),
"any1": (any, ),
"any2": (any, ),
"any3": (any, ),
"any4": (any, ),
"any5": (any, ),
"any6": (any, ),
"any7": (any, ),
"sort_by_None": ("BOOLEAN", { "default": False, "tooltip": "When True: None inputs will be outputed last."}),
},
}
RETURN_TYPES = (any, any, any, any, any, any, any, any, )
RETURN_NAMES = ("any0", "any1", "any2", "any3", "any4", "any5", "any6", "any7", )
FUNCTION = "main"
CATEGORY = "silver"
DESCRIPTION = """
Takes up to 8 inputs of any type and outputs them in the same order.
Useful to ensure that certain nodes only run after receiving the (unrequired) outputs from other nodes.
Ex: making a grid of images from a given folder path but only after inference took place (and saved the output into the same folder) in order to ensure the grid contains the generated image.
IMPORTANT:
When 'sort_by_None' is True -> None inputs will be added after non-None inputs.
"""
def main(self, any0=None, any1=None, any2=None, any3=None, any4=None, any5=None, any6=None, any7=None, sort_by_None=False):
if sort_by_None:
l = [e for e in [any0, any1, any2, any3, any4, any5, any6, any7] if e is not None]
while len(l) < 8:
l.append(None)
return (l[0], l[1], l[2], l[3], l[4], l[5], l[6], l[7], )
return (any0, any1, any2, any3, any4, any5, any6, any7, )
class SilverRunPythonCode:
"""
A ComfyUI node that takes a list and a Python code string as input,
executes the code against a copy of the list, and outputs the modified list.
This node allows for dynamic python code execution within ComfyUI workflows.
"""
def __init__(s):
pass
@classmethod
def INPUT_TYPES(s):
GLOBAL_FUNCTION_DISPLAY_STRING = "\n\nThese are the natively supported functions:\n\n" + "".join(_GLOBAL_FUNCTION_DISPLAY_STRINGS) if len(_GLOBAL_FUNCTION_DISPLAY_STRINGS) > 0 else ""
GLOBAL_OBJECT_DISPLAY_STRING = "\n\nThese are the natively supported objects (you can only access their initial value):\n\n" + "".join(_GLOBAL_OBJECT_DISPLAY_STRINGS) if len(_GLOBAL_OBJECT_DISPLAY_STRINGS) > 0 else ""
CLASS_WARNING_STR = CLASS_WARNING_STRING if s.__name__ == "SilverRunPythonCode" and not DEBUG_MODE else ""
return {
"optional": {
"list_input": ("LIST", {"default": None}),
"shared_locals": ("DICT", {"default": None}), # Made optional to allow node to be used standalone
"deepcopy": ("BOOLEAN", { "default": True }),
"python_code": (
"STRING",
{
"multiline": True,
"default": (
'"""' +
CLASS_WARNING_STR +
"\nTIP: copy all of this into a big note node and leave it next to this one for reference.\n" +
"These are the natively added imports by this node:\n\n" +
TOP_MODULE_IMPORT_STRING + "\n" +
EXTRA_MODULE_IMPORT_STRING +
GLOBAL_FUNCTION_DISPLAY_STRING +
GLOBAL_OBJECT_DISPLAY_STRING +
"\n\n---------- INSTRUCTIONS: ----------\n\n"
"The 'list_input' variable is available here.\n"
"Modify 'list_input' in place, e.g.:\n\n"
"list_input[0] = 'new_first_item' # do NOT use return at top-level in your code!\n\n"
"You can import standard and venv Python packages:\n\n"
"import math\n"
"import numpy as np\n"
"from PIL import ImageColor\n" +
SHARING_DATA_INFO_STRING +
'"""\n# ---------- BEGIN YOUR CODE ----------\n\n\n'
),
},
),
}
}
RETURN_TYPES = ("LIST", "DICT",)
RETURN_NAMES = ("list_input", "shared_locals",)
OUTPUT_NODE = True
FUNCTION = "execute"
CATEGORY = "silver" # Categorize your node for better organization
DESCRIPTION = CLASS_WARNING_STRING if __name__ == "SilverRunPythonCode" and not DEBUG_MODE else "Use [Silver] List Append to import inputs and [Silver] List Splitter or [Silver] List Select/Extract By Index to extract outputs from 'list_input'."
def execute(self, python_code, deepcopy=True, list_input=None, shared_locals=None):
"""
Executes the user-provided Python code and handles shared items.
Args:
list_input: The input list (or any object).
python_code: The Python code string to execute.
shared_locals: A dictionary of functions, classes, and variables
received from another node.
Returns:
A tuple containing the modified list and the dictionary of shared items.
"""
if list_input is None:
list_input = []
if shared_locals is None:
shared_locals = {}
if not isinstance(list_input, list):
raise ValueError("'list_input' is not a list!")
if not isinstance(shared_locals, dict):
raise ValueError("'shared_locals' is not a dict!")
if self.__class__.__name__ == "SilverRunPythonCode" and not DEBUG_MODE: # spam the user until they fix the security flaw of this node and prevent code execution
print(CLASS_WARNING_STRING + "\n[SilverRunPythonCode] 'python_code' execution will be skipped! Returning original inputs...")
return (list_input, shared_locals)
if deepcopy:
# Create a deep copy of the input list.
# This is crucial for two reasons:
# 1. Prevents accidental modification of the original object connected from another node,
# which could lead to unexpected side effects in the workflow.
# 2. Ensures that if the user's code fails, the original list remains untouched,
# allowing the node to return a stable (unmodified) output.
try:
# Attempt to deep copy. If list_input is not copyable (e.g., a simple int or a dict with unhashable keys),
# copy.deepcopy will return it directly or raise an error.
# In general, this is a robust way to handle various types.
current_list_data = copy.deepcopy(list_input)
except TypeError as e:
print(f"[SilverRunPythonCode] Warning: Could not deep copy input type {type(list_input)}. Proceeding with direct reference. Error: {e}")
current_list_data = list_input
except Exception as e:
print(f"[SilverRunPythonCode] Error during deep copy: {e}. Returning original input.")
return (list_input, shared_locals)
# 'exec_globals' ensures that commonly used modules like torch, numpy, and PIL
# are available in the global scope of the executed code.
# This helps resolve NameErrors when functions in the user's code try to
# access objects from these libraries (e.g., Image from PIL).
exec_globals = {}
global _EXEC_GLOBALS
if len(_EXEC_GLOBALS) == 0:
for import_line, imported_data in TOP_MODULE_IMPORTS.items():
for imported_module_name, imported_module in imported_data.items():
_EXEC_GLOBALS[imported_module_name] = imported_module
for actual_module_name, desired_alias in _packages_to_check:
if globals().get(desired_alias):
_EXEC_GLOBALS[desired_alias] = globals().get(desired_alias)
for function_name in _GLOBAL_FUNCTIONS_REGISTRY:
_EXEC_GLOBALS[function_name] = _GLOBAL_FUNCTIONS_REGISTRY[function_name]
for object_name in _GLOBAL_OBJECTS_REGISTRY:
_EXEC_GLOBALS[object_name] = _GLOBAL_OBJECTS_REGISTRY[object_name]
# attempt imports from imports in the node
if "import " in python_code:
try:
node_imports = get_top_level_imports(python_code.splitlines())
if len(node_imports) > 0:
for import_line, imported_data in node_imports.items():
for imported_module_name, imported_module in imported_data.items():
if imported_module_name not in _EXEC_GLOBALS:
_EXEC_GLOBALS[imported_module_name] = imported_module
print(f"[SilverRunPythonCode] Successfully imported: {imported_module_name}")