-
Notifications
You must be signed in to change notification settings - Fork 10
/
comfyui_primitive_ops.py
299 lines (221 loc) · 9.24 KB
/
comfyui_primitive_ops.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import inspect
import math
import typing
from typing import Literal
NODE_CLASS_MAPPINGS = {}
NODE_DISPLAY_NAME_MAPPINGS = {}
def register_node(identifier: str, display_name: str):
def decorator(cls):
NODE_CLASS_MAPPINGS[identifier] = cls
NODE_DISPLAY_NAME_MAPPINGS[identifier] = display_name
return cls
return decorator
def generate_functional_node(
category: str,
identifier: str,
display_name: str,
*,
multiline_string: bool = False,
output_node: bool = False,
):
def decorator(func):
signature = inspect.signature(func)
# generate INPUT_TYPES
required_inputs = {}
for name, param in signature.parameters.items():
has_default = param.default is not param.empty
param_type = param.annotation
if param_type is int:
if not has_default:
raise TypeError("INT input must have a default value")
required_inputs[name] = (
"INT",
{
"default": param.default,
"min": -0xFFFFFFFFFFFFFFFF,
"max": 0xFFFFFFFFFFFFFFFF,
},
)
elif param_type is float:
if not has_default:
raise TypeError("FLOAT input must have a default value")
required_inputs[name] = (
"FLOAT",
{
"default": param.default,
"min": -99999999999999999.0,
"max": 99999999999999999.0,
},
)
elif param_type is str:
if not has_default:
raise TypeError("STRING input must have a default value")
required_inputs[name] = (
"STRING",
{
"default": param.default,
"multiline": multiline_string,
"dynamicPrompts": False,
},
)
elif isinstance(param_type, str):
if has_default:
raise TypeError("Custom input types cannot have default values")
required_inputs[name] = (param_type,)
elif typing.get_origin(param_type) is Literal:
choices = typing.get_args(param_type)
if param.default is not None:
raise TypeError(
"Choice input types must have default value set to None"
)
required_inputs[name] = (choices,)
else:
raise NotImplementedError(
f"Unsupported functional node type: {param_type}"
)
# generate RETURN_TYPES
if signature.return_annotation is signature.empty:
raise TypeError("Functional node must have annotation for return type")
elif typing.get_origin(signature.return_annotation) is not tuple:
raise TypeError("Functional node must return a tuple")
return_types = []
for annot in typing.get_args(signature.return_annotation):
if isinstance(annot, str):
return_types.append(annot)
elif annot is int:
return_types.append("INT")
elif annot is float:
return_types.append("FLOAT")
elif annot is str:
return_types.append("STRING")
else:
raise NotImplementedError(f"Unsupported return type: {annot}")
@register_node(identifier, display_name)
class _:
CATEGORY = category
INPUT_TYPES = lambda: {"required": required_inputs}
RETURN_TYPES = tuple(return_types)
OUTPUT_NODE = output_node
FUNCTION = "execute"
def execute(self, *args, **kwargs):
return func(*args, **kwargs)
return func
return decorator
@generate_functional_node("jamesWalker55", "JWInteger", "Integer")
def _(value: int = 0) -> tuple[int]:
return (value,)
@generate_functional_node("jamesWalker55", "JWIntegerToFloat", "Integer to Float")
def _(value: int = 0) -> tuple[float]:
return (float(value),)
@generate_functional_node("jamesWalker55", "JWIntegerToString", "Integer to String")
def _(value: int = 0, format_string: str = "{:04d}") -> tuple[str]:
return (format_string.format(value),)
@generate_functional_node("jamesWalker55", "JWIntegerAdd", "Integer Add")
def _(a: int = 0, b: int = 0) -> tuple[int]:
return (a + b,)
@generate_functional_node("jamesWalker55", "JWIntegerSub", "Integer Subtract")
def _(a: int = 0, b: int = 0) -> tuple[int]:
return (a - b,)
@generate_functional_node("jamesWalker55", "JWIntegerMul", "Integer Multiply")
def _(a: int = 0, b: int = 0) -> tuple[int]:
return (a * b,)
@generate_functional_node("jamesWalker55", "JWIntegerDiv", "Integer Divide")
def _(a: int = 0, b: int = 0) -> tuple[float]:
return (a / b,)
@generate_functional_node(
"jamesWalker55", "JWIntegerAbsolute", "Integer Absolute Value"
)
def _(value: int = 0) -> tuple[int]:
return (abs(value),)
@generate_functional_node("jamesWalker55", "JWIntegerMin", "Integer Minimum")
def _(a: int = 0, b: int = 0) -> tuple[int]:
return (min(a, b),)
@generate_functional_node("jamesWalker55", "JWIntegerMax", "Integer Maximum")
def _(a: int = 0, b: int = 0) -> tuple[int]:
return (max(a, b),)
@generate_functional_node("jamesWalker55", "JWFloat", "Float")
def _(value: float = 0) -> tuple[float]:
return (value,)
@generate_functional_node("jamesWalker55", "JWFloatToInteger", "Float to Integer")
def _(
value: float = 0, mode: Literal["round", "floor", "ceiling"] = None
) -> tuple[int]:
if mode == "round":
return (round(value),)
elif mode == "floor":
return (math.floor(value),)
elif mode == "ceiling":
return (math.ceil(value),)
else:
raise NotImplementedError(f"Unsupported mode: {mode}")
@generate_functional_node("jamesWalker55", "JWFloatToString", "Float to String")
def _(value: float = 0, format_string: str = "{:.6g}") -> tuple[str]:
return (format_string.format(value),)
@generate_functional_node("jamesWalker55", "JWFloatAdd", "Float Add")
def _(a: float = 0, b: float = 0) -> tuple[float]:
return (a + b,)
@generate_functional_node("jamesWalker55", "JWFloatSub", "Float Subtract")
def _(a: float = 0, b: float = 0) -> tuple[float]:
return (a - b,)
@generate_functional_node("jamesWalker55", "JWFloatMul", "Float Multiply")
def _(a: float = 0, b: float = 0) -> tuple[float]:
return (a * b,)
@generate_functional_node("jamesWalker55", "JWFloatDiv", "Float Divide")
def _(a: float = 0, b: float = 0) -> tuple[float]:
return (a / b,)
@generate_functional_node("jamesWalker55", "JWFloatAbsolute", "Float Absolute Value")
def _(value: float = 0) -> tuple[float]:
return (abs(value),)
@generate_functional_node("jamesWalker55", "JWFloatMin", "Float Minimum")
def _(a: float = 0, b: float = 0) -> tuple[float]:
return (min(a, b),)
@generate_functional_node("jamesWalker55", "JWFloatMax", "Float Maximum")
def _(a: float = 0, b: float = 0) -> tuple[float]:
return (max(a, b),)
@generate_functional_node("jamesWalker55", "JWString", "String")
def _(text: str = "") -> tuple[str]:
return (text,)
@generate_functional_node("jamesWalker55", "JWStringToInteger", "String to Integer")
def _(text: str = "0") -> tuple[int]:
return (int(text),)
@generate_functional_node("jamesWalker55", "JWStringToFloat", "String to Float")
def _(text: str = "0.0") -> tuple[float]:
return (float(text),)
@generate_functional_node(
"jamesWalker55", "JWStringMultiline", "String (Multiline)", multiline_string=True
)
def _(text: str = "") -> tuple[str]:
return (text,)
@generate_functional_node("jamesWalker55", "JWStringConcat", "String Concatenate")
def _(a: str = "", b: str = "") -> tuple[str]:
return (a + b,)
@generate_functional_node("jamesWalker55", "JWStringReplace", "String Replace")
def _(source: str = "", to_replace: str = "", replace_with: str = "") -> tuple[str]:
return (source.replace(to_replace, replace_with),)
@generate_functional_node("jamesWalker55", "JWStringSplit", "String Split")
def _(
source: str = "a,b",
split_by: str = ",",
from_right: Literal["false", "true"] = None,
) -> tuple[str, str]:
from_right = from_right == "true"
if from_right:
splits = source.rsplit(split_by, 1)
else:
splits = source.split(split_by, 1)
match splits:
case a, b:
return (a, b)
case a:
return (a, "")
@generate_functional_node("jamesWalker55", "JWStringGetLine", "String Get Line")
def _(source: str = "", line_index: int = 0) -> tuple[str]:
return (source.splitlines()[line_index],)
@generate_functional_node("jamesWalker55", "JWStringUnescape", "String Unescape")
def _(text: str = "") -> tuple[str]:
"""parses '\\n' literals in a string to actual '\n' characters"""
# convert to bytes, while converting unicode to escaped literals
text_bytes = text.encode("ascii", "backslashreplace")
# convert back to string, parsing backslash escapes
text = text_bytes.decode("unicode-escape")
return (text,)