comfyui节点编写示例文件(上)

class Example:
    """
    A example node

    Class methods  # 类方法
    -------------
    INPUT_TYPES (dict):  # 告诉主程序输入节点参数。
        Tell the main program input parameters of nodes.  
    IS_CHANGED:  # 这个类方法是可选的,控制何时重新执行节点的可选方法。
        optional method to control when the node is re executed.  

    Attributes
    ----------
    RETURN_TYPES (`tuple`):  #  输出元组中每个元素的类型 
        The type of each element in the output tulple.
    RETURN_NAMES (`tuple`):  # 可选属性,为输出元组中的每个元素提供描述性名称。
        Optional: The name of each output in the output tulple.
    FUNCTION (`str`):  # 入口方法的名称
        The name of the entry-point method. For example, if `FUNCTION = "execute"` then it will run Example().execute()
    OUTPUT_NODE ([`bool`]):  # 如果此节点是输出节点,则从图中输出结果/图像
        If this node is an output node that outputs a result/image from the graph. The SaveImage node is an example.
        The backend iterates on these output nodes and tries to execute all their parents if their parent graph is properly connected.
        Assumed to be False if not present.
    CATEGORY (`str`):  # 节点在用户界面中应该出现的类别。这有助于用户根据功能查找和组织节点。
        The category the node should appear in the UI.
    execute(s) -> tuple || None:  # 入口方法。该方法的名称必须与 FUNCTION 属性的值相同。
        The entry point method. The name of this method must be the same as the value of property `FUNCTION`.
        For example, if `FUNCTION = "execute"` then this method's name must be `execute`, if `FUNCTION = "foo"` then it must be `foo`.
    """
    def __init__(self):
        pass
    
    @classmethod
    def INPUT_TYPES(s):
        """
            Return a dictionary which contains config for all input fields.  # 这个函数的作用的返回一个字典,其中包含所有输入字段的配置。 
            Some types (string): "MODEL", "VAE", "CLIP", "CONDITIONING", "LATENT", "IMAGE", "INT", "STRING", "FLOAT".  
            # 部分类型(字符串):“MODEL”、“VAE”、“CLIP”、“CONDITIONING”、“LATENT”、“IMAGE”、“INT”、“string”、“FLOAT”。
            Input types "INT", "STRING" or "FLOAT" are special values for fields on the node.  # 输入类型“INT”,“STRING”或“FLOAT”是节点上字段的特殊值。
            The type can be a list for selection.  # 类型可以是一个可供选择的列表。

            Returns: `dict`:
                - Key input_fields_group (`string`): Can be either required, hidden or optional. A node class must have property `required`
                # -关键字input_fields_group (' string '):可以是必需的,隐藏的或可选的。节点类必须具有属性“required”。
                - Value input_fields (`dict`): Contains input fields config:
                # - Value input_fields (' dict '):包含输入字段config:
                    * Key field_name (`string`): Name of a entry-point method's argument
                    # 入口点方法的参数名
                    * Value field_config (`tuple`):
                        + First value is a string indicate the type of field or a list for selection.
                        # +第一个值是字符串,表示字段的类型或可供选择的列表。
                        + Secound value is a config for type "INT", "STRING" or "FLOAT".
                        # +第二个值是类型为“INT”,“STRING”或“FLOAT”的配置。
        """
        return {
            "required": {
                "image": ("IMAGE",),
                "int_field": ("INT", {
                    "default": 0, 
                    "min": 0, #Minimum value
                    "max": 4096, #Maximum value
                    "step": 64, #Slider's step
                    "display": "number" # Cosmetic only: display as "number" or "slider"
                }),
                "float_field": ("FLOAT", {
                    "default": 1.0,
                    "min": 0.0,
                    "max": 10.0,
                    "step": 0.01,
                    "round": 0.001, #The value represeting the precision to round to, will be set to the step value by default. Can be set to False to disable rounding.
                    "display": "number"}),
                "print_to_screen": (["enable", "disable"],),
                "string_field": ("STRING", {
                    "multiline": False, #True if you want the field to look like the one on the ClipTextEncode node
                    "default": "Hello World!"
                }),
            },
        }

    RETURN_TYPES = ("IMAGE",)
    #RETURN_NAMES = ("image_output_name",)

    FUNCTION = "test"

    #OUTPUT_NODE = False

    CATEGORY = "Example"

    def test(self, image, string_field, int_field, float_field, print_to_screen):
        if print_to_screen == "enable":
            print(f"""Your input contains:
                string_field aka input text: {string_field}
                int_field: {int_field}
                float_field: {float_field}
            """)
        #do some processing on the image, in this example I just invert it
        image = 1.0 - image
        return (image,)

    """
        The node will always be re executed if any of the inputs change but
        this method can be used to force the node to execute again even when the inputs don't change.
        You can make this node return a number or a string. This value will be compared to the one returned the last time the node was
        executed, if it is different the node will be executed again.
        This method is used in the core repo for the LoadImage node where they return the image hash as a string, if the image hash
        changes between executions the LoadImage node is executed again.
    """
    #@classmethod
    #def IS_CHANGED(s, image, string_field, int_field, float_field, print_to_screen):
    #    return ""

# A dictionary that contains all nodes you want to export with their names
# NOTE: names should be globally unique  注意:名称应该是全局唯一的
NODE_CLASS_MAPPINGS = {
    "Example": Example  # 将节点名称与节点类进行映射。在这个示例中,将节点名称"Example"与类Example进行映射。
}

# A dictionary that contains the friendly/humanly readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS = {
    "Example": "Example Node"  # 节点名称与友好/可读的节点标题进行映射。在这个示例中,将节点名称"Example"与节点标题"Example Node"进行映射。
}

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值