diff --git a/__init__.py b/__init__.py index 33ab03f..73021fe 100644 --- a/__init__.py +++ b/__init__.py @@ -15,15 +15,29 @@ try: NODE_CLASS_MAPPINGS as LATENT_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS as LATENT_DISPLAY_MAPPINGS ) + from categories.math_tools import ( + NODE_CLASS_MAPPINGS as MATH_CLASS_MAPPINGS, + NODE_DISPLAY_NAME_MAPPINGS as MATH_DISPLAY_MAPPINGS + ) except ImportError: # Fallback for ComfyUI environments import importlib.util + + # Import latent tools spec = importlib.util.spec_from_file_location("latent_tools", os.path.join(current_dir, "categories", "latent_tools.py")) latent_tools = importlib.util.module_from_spec(spec) spec.loader.exec_module(latent_tools) LATENT_CLASS_MAPPINGS = latent_tools.NODE_CLASS_MAPPINGS LATENT_DISPLAY_MAPPINGS = latent_tools.NODE_DISPLAY_NAME_MAPPINGS + + # Import math tools + spec = importlib.util.spec_from_file_location("math_tools", os.path.join(current_dir, "categories", "math_tools.py")) + math_tools = importlib.util.module_from_spec(spec) + spec.loader.exec_module(math_tools) + + MATH_CLASS_MAPPINGS = math_tools.NODE_CLASS_MAPPINGS + MATH_DISPLAY_MAPPINGS = math_tools.NODE_DISPLAY_NAME_MAPPINGS # Combine all category mappings NODE_CLASS_MAPPINGS = {} @@ -33,6 +47,10 @@ NODE_DISPLAY_NAME_MAPPINGS = {} NODE_CLASS_MAPPINGS.update(LATENT_CLASS_MAPPINGS) NODE_DISPLAY_NAME_MAPPINGS.update(LATENT_DISPLAY_MAPPINGS) +# Add math tools +NODE_CLASS_MAPPINGS.update(MATH_CLASS_MAPPINGS) +NODE_DISPLAY_NAME_MAPPINGS.update(MATH_DISPLAY_MAPPINGS) + # Future categories will be added here: # from categories.image_tools import NODE_CLASS_MAPPINGS as IMAGE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS as IMAGE_DISPLAY_MAPPINGS # NODE_CLASS_MAPPINGS.update(IMAGE_CLASS_MAPPINGS) diff --git a/categories/__init__.py b/categories/__init__.py index cb4a41b..1b04739 100644 --- a/categories/__init__.py +++ b/categories/__init__.py @@ -3,6 +3,7 @@ Node categories for Chye ComfyUI Toolset """ from .latent_tools import * +from .math_tools import * # Import other category modules as they are added # from .image_tools import * diff --git a/categories/math_tools.py b/categories/math_tools.py new file mode 100644 index 0000000..03af4ac --- /dev/null +++ b/categories/math_tools.py @@ -0,0 +1,94 @@ +""" +Math tools for Chye ComfyUI Toolset +""" + +import sys +import os + +# Add parent directory to Python path for ComfyUI compatibility +current_dir = os.path.dirname(os.path.abspath(__file__)) +parent_dir = os.path.dirname(current_dir) +if parent_dir not in sys.path: + sys.path.append(parent_dir) + +try: + from shared.constants import ( + MATH_CATEGORY, + DEFAULT_MULTIPLIER_VALUE, MIN_MULTIPLIER_VALUE, MAX_MULTIPLIER_VALUE, MULTIPLIER_VALUE_STEP, + DEFAULT_INCREMENT, MIN_INCREMENT, MAX_INCREMENT, INCREMENT_STEP + ) + from shared.helpers import round_to_multiple +except ImportError: + # Fallback import for ComfyUI environments + import importlib.util + + # Import constants + constants_path = os.path.join(parent_dir, "shared", "constants.py") + spec = importlib.util.spec_from_file_location("constants", constants_path) + constants = importlib.util.module_from_spec(spec) + spec.loader.exec_module(constants) + + MATH_CATEGORY = constants.MATH_CATEGORY + DEFAULT_MULTIPLIER_VALUE = constants.DEFAULT_MULTIPLIER_VALUE + MIN_MULTIPLIER_VALUE = constants.MIN_MULTIPLIER_VALUE + MAX_MULTIPLIER_VALUE = constants.MAX_MULTIPLIER_VALUE + MULTIPLIER_VALUE_STEP = constants.MULTIPLIER_VALUE_STEP + DEFAULT_INCREMENT = constants.DEFAULT_INCREMENT + MIN_INCREMENT = constants.MIN_INCREMENT + MAX_INCREMENT = constants.MAX_INCREMENT + INCREMENT_STEP = constants.INCREMENT_STEP + + # Import helpers + helpers_path = os.path.join(parent_dir, "shared", "helpers.py") + spec = importlib.util.spec_from_file_location("helpers", helpers_path) + helpers = importlib.util.module_from_spec(spec) + spec.loader.exec_module(helpers) + + round_to_multiple = helpers.round_to_multiple + + +class CYHResolutionMultiplierNode: + """ + A math node that outputs resolution values based on a multiplier. + The value field will increment/decrement in steps of the multiplier. + Perfect for use with resolution width/height inputs in ComfyUI. + For example, if the multiplier is 32, the value will snap to multiples of 32 (32, 64, 96, etc.). + """ + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "multiplier": ("INT", {"default": DEFAULT_MULTIPLIER_VALUE, "min": MIN_MULTIPLIER_VALUE, "max": MAX_MULTIPLIER_VALUE, "step": MULTIPLIER_VALUE_STEP}), + "width": ("INT", {"default": DEFAULT_MULTIPLIER_VALUE, "min": MIN_MULTIPLIER_VALUE, "max": MAX_MULTIPLIER_VALUE * 20, "step": MULTIPLIER_VALUE_STEP}), + "height": ("INT", {"default": DEFAULT_MULTIPLIER_VALUE, "min": MIN_MULTIPLIER_VALUE, "max": MAX_MULTIPLIER_VALUE * 20, "step": MULTIPLIER_VALUE_STEP}), + }, + } + + RETURN_TYPES = ("INT", "INT") + FUNCTION = "calculate" + CATEGORY = MATH_CATEGORY + + @classmethod + def IS_CHANGED(cls, **kwargs): + return "" + + @classmethod + def VALIDATE_INPUTS(cls, **kwargs): + return True + + def calculate(self, multiplier, width, height): + # Round both width and height to the nearest multiple of the multiplier + result_width = round_to_multiple(width, multiplier) + result_height = round_to_multiple(height, multiplier) + return (result_width, result_height) + + +# Node registration for this category +NODE_CLASS_MAPPINGS = { + "CYHResolutionMultiplierNode": CYHResolutionMultiplierNode, +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "CYHResolutionMultiplierNode": "🔢 CYH Math | Resolution Multiplier", +} \ No newline at end of file diff --git a/shared/constants.py b/shared/constants.py index 402f9a4..352bbfa 100644 --- a/shared/constants.py +++ b/shared/constants.py @@ -67,6 +67,18 @@ ORIENTATIONS = ["Portrait", "Landscape"] # Node categories LATENT_CATEGORY = "latent" +MATH_CATEGORY = "math" + +# Math node constants +DEFAULT_MULTIPLIER_VALUE = 32 +MIN_MULTIPLIER_VALUE = 1 +MAX_MULTIPLIER_VALUE = 1024 +MULTIPLIER_VALUE_STEP = 1 + +DEFAULT_INCREMENT = 1 +MIN_INCREMENT = 1 +MAX_INCREMENT = 100 +INCREMENT_STEP = 1 # Phone screen resolutions PHONE_RESOLUTIONS = {