-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
172 lines (135 loc) · 5.19 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import inspect
import textwrap
import nodes
class AnyType(str):
def __ne__(self, __value: object) -> bool:
return False
any_typ = AnyType("*")
class SrlConditionalInterrupt:
"""Interrupt processing if the boolean input is true. Pass through the other input."""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"interrupt": ("BOOLEAN", {"forceInput": True}),
"inp": (any_typ,),
},
}
RETURN_TYPES = (any_typ,)
RETURN_NAMES = ("output",)
FUNCTION = "doit"
CATEGORY = "utils"
def doit(self, interrupt, inp):
if interrupt:
nodes.interrupt_processing()
return (inp,)
class SrlFormatString:
"""Use Python f-string syntax to generate a string using the inputs as the arguments."""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"format": ("STRING", {
"multiline": False,
"default": "first input via str(): {}, second input via repr(): {!r}, third input by index: {2}, fifth input by name: {in4}",
}),
},
"optional": {
"in0": (any_typ,),
"in1": (any_typ,),
"in2": (any_typ,),
"in3": (any_typ,),
"in4": (any_typ,),
},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "doit"
CATEGORY = "utils"
def doit(self, format, **kwargs):
# Allow referencing arguments both by name and index.
return (format.format(*kwargs.values(), **kwargs),)
class SrlEval:
"""Evaluate any Python code as a function with the given inputs."""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"parameters": ("STRING", {
"multiline": False,
"default": "a, b=None, c=\"foo\", *rest",
"dynamicPrompts": False,
}),
"code": ("STRING", {
"multiline": True,
"default": "code goes here\nreturn a + b",
"dynamicPrompts": False,
}),
},
"optional": {
"arg0": (any_typ,),
"arg1": (any_typ,),
"arg2": (any_typ,),
"arg3": (any_typ,),
"arg4": (any_typ,),
}
}
RETURN_TYPES = (any_typ,)
FUNCTION = "doit"
CATEGORY = "utils"
def doit(self, parameters, code, **kw):
# Indent the code for the main body of the function
func_code = textwrap.indent(code, " ")
source = f"def func({parameters}):\n{func_code}"
# The provided code can mutate globals or really do anything, but ComfyUI isn't secure to begin with.
loc = {}
exec(source, globals(), loc)
func = loc["func"]
argspec = inspect.getfullargspec(func)
# We don't allow variable keyword arguments or keyword only arguments, but we do allow varargs
assert argspec.varkw is None
assert not argspec.kwonlyargs
input_names = list(self.INPUT_TYPES()["optional"].keys())
parameter_names = argspec.args
# Convert the list of defaults into a dictionary to make it easier to use
default_list = argspec.defaults if argspec.defaults is not None else []
defaults = {parameter_name: default for parameter_name, default in zip(parameter_names[-len(default_list):], default_list)}
# We handle substituting default values ourselves in order to support *args
args = [kw[input_name] if input_name in kw else defaults[parameter_name] for parameter_name, input_name in zip(parameter_names, input_names)]
# Support *args
if argspec.varargs is not None:
unnamed_inputs = input_names[len(argspec.args):]
# I considered requiring the remaining inputs to be contiguous, but I don't think it's helpful.
args += [kw[input_name] for input_name in unnamed_inputs if input_name in kw]
ret = func(*args)
return (ret,)
class SrlFilterImageList:
"""Filter an image list based on a list of bools"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"images": ("IMAGE",),
"keep": ("BOOLEAN", {"forceInput": True}),
}
}
RETURN_TYPES = ("IMAGE",)
INPUT_IS_LIST = True
OUTPUT_IS_LIST = (True,)
FUNCTION = "doit"
def doit(self, images, keep):
return ([im for im, k in zip(images, keep) if k],)
# A dictionary that contains all nodes you want to export with their names
# NOTE: names should be globally unique
NODE_CLASS_MAPPINGS = {
"SRL Conditional Interrrupt": SrlConditionalInterrupt,
"SRL Format String": SrlFormatString,
"SRL Eval": SrlEval,
"SRL Filter Image List": SrlFilterImageList,
}
# A dictionary that contains the friendly/humanly readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS = {
"SrlConditionalInterrupt": "SRL Conditional Interrupt",
"SrlFormatString": "SRL Format String",
"SrlEval": "SRL Eval",
"SrlFilterImageList": "SRL Filter Image List",
}