Consolidate boolean, casting, control flow, data list and dict nodes

This commit is contained in:
StableLlama
2025-05-11 16:18:45 +02:00
parent 22bb1569ba
commit 2e4dadaa80
5 changed files with 235 additions and 490 deletions
+21 -57
View File
@@ -1,8 +1,8 @@
from typing import Any
from inspect import cleandoc
from comfy.comfy_types.node_typing import IO, ComfyNodeABC
class CastToString:
class CastToString(ComfyNodeABC):
"""
Converts any input to a STRING. Non-string values are converted using str().
"""
@@ -10,24 +10,20 @@ class CastToString:
def INPUT_TYPES(cls):
return {
"required": {
"input": ("*", {})
"input": (IO.ANY, {})
}
}
RETURN_TYPES = ("STRING",)
RETURN_TYPES = (IO.STRING,)
CATEGORY = "Basic/cast"
DESCRIPTION = cleandoc(__doc__ or "")
FUNCTION = "convert_to_string"
@classmethod
def VALIDATE_INPUTS(cls, input_types: dict[str, str]) -> bool:
return True
def convert_to_string(self, input: Any) -> tuple[str]:
return (str(input),)
class CastToInt:
class CastToInt(ComfyNodeABC):
"""
Converts any numeric input to an INT. Non-numeric or invalid inputs raise a ValueError.
"""
@@ -35,19 +31,15 @@ class CastToInt:
def INPUT_TYPES(cls):
return {
"required": {
"input": ("*", {})
"input": (IO.ANY, {})
}
}
RETURN_TYPES = ("INT",)
RETURN_TYPES = (IO.INT,)
CATEGORY = "Basic/cast"
DESCRIPTION = cleandoc(__doc__ or "")
FUNCTION = "convert_to_int"
@classmethod
def VALIDATE_INPUTS(cls, input_types: dict[str, str]) -> bool:
return True
def convert_to_int(self, input: Any) -> tuple[int]:
try:
return (int(input),)
@@ -55,7 +47,7 @@ class CastToInt:
raise ValueError(f"Cannot convert {input} to an INT.")
class CastToFloat:
class CastToFloat(ComfyNodeABC):
"""
Converts any numeric input to a FLOAT. Non-numeric or invalid inputs raise a ValueError.
"""
@@ -63,7 +55,7 @@ class CastToFloat:
def INPUT_TYPES(cls):
return {
"required": {
"input": ("*", {})
"input": (IO.ANY, {})
}
}
@@ -72,10 +64,6 @@ class CastToFloat:
DESCRIPTION = cleandoc(__doc__ or "")
FUNCTION = "convert_to_float"
@classmethod
def VALIDATE_INPUTS(cls, input_types: dict[str, str]) -> bool:
return True
def convert_to_float(self, input: Any) -> tuple[float]:
try:
return (float(input),)
@@ -83,7 +71,7 @@ class CastToFloat:
raise ValueError(f"Cannot convert {input} to a FLOAT.")
class CastToBoolean:
class CastToBoolean(ComfyNodeABC):
"""
Converts any input to a BOOLEAN. Follows standard Python truthy/falsy rules.
"""
@@ -91,7 +79,7 @@ class CastToBoolean:
def INPUT_TYPES(cls):
return {
"required": {
"input": ("*", {})
"input": (IO.ANY, {})
}
}
@@ -100,15 +88,11 @@ class CastToBoolean:
DESCRIPTION = cleandoc(__doc__ or "")
FUNCTION = "convert_to_boolean"
@classmethod
def VALIDATE_INPUTS(cls, input_types: dict[str, str]) -> bool:
return True
def convert_to_boolean(self, input: Any) -> tuple[bool]:
return (bool(input),)
class CastToList:
class CastToList(ComfyNodeABC):
"""
Converts any input to a LIST. Non-list inputs are wrapped in a list. If input is a ComfyUI data list,
it converts the individual items into a Python LIST.
@@ -117,7 +101,7 @@ class CastToList:
def INPUT_TYPES(cls):
return {
"required": {
"input": ("*", {})
"input": (IO.ANY, {})
}
}
@@ -126,17 +110,13 @@ class CastToList:
DESCRIPTION = cleandoc(__doc__ or "")
FUNCTION = "convert_to_list"
@classmethod
def VALIDATE_INPUTS(cls, input_types: dict[str, str]) -> bool:
return True
def convert_to_list(self, input: Any) -> tuple[list]:
if isinstance(input, list):
return (input,)
return ([input],)
class CastDataListToList:
class CastDataListToList(ComfyNodeABC):
"""
Converts a ComfyUI data list into a LIST object.
@@ -147,7 +127,7 @@ class CastDataListToList:
def INPUT_TYPES(cls):
return {
"required": {
"list": ("*", {}),
"list": (IO.ANY, {}),
}
}
@@ -157,15 +137,11 @@ class CastDataListToList:
FUNCTION = "convert"
INPUT_IS_LIST = True
@classmethod
def VALIDATE_INPUTS(cls, input_types: dict[str, str]) -> bool:
return True
def convert(self, **kwargs: list[Any]) -> tuple[list[Any]]:
return (kwargs.get('list', []).copy(),)
class CastToSet:
class CastToSet(ComfyNodeABC):
"""
Converts any input to a SET. Non-set inputs are converted into a set. If input is a ComfyUI data list,
it casts the individual items into a SET.
@@ -174,7 +150,7 @@ class CastToSet:
def INPUT_TYPES(cls):
return {
"required": {
"input": ("*", {})
"input": (IO.ANY, {})
}
}
@@ -183,17 +159,13 @@ class CastToSet:
DESCRIPTION = cleandoc(__doc__ or "")
FUNCTION = "convert_to_set"
@classmethod
def VALIDATE_INPUTS(cls, input_types: dict[str, str]) -> bool:
return True
def convert_to_set(self, input: Any) -> tuple[set]:
if isinstance(input, set):
return (input,)
return ({input,} if not isinstance(input, list) else set(input),)
class CastDataListToSet:
class CastDataListToSet(ComfyNodeABC):
"""
Converts a ComfyUI data list into a LIST object.
@@ -204,7 +176,7 @@ class CastDataListToSet:
def INPUT_TYPES(cls):
return {
"required": {
"list": ("*", {}),
"list": (IO.ANY, {}),
}
}
@@ -214,15 +186,11 @@ class CastDataListToSet:
FUNCTION = "convert"
INPUT_IS_LIST = True
@classmethod
def VALIDATE_INPUTS(cls, input_types: dict[str, str]) -> bool:
return True
def convert(self, **kwargs: list[Any]) -> tuple[set[Any]]:
return (set(kwargs.get('list', [])),)
class CastToDict:
class CastToDict(ComfyNodeABC):
"""
Converts compatible inputs to a DICT. Input must be a mapping or a list of key-value pairs.
"""
@@ -230,7 +198,7 @@ class CastToDict:
def INPUT_TYPES(cls):
return {
"required": {
"input": ("*", {})
"input": (IO.ANY, {})
}
}
@@ -239,10 +207,6 @@ class CastToDict:
DESCRIPTION = cleandoc(__doc__ or "")
FUNCTION = "convert_to_dict"
@classmethod
def VALIDATE_INPUTS(cls, input_types: dict[str, str]) -> bool:
return True
def convert_to_dict(self, input: Any) -> tuple[dict]:
try:
return (dict(input),)