This commit is contained in:
justumen
2024-09-17 22:05:03 +02:00
parent c19c417611
commit 7a703f1113
4 changed files with 30 additions and 9 deletions

View File

@@ -11,6 +11,7 @@ class WriteTextAdvanced:
"text": ("STRING", {"multiline": True, "lines": 10}),
},
"optional": {
"variables": ("STRING", {"multiline": True, "lines": 5}),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
},
}
@@ -21,9 +22,10 @@ class WriteTextAdvanced:
OUTPUT_NODE = True
CATEGORY = "Bjornulf"
def write_text_special(self, text, seed=None):
def write_text_special(self, text, variables="", seed=None):
logging.info(f"Raw text: {text}")
# If seed is not provided, generate a new one
logging.info(f"Variables: {variables}")
if len(text) > 10000:
return ("Text too large to process at once. Please split into smaller parts.",)
@@ -32,17 +34,30 @@ class WriteTextAdvanced:
random.seed(seed)
# Parse variables
var_dict = {}
for line in variables.split('\n'):
if '=' in line:
key, value = line.split('=', 1)
var_dict[key.strip()] = value.strip()
logging.info(f"Parsed variables: {var_dict}")
# Replace variables
for key, value in var_dict.items():
text = text.replace(f"<{key}>", value)
# Handle random choices
pattern = r'\{([^}]+)\}'
def replace_random(match):
return random.choice(match.group(1).split('|'))
result = re.sub(pattern, replace_random, text)
logging.info(f"Picked text: {result}")
logging.info(f"Final text: {result}")
return (result,)
@classmethod
def IS_CHANGED(s, text, seed=None):
return (text, seed)
def IS_CHANGED(s, text, variables="", seed=None):
return (text, variables, seed)