Refactor ResolutionDivider class documentation and logic
Updated class docstring for clarity and added comments for better understanding. Modified return values to reflect the visual-only nature of the node.
This commit is contained in:
+39
-37
@@ -5,8 +5,12 @@ import folder_paths
|
||||
class ResolutionDivider:
|
||||
"""
|
||||
Smart Resolution Divider Node.
|
||||
Parses resolution strings or image files and divides them by a float value.
|
||||
Useful for VRAM optimization in workflows like Wan 2.2 I2V.
|
||||
Parses resolution strings or image files and calculates a downscaled resolution
|
||||
based on a divisor value.
|
||||
|
||||
This node is designed as a visual utility helper (calculator), primarily for
|
||||
optimizing VRAM usage in Image-to-Video workflows like Wan 2.2.
|
||||
It does not output data to other nodes, but displays results in real-time via the UI.
|
||||
"""
|
||||
def __init__(self):
|
||||
pass
|
||||
@@ -14,71 +18,69 @@ class ResolutionDivider:
|
||||
@classmethod
|
||||
def INPUT_TYPES(s):
|
||||
input_dir = folder_paths.get_input_directory()
|
||||
# Get list of files in input directory
|
||||
# Get list of files in the input directory for the dropdown
|
||||
files = [f for f in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, f))]
|
||||
|
||||
return {
|
||||
"required": {
|
||||
# 1. Top Position: The resolution string
|
||||
# 1. Top Position: The resolution string input
|
||||
"res_string": ("STRING", {
|
||||
"default": "",
|
||||
"multiline": False,
|
||||
"dynamicPrompts": False,
|
||||
"tooltip": "Example format: 1920x1080 or 1024 1024"
|
||||
"tooltip": "Enter resolution (e.g., 1920x1080) or select an image below."
|
||||
}),
|
||||
|
||||
# 2. Middle Position: The divisor value
|
||||
# 2. Middle Position: The divisor value (Float)
|
||||
"divider": ("FLOAT", {
|
||||
"default": 1.5,
|
||||
"min": 0.1,
|
||||
"max": 10.0, # Increased max just in case
|
||||
"max": 10.0,
|
||||
"step": 0.01,
|
||||
"display": "number",
|
||||
"tooltip": "The value to divide the resolution by (e.g., 1.5 or 2.0)"
|
||||
"tooltip": "The value to divide the resolution by (e.g., 1.5 for Wan 2.2 optimization)."
|
||||
}),
|
||||
|
||||
# 3. Bottom Position (in Python list): Image selector & Upload button
|
||||
# 3. Bottom Position: Image selector & Upload button
|
||||
"image": (sorted(files), {"image_upload": True}),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("RESULT", "STRING")
|
||||
RETURN_NAMES = ("result_display", "info_str")
|
||||
# No output slots (Visual-only node)
|
||||
RETURN_TYPES = ()
|
||||
RETURN_NAMES = ()
|
||||
|
||||
# Treat this as an output node so ComfyUI executes it even without connections
|
||||
OUTPUT_NODE = True
|
||||
|
||||
FUNCTION = "calculate_smart"
|
||||
|
||||
# NEW CATEGORY NAME
|
||||
CATEGORY = "Resolution Utils"
|
||||
|
||||
def calculate_smart(self, res_string, divider, image):
|
||||
# Handle empty input
|
||||
# Handle empty input case to prevent errors
|
||||
if not res_string.strip():
|
||||
return ("0 x 0", "Empty Input")
|
||||
return {}
|
||||
|
||||
# Regex to clean input: replace x, X, *, and commas with space
|
||||
# Regex to clean input: replace 'x', 'X', '*', and ',' with space
|
||||
clean_str = re.sub(r'[xX*,]', ' ', res_string)
|
||||
# Extract numbers
|
||||
|
||||
# Extract numbers from the string
|
||||
parts = [int(s) for s in clean_str.split() if s.isdigit()]
|
||||
|
||||
if len(parts) < 2:
|
||||
return ("Error", "Invalid Format")
|
||||
if len(parts) >= 2:
|
||||
width = parts[0]
|
||||
height = parts[1]
|
||||
|
||||
# Prevent division by zero
|
||||
if divider == 0: divider = 1.0
|
||||
|
||||
width = parts[0]
|
||||
height = parts[1]
|
||||
# Calculate new dimensions (integer)
|
||||
new_w = int(width / divider)
|
||||
new_h = int(height / divider)
|
||||
|
||||
# Print info to the console (useful for debugging history)
|
||||
print(f"[ResDivider] File: {image} | Original: {width}x{height} | Divisor: {divider} | Result: {new_w}x{new_h}")
|
||||
|
||||
# Prevent division by zero
|
||||
if divider == 0: divider = 1.0
|
||||
|
||||
# Calculate new dimensions (integer)
|
||||
new_w = int(width / divider)
|
||||
new_h = int(height / divider)
|
||||
|
||||
# Simple result for display
|
||||
simple_result = f"{new_w} x {new_h}"
|
||||
|
||||
# Detailed info for logging
|
||||
full_info = f"File: {image} | Original: {width}x{height} | Div: {divider} | Result: {new_w}x{new_h}"
|
||||
|
||||
# Console Log
|
||||
print(f"[ResDivider] {full_info}")
|
||||
|
||||
return (simple_result, full_info)
|
||||
# Return empty dictionary as this is an OUTPUT_NODE with no outputs
|
||||
return {"ui": {"text": ["Calculation Done"]}}
|
||||
|
||||
Reference in New Issue
Block a user