File size: 2,738 Bytes
5b97e57 | 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 | from .utils import FlexibleOptionalInputType
import re
class DynamicStringConcat:
"""
Eine dynamische String-Concat-Node, die automatisch neue Inputs hinzufügt,
sobald alle vorhandenen Inputs belegt sind, ähnlich wie die rgthree Any Switch Node.
"""
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
"""
Definiert die Input-Typen für die Node.
Die Anzahl der Inputs wird dynamisch erweitert.
"""
return {
"required": {
"separator": ("STRING", {"default": " ", "multiline": False}),
"ignore_empty": ("BOOLEAN", {"default": True}),
},
"optional": FlexibleOptionalInputType("STRING")
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("concatenated_string",)
FUNCTION = "concatenate_strings"
CATEGORY = "utils"
OUTPUT_NODE = False
def concatenate_strings(self, separator=" ", ignore_empty=True, **kwargs):
"""
Führt alle bereitgestellten String-Inputs zusammen.
Args:
separator (str): Trennzeichen zwischen den Strings
ignore_empty (bool): Ob leere Strings ignoriert werden sollen
**kwargs: Alle Input-Strings
Returns:
tuple: (zusammengeführter String)
"""
# Sammle alle Input-Strings
inputs = []
# Sortiere die Inputs numerisch, um die korrekte Reihenfolge sicherzustellen
sorted_items = sorted(kwargs.items(), key=lambda item: int(item[0].split('_')[1]) if item[0].startswith("input_") else -1)
for key, value in sorted_items:
if key.startswith("input_") and value is not None:
if isinstance(value, (list, tuple)):
# Falls es ein Array ist, nimm das erste Element
value = value[0] if len(value) > 0 else ""
# Wandle zu String um
str_value = str(value).strip()
# Füge hinzu, wenn nicht leer oder wenn leere Strings nicht ignoriert werden
if str_value or not ignore_empty:
inputs.append(str_value)
# Führe alle Strings zusammen
result = separator.join(inputs)
print(f"Dynamic String Concat: Concatenated {len(inputs)} inputs: '{result}'")
return (result,)
# Node-Mappings für ComfyUI
NODE_CLASS_MAPPINGS = {
"DynamicStringConcat": DynamicStringConcat
}
NODE_DISPLAY_NAME_MAPPINGS = {
"DynamicStringConcat": "Dynamic String Concat"
}
|