Skip to content

Commit 1395bce

Browse files
authored
update example_node to use V3 schema (#9723)
1 parent e9364ee commit 1395bce

File tree

1 file changed

+69
-94
lines changed

1 file changed

+69
-94
lines changed
Lines changed: 69 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,70 @@
1-
class Example:
1+
from typing_extensions import override
2+
3+
from comfy_api.latest import ComfyExtension, io
4+
5+
6+
class Example(io.ComfyNode):
27
"""
3-
A example node
8+
An example node
49

510
Class methods
611
-------------
7-
INPUT_TYPES (dict):
8-
Tell the main program input parameters of nodes.
9-
IS_CHANGED:
12+
define_schema (io.Schema):
13+
Tell the main program the metadata, input, output parameters of nodes.
14+
fingerprint_inputs:
1015
optional method to control when the node is re executed.
16+
check_lazy_status:
17+
optional method to control list of input names that need to be evaluated.
1118

12-
Attributes
13-
----------
14-
RETURN_TYPES (`tuple`):
15-
The type of each element in the output tuple.
16-
RETURN_NAMES (`tuple`):
17-
Optional: The name of each output in the output tuple.
18-
FUNCTION (`str`):
19-
The name of the entry-point method. For example, if `FUNCTION = "execute"` then it will run Example().execute()
20-
OUTPUT_NODE ([`bool`]):
21-
If this node is an output node that outputs a result/image from the graph. The SaveImage node is an example.
22-
The backend iterates on these output nodes and tries to execute all their parents if their parent graph is properly connected.
23-
Assumed to be False if not present.
24-
CATEGORY (`str`):
25-
The category the node should appear in the UI.
26-
DEPRECATED (`bool`):
27-
Indicates whether the node is deprecated. Deprecated nodes are hidden by default in the UI, but remain
28-
functional in existing workflows that use them.
29-
EXPERIMENTAL (`bool`):
30-
Indicates whether the node is experimental. Experimental nodes are marked as such in the UI and may be subject to
31-
significant changes or removal in future versions. Use with caution in production workflows.
32-
execute(s) -> tuple || None:
33-
The entry point method. The name of this method must be the same as the value of property `FUNCTION`.
34-
For example, if `FUNCTION = "execute"` then this method's name must be `execute`, if `FUNCTION = "foo"` then it must be `foo`.
3519
"""
36-
def __init__(self):
37-
pass
3820

3921
@classmethod
40-
def INPUT_TYPES(s):
22+
def define_schema(cls) -> io.Schema:
4123
"""
42-
Return a dictionary which contains config for all input fields.
43-
Some types (string): "MODEL", "VAE", "CLIP", "CONDITIONING", "LATENT", "IMAGE", "INT", "STRING", "FLOAT".
44-
Input types "INT", "STRING" or "FLOAT" are special values for fields on the node.
45-
The type can be a list for selection.
46-
47-
Returns: `dict`:
48-
- Key input_fields_group (`string`): Can be either required, hidden or optional. A node class must have property `required`
49-
- Value input_fields (`dict`): Contains input fields config:
50-
* Key field_name (`string`): Name of a entry-point method's argument
51-
* Value field_config (`tuple`):
52-
+ First value is a string indicate the type of field or a list for selection.
53-
+ Second value is a config for type "INT", "STRING" or "FLOAT".
24+
Return a schema which contains all information about the node.
25+
Some types: "Model", "Vae", "Clip", "Conditioning", "Latent", "Image", "Int", "String", "Float", "Combo".
26+
For outputs the "io.Model.Output" should be used, for inputs the "io.Model.Input" can be used.
27+
The type can be a "Combo" - this will be a list for selection.
5428
"""
55-
return {
56-
"required": {
57-
"image": ("IMAGE",),
58-
"int_field": ("INT", {
59-
"default": 0,
60-
"min": 0, #Minimum value
61-
"max": 4096, #Maximum value
62-
"step": 64, #Slider's step
63-
"display": "number", # Cosmetic only: display as "number" or "slider"
64-
"lazy": True # Will only be evaluated if check_lazy_status requires it
65-
}),
66-
"float_field": ("FLOAT", {
67-
"default": 1.0,
68-
"min": 0.0,
69-
"max": 10.0,
70-
"step": 0.01,
71-
"round": 0.001, #The value representing the precision to round to, will be set to the step value by default. Can be set to False to disable rounding.
72-
"display": "number",
73-
"lazy": True
74-
}),
75-
"print_to_screen": (["enable", "disable"],),
76-
"string_field": ("STRING", {
77-
"multiline": False, #True if you want the field to look like the one on the ClipTextEncode node
78-
"default": "Hello World!",
79-
"lazy": True
80-
}),
81-
},
82-
}
83-
84-
RETURN_TYPES = ("IMAGE",)
85-
#RETURN_NAMES = ("image_output_name",)
86-
87-
FUNCTION = "test"
88-
89-
#OUTPUT_NODE = False
90-
91-
CATEGORY = "Example"
92-
93-
def check_lazy_status(self, image, string_field, int_field, float_field, print_to_screen):
29+
return io.Schema(
30+
node_id="Example",
31+
display_name="Example Node",
32+
category="Example",
33+
inputs=[
34+
io.Image.Input("image"),
35+
io.Int.Input(
36+
"int_field",
37+
min=0,
38+
max=4096,
39+
step=64, # Slider's step
40+
display_mode=io.NumberDisplay.number, # Cosmetic only: display as "number" or "slider"
41+
lazy=True, # Will only be evaluated if check_lazy_status requires it
42+
),
43+
io.Float.Input(
44+
"float_field",
45+
default=1.0,
46+
min=0.0,
47+
max=10.0,
48+
step=0.01,
49+
round=0.001, #The value representing the precision to round to, will be set to the step value by default. Can be set to False to disable rounding.
50+
display_mode=io.NumberDisplay.number,
51+
lazy=True,
52+
),
53+
io.Combo.Input("print_to_screen", options=["enable", "disable"]),
54+
io.String.Input(
55+
"string_field",
56+
multiline=False, # True if you want the field to look like the one on the ClipTextEncode node
57+
default="Hello world!",
58+
lazy=True,
59+
)
60+
],
61+
outputs=[
62+
io.Image.Output(),
63+
],
64+
)
65+
66+
@classmethod
67+
def check_lazy_status(cls, image, string_field, int_field, float_field, print_to_screen):
9468
"""
9569
Return a list of input names that need to be evaluated.
9670

@@ -107,7 +81,8 @@ class Example:
10781
else:
10882
return []
10983

110-
def test(self, image, string_field, int_field, float_field, print_to_screen):
84+
@classmethod
85+
def execute(cls, image, string_field, int_field, float_field, print_to_screen) -> io.NodeOutput:
11186
if print_to_screen == "enable":
11287
print(f"""Your input contains:
11388
string_field aka input text: {string_field}
@@ -116,7 +91,7 @@ class Example:
11691
""")
11792
#do some processing on the image, in this example I just invert it
11893
image = 1.0 - image
119-
return (image,)
94+
return io.NodeOutput(image)
12095

12196
"""
12297
The node will always be re executed if any of the inputs change but
@@ -127,7 +102,7 @@ class Example:
127102
changes between executions the LoadImage node is executed again.
128103
"""
129104
#@classmethod
130-
#def IS_CHANGED(s, image, string_field, int_field, float_field, print_to_screen):
105+
#def fingerprint_inputs(s, image, string_field, int_field, float_field, print_to_screen):
131106
# return ""
132107

133108
# Set the web directory, any .js file in that directory will be loaded by the frontend as a frontend extension
@@ -143,13 +118,13 @@ async def get_hello(request):
143118
return web.json_response("hello")
144119

145120

146-
# A dictionary that contains all nodes you want to export with their names
147-
# NOTE: names should be globally unique
148-
NODE_CLASS_MAPPINGS = {
149-
"Example": Example
150-
}
121+
class ExampleExtension(ComfyExtension):
122+
@override
123+
async def get_node_list(self) -> list[type[io.ComfyNode]]:
124+
return [
125+
Example,
126+
]
127+
151128

152-
# A dictionary that contains the friendly/humanly readable titles for the nodes
153-
NODE_DISPLAY_NAME_MAPPINGS = {
154-
"Example": "Example Node"
155-
}
129+
async def comfy_entrypoint() -> ExampleExtension: # ComfyUI calls this to load your extension and its nodes.
130+
return ExampleExtension()

0 commit comments

Comments
 (0)