From 79d2d88de49dbfa791f4655fc2fc9684732180ed Mon Sep 17 00:00:00 2001 From: hexxacubic Date: Sat, 24 May 2025 20:09:06 +0200 Subject: [PATCH] Create Random_Project.py New Node --- Random_Project.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 Random_Project.py diff --git a/Random_Project.py b/Random_Project.py new file mode 100644 index 0000000..0308d13 --- /dev/null +++ b/Random_Project.py @@ -0,0 +1,81 @@ +import random + +class Random_Project: + """ + Random Project Node: + • Ein Textfeld “entries” mit Einträgen im Format: + ###1 + positive Prompt-Zeilen + --- + negative Prompt-Zeilen + + ###2 + … + • seed: ganzzahliger Seed für deterministische Auswahl (0 = kein Seed) + • index: feste Auswahl (wenn randomize=0) + • randomize: 0 = nutze index, 1 = wähle zufällig + Outputs: + • pos (STRING): ausgewählter positiver Prompt + • neg (STRING): zugehöriger negativer Prompt + """ + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "entries": ("STRING", {"multiline": True, "default": ""}), + "seed": ("INT", {"default": 0, "min": 0}), + "index": ("INT", {"default": 1, "min": 1}), + "randomize": ("INT", {"default": 1, "min": 0, "max": 1}), + } + } + + RETURN_TYPES = ("STRING", "STRING") + FUNCTION = "random_project" + OUTPUT_NODE = True + CATEGORY = "hexxacubic" + + def random_project(self, entries, seed, index, randomize): + # Parse entries nach ###Num / --- Struktur + sections = {} + current = None + for raw in entries.splitlines(): + line = raw.rstrip("\n") + if not line.strip(): + continue + if line.startswith("###"): + try: + num = int(line[3:]) + current = num + sections[current] = {"pos": [], "neg": [], "in_neg": False} + except ValueError: + current = None + elif current is not None: + if line.strip() == "---": + sections[current]["in_neg"] = True + else: + if sections[current]["in_neg"]: + sections[current]["neg"].append(line) + else: + sections[current]["pos"].append(line) + + if not sections: + return "", "" + + # Seed setzen, falls gewünscht + if seed: + random.seed(seed) + + # Auswahl des Index + if randomize == 1: + idx = random.choice(list(sections.keys())) + else: + idx = index + + chosen = sections.get(idx) + if not chosen: + return "", "" + + pos = "\n".join(chosen["pos"]).strip() + neg = "\n".join(chosen["neg"]).strip() + return pos, neg