This commit is contained in:
justumen
2025-02-27 18:00:12 +01:00
parent 6a21e32a42
commit 10263f2110
38 changed files with 1965 additions and 432 deletions

View File

@@ -1,5 +1,8 @@
import os
import re
import random
import csv
from itertools import cycle
from aiohttp import web
from server import PromptServer
@@ -17,7 +20,7 @@ class LineSelector:
"LOOP": ("BOOLEAN", {"default": False}), # Return all lines as list
"LOOP_SEQUENTIAL": ("BOOLEAN", {"default": False}), # Sequential looping
"jump": ("INT", {"default": 1, "min": 1, "max": 100, "step": 1}), # Jump size for sequential loop
"pick_random_variable": ("BOOLEAN", {"default": False}), # Enable random choice functionality
"pick_random_variable": ("BOOLEAN", {"default": True}), # Enable random choice functionality
},
"optional": {
"variables": ("STRING", {"multiline": True, "forceInput": True}),
@@ -35,6 +38,122 @@ class LineSelector:
FUNCTION = "select_line"
CATEGORY = "Bjornulf"
def find_variables(self, text):
stack = []
variables = []
for i, char in enumerate(text):
if char == '{':
stack.append((i, len(stack) + 1))
elif char == '}' and stack:
start, nesting = stack.pop()
variables.append({
'start': start,
'end': i + 1,
'nesting': nesting
})
variables.sort(key=lambda x: (-x['nesting'], -x['end']))
return variables
def parse_option(self, part):
if part.startswith('%csv='):
try:
filename = part.split('=', 1)[1].strip()
with open(filename, 'r') as f:
return [row[0] for row in csv.reader(f)]
except Exception as e:
return [f"[CSV Error: {str(e)}]"]
elif '(' in part and '%)' in part:
option, weight = part.rsplit('(', 1)
return (option.strip(), float(weight.split('%)')[0]))
return part.strip()
def process_content(self, content, seed):
random.seed(seed)
parts = []
weights = []
group_defined = False
group_name = None
for p in content.split('|'):
p = p.strip()
if p.startswith('group='):
group_name = p.split('=', 1)[1].strip()
group_defined = True
continue
parsed = self.parse_option(p)
if isinstance(parsed, list): # CSV data
parts.extend(parsed)
weights.extend([1]*len(parsed))
elif isinstance(parsed, tuple): # Weighted option
parts.append(parsed[0])
weights.append(parsed[1])
else:
parts.append(parsed)
weights.append(1)
if group_defined:
return {'type': 'group', 'name': group_name, 'options': parts}
if any(w != 1 for w in weights):
total = sum(weights)
if total == 0: weights = [1]*len(parts)
return random.choices(parts, weights=[w/total for w in weights])[0]
return random.choice(parts) if parts else ''
def process_advanced_syntax(self, text, seed):
# Process nested variables
variables = self.find_variables(text)
substitutions = []
groups = {}
for var in variables:
start, end = var['start'], var['end']
content = text[start+1:end-1]
processed = self.process_content(content, seed)
if isinstance(processed, dict):
if processed['type'] == 'group':
group_name = processed['name']
if group_name not in groups:
groups[group_name] = []
groups[group_name].append({
'start': start,
'end': end,
'options': processed['options']
})
else:
substitutions.append({
'start': start,
'end': end,
'sub': processed
})
# Handle groups
for group_name, matches in groups.items():
if not matches or not matches[0]['options']:
continue
options = matches[0]['options']
permuted = random.sample(options, len(options))
perm_cycle = cycle(permuted)
for m in matches:
substitutions.append({
'start': m['start'],
'end': m['end'],
'sub': next(perm_cycle)
})
# Apply regular substitutions
substitutions.sort(key=lambda x: -x['start'])
result_text = text
for sub in substitutions:
result_text = result_text[:sub['start']] + sub['sub'] + result_text[sub['end']:]
return result_text
def select_line(self, text, line_number, RANDOM, LOOP, LOOP_SEQUENTIAL, jump, pick_random_variable, variables="", seed=-1):
# Parse variables
var_dict = {}
@@ -58,16 +177,15 @@ class LineSelector:
import os
# Set seed if provided
if seed >= 0:
random.seed(seed)
if seed < 0:
seed = random.randint(0, 0x7FFFFFFFFFFFFFFF)
# Process random choice functionality if enabled
# Process WriteTextAdvanced syntax if enabled
if pick_random_variable:
pattern = r'\{([^}]+)\}'
def replace_random(match):
return random.choice(match.group(1).split('|'))
lines = [re.sub(pattern, replace_random, line) for line in lines]
processed_lines = []
for line in lines:
processed_lines.append(self.process_advanced_syntax(line, seed))
lines = processed_lines
# Handle sequential looping
if LOOP_SEQUENTIAL: