Update Random_Project.py

This commit is contained in:
hexxacubic
2025-05-24 20:37:13 +02:00
committed by GitHub
parent 618d355cdb
commit 7a86ad0b4e
+26 -32
View File
@@ -4,27 +4,27 @@ import hashlib
class Random_Project:
"""
Random Project Node:
• entries: Multiline-String mit Einträgen im Format
• entries: multiline string containing entries in the format:
###X
positive Prompt-Zeilen
positive prompt lines
---
negative Prompt-Zeilen
für beliebige X (nicht zwangsläufig lückenlos).
• seed: String (beliebige Länge), wird per SHA256 → Integer gemappt.
• control_after_generate: "randomize" oder "seed"
- randomize: wähle echt zufällig (wenn seed gesetzt, initialisiere RNG damit)
- seed: deterministische Auswahl via (seed_int-1) % Anzahl_Einträge
negative prompt lines
for arbitrary X values (not necessarily contiguous).
• seed: integer seed (0 = no seed), adjustable via arrows or keyboard input
• control_after_generate: dropdown selection with arrows
- "randomize": choose a truly random entry (if seed is provided, RNG is seeded accordingly)
- "seed": deterministic selection using (seed - 1) % number_of_entries
Outputs:
• pos (STRING): positiver Prompt
• neg (STRING): negativer Prompt
• pos (STRING): selected positive prompt
• neg (STRING): selected negative prompt
"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"entries": ("STRING", {"multiline": True, "default": ""}),
"seed": ("STRING", {"default": ""}),
"entries": ("STRING", {"multiline": True, "default": ""}),
"seed": ("INT", {"default": 0, "min": 0}),
"control_after_generate": (
"STRING",
{
@@ -41,17 +41,17 @@ class Random_Project:
CATEGORY = "hexxacubic"
def random_project(self, entries, seed, control_after_generate):
# 1) Einträge parsen
# Parse the entries into sections keyed by integer identifiers
sections = {}
current = None
for raw in entries.splitlines():
line = raw.rstrip("\n")
for raw_line in entries.splitlines():
line = raw_line.rstrip("\n")
if not line.strip():
continue
if line.startswith("###"):
try:
num = int(line[3:])
current = num
section_id = int(line[3:])
current = section_id
sections[current] = {"pos": [], "neg": [], "in_neg": False}
except ValueError:
current = None
@@ -62,30 +62,24 @@ class Random_Project:
bucket = "neg" if sections[current]["in_neg"] else "pos"
sections[current][bucket].append(line)
# If no valid sections are found, return empty strings
if not sections:
return "", ""
# 2) Seed-String → Integer via SHA256 (0 bei leerem String)
if seed:
h = hashlib.sha256(seed.encode("utf-8")).hexdigest()
seed_int = int(h, 16)
else:
seed_int = 0
# sortierte Keys für Modulo-Logik
# Sort the available section keys
sorted_keys = sorted(sections.keys())
# 3) Auswahl
# Determine which section to choose
if control_after_generate == "randomize":
# wenn ein seed kommt, initialisiere RNG damit
if seed_int:
random.seed(seed_int)
# Seed the RNG if provided
if seed:
random.seed(seed)
idx = random.choice(sorted_keys)
else: # control_after_generate == "seed"
# (seed_int-1) mod N → wählt eine deterministische Position
idx = sorted_keys[(seed_int - 1) % len(sorted_keys)]
# Use modulo to wrap the seed to a valid index
idx = sorted_keys[(seed - 1) % len(sorted_keys)]
# 4) Prompt-Zeilen zusammensetzen
# Build the output prompts
chosen = sections[idx]
pos = "\n".join(chosen["pos"]).strip()
neg = "\n".join(chosen["neg"]).strip()