From 727ab9bc4765789b83647adca04d76d5f7a9d574 Mon Sep 17 00:00:00 2001 From: akatz-ai Date: Wed, 18 Jun 2025 01:43:43 +0000 Subject: [PATCH] Add boolean comparison for number nodes --- math_nodes.py | 125 +++++++++++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 2 +- 2 files changed, 126 insertions(+), 1 deletion(-) diff --git a/math_nodes.py b/math_nodes.py index 4fa1d06..9efd5a6 100644 --- a/math_nodes.py +++ b/math_nodes.py @@ -509,6 +509,125 @@ class NumberRange(UtilityNode): else: return (min_value < value < max_value,) +@VariantSupport() +class NumberComparison(BooleanNode): + """ + Compare two numbers and return a boolean result. + """ + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "a": (NUMBER, {"default": 0.0}), + "b": (NUMBER, {"default": 0.0}), + "operation": (["==", "!=", "<", ">", "<=", ">="],), + }, + } + + RETURN_TYPES = ("BOOLEAN",) + RETURN_NAMES = ("result",) + FUNCTION = "compare" + + def compare(self, a, b, operation): + if operation == "==": + return (a == b,) + elif operation == "!=": + return (a != b,) + elif operation == "<": + return (a < b,) + elif operation == ">": + return (a > b,) + elif operation == "<=": + return (a <= b,) + elif operation == ">=": + return (a >= b,) + +@VariantSupport() +class IntegerComparison(BooleanNode): + """ + Compare two integers and return a boolean result. + """ + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "a": ("INT", {"default": 0, "min": -0xffffffffffffffff, "max": 0xffffffffffffffff, "step": 1}), + "b": ("INT", {"default": 0, "min": -0xffffffffffffffff, "max": 0xffffffffffffffff, "step": 1}), + "operation": (["==", "!=", "<", ">", "<=", ">="],), + }, + } + + RETURN_TYPES = ("BOOLEAN",) + RETURN_NAMES = ("result",) + FUNCTION = "compare" + + def compare(self, a, b, operation): + if operation == "==": + return (a == b,) + elif operation == "!=": + return (a != b,) + elif operation == "<": + return (a < b,) + elif operation == ">": + return (a > b,) + elif operation == "<=": + return (a <= b,) + elif operation == ">=": + return (a >= b,) + +@VariantSupport() +class FloatComparison(BooleanNode): + """ + Compare two floats and return a boolean result. + """ + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "a": ("FLOAT", {"default": 0.0, "min": -999999999999.0, "max": 999999999999.0, "step": 0.001}), + "b": ("FLOAT", {"default": 0.0, "min": -999999999999.0, "max": 999999999999.0, "step": 0.001}), + "operation": (["==", "!=", "<", ">", "<=", ">="],), + }, + "optional": { + "tolerance": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.0001}), + }, + } + + RETURN_TYPES = ("BOOLEAN",) + RETURN_NAMES = ("result",) + FUNCTION = "compare" + + def compare(self, a, b, operation, tolerance=0.0): + # Handle floating point comparison with optional tolerance + if tolerance > 0 and operation in ["==", "!="]: + equal = abs(a - b) <= tolerance + if operation == "==": + return (equal,) + else: # != + return (not equal,) + + if operation == "==": + return (a == b,) + elif operation == "!=": + return (a != b,) + elif operation == "<": + return (a < b,) + elif operation == ">": + return (a > b,) + elif operation == "<=": + return (a <= b,) + elif operation == ">=": + return (a >= b,) + @VariantSupport() class BooleanLogic(BooleanNode): """ @@ -587,6 +706,9 @@ MATH_NODE_CLASS_MAPPINGS = { "NumberClamp": NumberClamp, "NumberLerp": NumberLerp, "NumberRange": NumberRange, + "NumberComparison": NumberComparison, + "IntegerComparison": IntegerComparison, + "FloatComparison": FloatComparison, "BooleanLogic": BooleanLogic, "BooleanUnary": BooleanUnary, } @@ -606,6 +728,9 @@ MATH_NODE_DISPLAY_NAME_MAPPINGS = { "NumberClamp": f"Number Clamp {NODE_POSTFIX}", "NumberLerp": f"Number Lerp {NODE_POSTFIX}", "NumberRange": f"Number Range {NODE_POSTFIX}", + "NumberComparison": f"Number Comparison {NODE_POSTFIX}", + "IntegerComparison": f"Integer Comparison {NODE_POSTFIX}", + "FloatComparison": f"Float Comparison {NODE_POSTFIX}", "BooleanLogic": f"Boolean Logic {NODE_POSTFIX}", "BooleanUnary": f"Boolean Unary {NODE_POSTFIX}", } \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 6c59b09..8b7a3db 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "comfyui-basic-math" description = "Custom nodes for performing basic math operations" -version = "0.0.3" +version = "0.0.4" license = { file = "LICENSE.txt" } [project.urls]