Skip to content

Tutorial 3 ‐ Concatenate Hello World

Akatsuzi edited this page Dec 5, 2023 · 9 revisions

In this tutorial, we are going to build the Concatenate Hello World node below

image

The node concatenates two text strings and outputs these as a combined string.

1. Open the nodes.py file in Notepad++ or an IDE

  • Windows Notepad is not recommended.

2. Setup the class structure

class ConcatenateHelloWorld:

    @classmethod
    def INPUT_TYPES(cls):

    RETURN_TYPES = ("STRING",)
    FUNCTION = "concatenate_text"
    CATEGORY = "Tutorial Nodes"

    def concatenate_text(self, text1, text2):
       
        return {}

  • RETURN_TYPES defines the output types
  • FUNCTION is used to specify the main function
  • CATEGORY specifies where the node will be included in the ComfyUI node menu
  • The first parameter in the main function should always be self
  • The return statement is used to specify the objects that are passed to the node outputs

5. Add the input parameters into the class structure

  • The input parameters must match the parameters in the concatenate_text function
       return {"required": {
                    "text1": ("STRING", {"multiline": False, "default": "Hello"}),
                    "text2": ("STRING", {"multiline": False, "default": "World"}),
                    }
                }

8. Add the main function into the class structure

  • In this case the function consists of a text formula.
  • Python is fussy about indents. Take care to use precise indentation.
        text_out = text1 + " " + text2

11. Check the completed node

It should look like this:

class ConcatenateHelloWorld:     

    @classmethod
    def INPUT_TYPES(cls):
               
        return {"required": {       
                    "text1": ("STRING", {"multiline": False, "default": "Hello"}),
                    "text2": ("STRING", {"multiline": False, "default": "World"}),
                    }
                }

    RETURN_TYPES = ("STRING",)
    FUNCTION = "concatenate_text"
    CATEGORY = "Tutorial Nodes"

    def concatenate_text(self, text1, text2):

        text_out = text1 + " " + text2
        
        return (text_out,)

Add the node to the __init__.py file

1. Add a class mapping for the Hello World node in the __init__.py file

    "Concatenate Hello World": ConcatenateHelloWorld,

2. Check the updated __init__.py file

It should look like this:

from .nodes.nodes import *

NODE_CLASS_MAPPINGS = {
    "Print Hello World": PrintHelloWorld,
    "Concatenate Hello World": ConcatenateHelloWorld,
    }
    
print("\033[34mComfyUI Tutorial Nodes: \033[92mLoaded\033[0m")

3. You can now start ComfyUI to test the node