diff --git a/README.md b/README.md index c3a24b0..134eb2d 100644 --- a/README.md +++ b/README.md @@ -259,6 +259,43 @@ Useful for dynamic resizing, conditional logic based on aspect ratio, or passing --- +### 🧮 Math & Random + +
+➕ Int/Float Math Operation - Perform basic arithmetic + +**Category:** `DebugPadawan/Math` + +Performs simple math operations (add, subtract, multiply, divide, modulo, power) on integers or floats. + +**📥 Inputs:** +- `a`, `b` *(Int/Float)*: The values to calculate +- `operation` *(List)*: The type of mathematical operation + +**📤 Outputs:** +- `result` *(Int/Float)*: The calculated value (returns both Int and Float variations) + +
+ +
+🎲 Random Generator - Seed-based random values + +**Category:** `DebugPadawan/Math` + +Generates a random float or integer between a min and max value, driven by a specific seed for reproducibility. + +**📥 Inputs:** +- `seed` *(Integer)*: Seed for the random generator +- `min_val`, `max_val` *(Float/Integer)*: The range bounds +- `mode` *(List)*: Whether to generate a float or an int + +**📤 Outputs:** +- `val` *(Float/Integer)*: The random value (returns both Int and Float formats) + +
+ +--- + ### 🔧 Utility Nodes
@@ -338,6 +375,7 @@ Data → Wait (2.0s) → Processing → Wait (1.0s) → Output | **DebugPadawan/Utilities** | List Info | Data analysis helpers | | **DebugPadawan/Logic** | Conditional String | Conditional operations | | **DebugPadawan/Image** | Image Info | Image tensor analysis | +| **DebugPadawan/Math** | Int Math Operation, Float Math Operation, Random Generator | Basic arithmetic and random number generation | --- diff --git a/__init__.py b/__init__.py index 091abc0..61a548a 100644 --- a/__init__.py +++ b/__init__.py @@ -13,6 +13,8 @@ from .nodes.json import NODE_CLASS_MAPPINGS as JSON_NODES from .nodes.json import NODE_DISPLAY_NAME_MAPPINGS as JSON_DISPLAY_NAMES from .nodes.image import NODE_CLASS_MAPPINGS as IMAGE_NODES from .nodes.image import NODE_DISPLAY_NAME_MAPPINGS as IMAGE_DISPLAY_NAMES +from .nodes.math_nodes import NODE_CLASS_MAPPINGS as MATH_NODES +from .nodes.math_nodes import NODE_DISPLAY_NAME_MAPPINGS as MATH_DISPLAY_NAMES # Combine all node mappings NODE_CLASS_MAPPINGS = { @@ -21,6 +23,7 @@ NODE_CLASS_MAPPINGS = { **TIMING_NODES, **JSON_NODES, **IMAGE_NODES, + **MATH_NODES, } NODE_DISPLAY_NAME_MAPPINGS = { @@ -29,6 +32,7 @@ NODE_DISPLAY_NAME_MAPPINGS = { **TIMING_DISPLAY_NAMES, **JSON_DISPLAY_NAMES, **IMAGE_DISPLAY_NAMES, + **MATH_DISPLAY_NAMES, } __all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS'] diff --git a/nodes/math_nodes.py b/nodes/math_nodes.py new file mode 100644 index 0000000..fd66e65 --- /dev/null +++ b/nodes/math_nodes.py @@ -0,0 +1,120 @@ +import random +import math + +class IntMathOperation: + """ + Node for performing basic integer math operations + """ + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "a": ("INT", {"default": 0, "step": 1}), + "b": ("INT", {"default": 0, "step": 1}), + "operation": (["add", "subtract", "multiply", "divide", "modulo", "power"],), + } + } + + RETURN_TYPES = ("INT", "FLOAT") + RETURN_NAMES = ("int_result", "float_result") + FUNCTION = "perform_math" + CATEGORY = "DebugPadawan/Math" + + def perform_math(self, a, b, operation): + if operation == "add": + res = a + b + elif operation == "subtract": + res = a - b + elif operation == "multiply": + res = a * b + elif operation == "divide": + res = a / b if b != 0 else 0 + elif operation == "modulo": + res = a % b if b != 0 else 0 + elif operation == "power": + res = math.pow(a, b) + else: + res = 0 + + return (int(res), float(res)) + + +class FloatMathOperation: + """ + Node for performing basic float math operations + """ + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "a": ("FLOAT", {"default": 0.0, "step": 0.01}), + "b": ("FLOAT", {"default": 0.0, "step": 0.01}), + "operation": (["add", "subtract", "multiply", "divide", "power"],), + } + } + + RETURN_TYPES = ("FLOAT", "INT") + RETURN_NAMES = ("float_result", "int_result") + FUNCTION = "perform_math" + CATEGORY = "DebugPadawan/Math" + + def perform_math(self, a, b, operation): + if operation == "add": + res = a + b + elif operation == "subtract": + res = a - b + elif operation == "multiply": + res = a * b + elif operation == "divide": + res = a / b if b != 0 else 0.0 + elif operation == "power": + res = math.pow(a, b) + else: + res = 0.0 + + return (float(res), int(res)) + + +class RandomGenerator: + """ + Node for generating random integers or floats based on a seed + """ + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "min_val": ("FLOAT", {"default": 0.0, "step": 0.01}), + "max_val": ("FLOAT", {"default": 1.0, "step": 0.01}), + "mode": (["float", "int"],), + } + } + + RETURN_TYPES = ("FLOAT", "INT") + RETURN_NAMES = ("float_val", "int_val") + FUNCTION = "generate" + CATEGORY = "DebugPadawan/Math" + + def generate(self, seed, min_val, max_val, mode): + # Initialize the random generator with the seed + rng = random.Random(seed) + + if mode == "float": + res = rng.uniform(min_val, max_val) + return (float(res), int(res)) + else: + # For int mode, ensure min and max are integers + res = rng.randint(int(min_val), int(max_val)) + return (float(res), int(res)) + +NODE_CLASS_MAPPINGS = { + "DebugPadawan_IntMathOperation": IntMathOperation, + "DebugPadawan_FloatMathOperation": FloatMathOperation, + "DebugPadawan_RandomGenerator": RandomGenerator, +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "DebugPadawan_IntMathOperation": "Int Math Operation", + "DebugPadawan_FloatMathOperation": "Float Math Operation", + "DebugPadawan_RandomGenerator": "Random Generator", +}