From 9f03494d851121ac68c00aa93ffd669adbe4bea7 Mon Sep 17 00:00:00 2001 From: LaVie024 <62406970+LaVie024@users.noreply.github.com> Date: Mon, 19 May 2025 18:38:52 +0000 Subject: [PATCH] Add files via upload --- comfy_extras/nodes_convert_type.py | 89 ++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 comfy_extras/nodes_convert_type.py diff --git a/comfy_extras/nodes_convert_type.py b/comfy_extras/nodes_convert_type.py new file mode 100644 index 000000000..03a2bb44b --- /dev/null +++ b/comfy_extras/nodes_convert_type.py @@ -0,0 +1,89 @@ +import re +import nodes +from comfy.comfy_types.node_typing import IO + +# Portions of this code is derived from Itdrdata/ComfyUI-Impact-Pack, +# specifically the handling of anytype and parts of the ConvertDataType +# node. + +class AnyType(str): + def __ne__(self, __value: object) -> bool: + return False + +any_typ = AnyType("*") + +class ConvertDataType: + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return {"required": {"value": (any_typ,), + "int_mode": (["truncate","round","bankers_rounding"],), + "string_round_value": ("BOOLEAN", {"default": True}), + "string_round_to": ("INT", {"default": 2, "min": 0}) + }} + + RETURN_TYPES = ("STRING", "FLOAT", "INT", "BOOLEAN", IO.ANY,) + RETURN_NAMES = ("STRING", "FLOAT", "INT", "BOOLEAN", "COMBO",) + FUNCTION = "convert_type" + + CATEGORY = "utils/primitive" + + @staticmethod + def is_number(string): + pattern = re.compile(r'^[-+]?[0-9]*\.?[0-9]+$') + return bool(pattern.match(string)) + + def convert_type(self, value, int_mode, string_round_value, string_round_to): + # Initialize all outputs, necessary in the case of converting a string, + # and the string is not a number. + float_out = None + int_out = None + string_out = None + + if isinstance(value, str): + if self.is_number(value): + float_out = float(value) + else: + string_out = value + elif isinstance(value, (int, float)): + float_out = float(value) + else: + string_out = str(value) + + if float_out is not None: + v = float_out + if int_mode == "truncate": + int_out = int(v) + elif int_mode == "bankers_rounding": + int_out = int(round(v)) + elif int_mode == "round": + int_out = int(v + 0.5) if v >= 0 else int(v - 0.5) + else: + raise ValueError(f"Unknown mode: {int_mode}") + + if string_round_value: + string_out = f"{v:.{string_round_to}f}" + else: + string_out = str(v) + + if float_out is not None: + boolean_out = bool(float_out) + elif string_out is not None: + boolean_out = bool(string_out) + else: + boolean_out = False + + items = ( + [t.strip() for t in string_out.split(",") if t.strip()] + if isinstance(string_out, str) else + [] + ) + combo_out = items[0] if items else "" + + return (string_out, float_out, int_out, boolean_out, combo_out) + +NODE_CLASS_MAPPINGS = { + "Convert Data Type": ConvertDataType +}