> ## Documentation Index
> Fetch the complete documentation index at: https://docs.comfy.org/llms.txt
> Use this file to discover all available pages before exploring further.

# 代码接口

> 什么是自定义节点以及如何使用它们？

## 概述

自定义节点是 Python 代码和可能的模型权重的组合。自定义节点非常强大，允许 Comfy 社区在 ComfyUI 中构建自己的功能。

如果您更喜欢阅读代码，可以查看仓库中的[示例](https://github.com/Comfy-Org/ComfyUI/blob/master/custom_nodes/example_node.py.example)。否则，我们将在下面探索 Comfy 的一个内置节点。

## 接口

让我们通过查看 ComfyUI 内置的 [Load Checkpoint](https://github.com/Comfy-Org/ComfyUI/blob/master/nodes.py#L529C7-L529C29) 节点来研究自定义节点的接口。这个节点用于加载检查点文件。

### 函数

每个自定义节点都可以实现以下方法。

#### INPUT\_TYPES

这定义了自定义节点可以接收的输入参数。

```python theme={null}
    @classmethod
    def INPUT_TYPES(s):
        return {
            "required": {
                "ckpt_name": (folder_paths.
            get_filename_list("checkpoints"),),
        }}
```

在这里，您可以看到输入类型定义为 Python 中的字典。您可以定义输入为 `required`、`hidden` 或 `optional`。

每个输入必须有一个类型（例如 VAE）。如果您使用其中一个特殊内置类型：`INT`、`STRING` 或 `FLOAT`，那么它们可以是一个列表。

<Accordion title="可选字段">
  `default`: 您可以定义一个默认值。

  `min`: 您可以定义一个最小值。

  `max`: 您可以定义一个最大值。

  `step`: 您可以使用一个滑块。
</Accordion>

#### IS\_CHANGED

可选。允许节点控制何时重新执行。ComfyUI 尝试仅执行已更改的节点以提高效率。

### 属性

#### RETURN\_TYPES

Tuple. 输出元组中每个元素的类型。

```python theme={null}
RETURN_TYPES = ("MODEL", "CLIP", "VAE")
```

#### RETURN\_NAMES

可选。输出元组中每个输出的名称。

```python theme={null}
CATEGORY = "loaders"
```

#### FUNCTION

自定义节点类中 Python 函数的名称。

对于 LoadCheckpointSimple，函数定义如下：

```python theme={null}
FUNCTION = "load_checkpoint"
```

#### 入口点函数

当节点被调用时，此 Python 函数会被调用。需要与 FUNCTION 中的字符串匹配。

```python theme={null}
def load_checkpoint(self, ckpt_name, output_vae=True, output_clip=True):
        ckpt_path = folder_paths.get_full_path("checkpoints", ckpt_name)
        out = comfy.sd.load_checkpoint_guess_config(ckpt_path, output_vae=True, output_clip=True, embedding_directory=folder_paths.get_folder_paths("embeddings"))
        return out[:3]
```

#### OUTPUT\_NODE

Bool. 默认为 False。如果您的节点将输出结果/图像，请设置为 true。

#### CATEGORY

String. 您希望节点在 UI 中出现的类别。

```python theme={null}
CATEGORY = "loaders"
```

#### WEB\_DIRECTORY

自定义节点可以有自定义 UI。

此属性设置 web 目录。该目录中的任何 `.js` 文件将被加载为前端扩展。

自定义节点还可以在 `WEB_DIRECTORY/docs` 文件夹中包含 markdown 文档。请参阅[帮助页面](/zh/custom-nodes/help_page)部分，了解如何为您的节点添加丰富的文档。

#### NODE\_CLASS\_MAPPINGS

一个包含您希望导出的所有节点及其类名的字典。类名应该唯一。这意味着您可以在一个“自定义节点”中定义多个节点，并一起导出它们。

```python theme={null}
NODE_CLASS_MAPPINGS = {
    "CheckpointLoaderSimple": CheckpointLoaderSimple,
}
```

#### NODE\_DISPLAY\_NAME\_MAPPINGS

如果您希望为每个节点定义更易读的名称。

```python theme={null}
NODE_DISPLAY_NAME_MAPPINGS = {
    "CheckpointLoaderSimple": "Load Checkpoint",
}
```
