Files
aodh-pack/nodes/resolution_reader/resolution_reader.py

94 lines
3.3 KiB
Python

import os
import random
class ResolutionReader:
@classmethod
def INPUT_TYPES(s):
base_path = os.path.dirname(os.path.realpath(__file__))
file_path = os.path.join(base_path, "resolutions", "resolutions.txt")
lines = []
if os.path.exists(file_path):
with open(file_path, 'r') as f:
lines = [line.strip() for line in f.readlines() if line.strip()]
if not lines:
lines = ["1024,1024,1.0"]
return {
"required": {
"resolution": (lines, ),
"selection_mode": (["manual", "sequential", "random"], {"default": "manual"}),
"repeat_count": ("INT", {"default": 1, "min": 1, "max": 100, "step": 1}),
}
}
@classmethod
def IS_CHANGED(s, resolution, selection_mode, repeat_count):
if selection_mode == "random" or selection_mode == "sequential":
return float("nan")
return resolution
RETURN_TYPES = ("INT", "INT", "FLOAT", "INT")
RETURN_NAMES = ("width", "height", "upscale", "total_resolutions")
FUNCTION = "read_resolution"
CATEGORY = "AODH Pack"
def __init__(self):
self.current_index = 0
self.current_count = 0
self.last_selection = None
self.last_start_resolution = None
def read_resolution(self, resolution, selection_mode, repeat_count):
repeat_count = max(1, repeat_count)
base_path = os.path.dirname(os.path.realpath(__file__))
file_path = os.path.join(base_path, "resolutions", "resolutions.txt")
lines = []
if os.path.exists(file_path):
with open(file_path, 'r') as f:
lines = [line.strip() for line in f.readlines() if line.strip()]
if not lines:
return (0, 0, 0.0, 0)
# Reset sequence if resolution changes
if self.last_start_resolution != resolution:
try:
self.current_index = lines.index(resolution)
except ValueError:
self.current_index = 0
self.current_count = 0
self.last_start_resolution = resolution
if selection_mode == "manual":
selected_line = resolution
elif selection_mode == "sequential":
if self.current_count >= repeat_count:
self.current_index += 1
self.current_count = 0
selected_line = lines[self.current_index % len(lines)]
self.current_count += 1
elif selection_mode == "random":
if self.current_count >= repeat_count or self.last_selection is None:
# Use a local Random instance to ensure randomness regardless of global seed
rng = random.Random()
self.last_selection = rng.choice(lines)
self.current_count = 0
selected_line = self.last_selection
self.current_count += 1
else:
selected_line = resolution
try:
# Format: width, height, upscale
parts = [p.strip() for p in selected_line.split(',')]
width = int(parts[0])
height = int(parts[1])
upscale = float(parts[2])
return (width, height, upscale, len(lines))
except (ValueError, IndexError):
return (0, 0, 0.0, len(lines))