Skip to content

Commit b81ebfa

Browse files
committed
Make flexible inputs work with comfyanonymous/ComfyUI#2666
1 parent 64bc679 commit b81ebfa

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

py/any_switch.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from .context_utils import is_context_empty
22
from .constants import get_category, get_name
3-
from .utils import any_type
4-
from .utils import ContainsAnyDict
3+
from .utils import FlexibleOptionalInputType, any_type
54

65

76
def is_none(value):
@@ -22,7 +21,7 @@ class RgthreeAnySwitch:
2221
def INPUT_TYPES(cls): # pylint: disable = invalid-name, missing-function-docstring
2322
return {
2423
"required": {},
25-
"optional": ContainsAnyDict(),
24+
"optional": FlexibleOptionalInputType(any_type),
2625
}
2726

2827
RETURN_TYPES = (any_type,)

py/context_merge.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from .constants import get_category, get_name
33
from .context_utils import (ORIG_CTX_RETURN_TYPES, ORIG_CTX_RETURN_NAMES, merge_new_context,
44
get_orig_context_return_tuple, is_context_empty)
5-
from .utils import ContainsAnyDict
5+
from .utils import FlexibleOptionalInputType
66

77

88
class RgthreeContextMerge:
@@ -15,7 +15,7 @@ class RgthreeContextMerge:
1515
def INPUT_TYPES(cls): # pylint: disable = invalid-name, missing-function-docstring
1616
return {
1717
"required": {},
18-
"optional": ContainsAnyDict(),
18+
"optional": FlexibleOptionalInputType("RGTHREE_CONTEXT"),
1919
}
2020

2121
RETURN_TYPES = ORIG_CTX_RETURN_TYPES

py/context_switch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from .constants import get_category, get_name
33
from .context_utils import (ORIG_CTX_RETURN_TYPES, ORIG_CTX_RETURN_NAMES, is_context_empty,
44
get_orig_context_return_tuple)
5-
from .utils import ContainsAnyDict
5+
from .utils import FlexibleOptionalInputType
66

77

88
class RgthreeContextSwitch:
@@ -15,7 +15,7 @@ class RgthreeContextSwitch:
1515
def INPUT_TYPES(cls): # pylint: disable = invalid-name, missing-function-docstring
1616
return {
1717
"required": {},
18-
"optional": ContainsAnyDict(),
18+
"optional": FlexibleOptionalInputType("RGTHREE_CONTEXT"),
1919
}
2020

2121
RETURN_TYPES = ORIG_CTX_RETURN_TYPES

py/power_lora_loader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from nodes import LoraLoader
22
from .constants import get_category, get_name
33
from .power_prompt_utils import get_lora_by_filename
4-
from .utils import ContainsAnyDict
4+
from .utils import FlexibleOptionalInputType, any_type
55

66

77
class RgthreePowerLoraLoader:
@@ -18,7 +18,7 @@ def INPUT_TYPES(cls): # pylint: disable = invalid-name, missing-function-docstr
1818
"clip": ("CLIP",),
1919
},
2020
# Since we will pass any number of loras in from the UI, this needs to always allow an
21-
"optional": ContainsAnyDict(),
21+
"optional": FlexibleOptionalInputType(any_type),
2222
"hidden": {},
2323
}
2424

py/utils.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,26 @@ class AnyType(str):
99
def __ne__(self, __value: object) -> bool:
1010
return False
1111

12+
class FlexibleOptionalInputType(dict):
13+
"""A special class to make flexible nodes that pass data to our python handlers.
1214
13-
class ContainsAnyDict(dict):
14-
"""A special class that always returns true for contains check ('prop' in my_dict)."""
15+
Enables both flexible/dynamic input types (like for Any Switch) or a dynamic number of inputs
16+
(like for Any Switch, Context Switch, Context Merge, Power Lora Loader, etc).
17+
18+
Note, for ComfyUI, all that's needed is the `__contains__` override below, which tells ComfyUI
19+
that our node will handle the input, regardless of what it is.
20+
21+
However, with https://github.com/comfyanonymous/ComfyUI/pull/2666 a large change would occur
22+
requiring more details on the input itself. There, we need to return a list/tuple where the first
23+
item is the type. This can be a real type, or use the AnyType for additional flexibility.
24+
25+
This should be forwards compatible unless more changes occur in the PR.
26+
"""
27+
def __init__(self, type):
28+
self.type = type
29+
30+
def __getitem__(self, key):
31+
return (self.type, )
1532

1633
def __contains__(self, key):
1734
return True

0 commit comments

Comments
 (0)