from __future__ import annotations import copy import inspect from abc import ABC, abstractmethod from collections import Counter from collections.abc import Iterable from dataclasses import asdict, dataclass, field from enum import Enum from typing import Any, Callable, Literal, TypedDict, TypeVar, TYPE_CHECKING from typing_extensions import NotRequired, final # used for type hinting import torch if TYPE_CHECKING: from spandrel import ImageModelDescriptor from comfy.clip_vision import ClipVisionModel from comfy.clip_vision import Output as ClipVisionOutput_ from comfy.bg_removal_model import BackgroundRemovalModel from comfy.controlnet import ControlNet from comfy.hooks import HookGroup, HookKeyframeGroup from comfy.model_patcher import ModelPatcher from comfy.samplers import CFGGuider, Sampler from comfy.sd import CLIP, VAE from comfy.sd import StyleModel as StyleModel_ from comfy_api.input import VideoInput, CurveInput as CurveInput_ from comfy_api.internal import (_ComfyNodeInternal, _NodeOutputInternal, classproperty, copy_class, first_real_override, is_class, prune_dict, shallow_clone_class) from comfy_execution.graph_utils import ExecutionBlocker from ._util import MESH, VOXEL, SPLAT, SVG as _SVG, File3D class FolderType(str, Enum): input = "input" output = "output" temp = "temp" class UploadType(str, Enum): image = "image_upload" audio = "audio_upload" video = "video_upload" model = "file_upload" class RemoteOptions: def __init__(self, route: str, refresh_button: bool, control_after_refresh: Literal["first", "last"]="first", timeout: int=None, max_retries: int=None, refresh: int=None): self.route = route """The route to the remote source.""" self.refresh_button = refresh_button """Specifies whether to show a refresh button in the UI below the widget.""" self.control_after_refresh = control_after_refresh """Specifies the control after the refresh button is clicked. If "first", the first item will be automatically selected, and so on.""" self.timeout = timeout """The maximum amount of time to wait for a response from the remote source in milliseconds.""" self.max_retries = max_retries """The maximum number of retries before aborting the request.""" self.refresh = refresh """The TTL of the remote input's value in milliseconds. Specifies the interval at which the remote input's value is refreshed.""" def as_dict(self): return prune_dict({ "route": self.route, "refresh_button": self.refresh_button, "control_after_refresh": self.control_after_refresh, "timeout": self.timeout, "max_retries": self.max_retries, "refresh": self.refresh, }) class NumberDisplay(str, Enum): number = "number" slider = "slider" gradient_slider = "gradientslider" class ControlAfterGenerate(str, Enum): fixed = "fixed" increment = "increment" decrement = "decrement" randomize = "randomize" class _ComfyType(ABC): Type = Any io_type: str = None # NOTE: this is a workaround to make the decorator return the correct type T = TypeVar("T", bound=type) def comfytype(io_type: str, **kwargs): ''' Decorator to mark nested classes as ComfyType; io_type will be bound to the class. A ComfyType may have the following attributes: - Type = - class Input(Input): ... - class Output(Output): ... ''' def decorator(cls: T) -> T: if isinstance(cls, _ComfyType) or issubclass(cls, _ComfyType): # clone Input and Output classes to avoid modifying the original class new_cls = cls if hasattr(new_cls, "Input"): new_cls.Input = copy_class(new_cls.Input) if hasattr(new_cls, "Output"): new_cls.Output = copy_class(new_cls.Output) else: # copy class attributes except for special ones that shouldn't be in type() cls_dict = { k: v for k, v in cls.__dict__.items() if k not in ('__dict__', '__weakref__', '__module__', '__doc__') } # new class new_cls: ComfyTypeIO = type( cls.__name__, (cls, ComfyTypeIO), cls_dict ) # metadata preservation new_cls.__module__ = cls.__module__ new_cls.__doc__ = cls.__doc__ # assign ComfyType attributes, if needed new_cls.io_type = io_type if hasattr(new_cls, "Input") and new_cls.Input is not None: new_cls.Input.Parent = new_cls if hasattr(new_cls, "Output") and new_cls.Output is not None: new_cls.Output.Parent = new_cls return new_cls return decorator def Custom(io_type: str) -> type[ComfyTypeIO]: '''Create a ComfyType for a custom io_type.''' @comfytype(io_type=io_type) class CustomComfyType(ComfyTypeIO): ... return CustomComfyType class _IO_V3: ''' Base class for V3 Inputs and Outputs. ''' Parent: _ComfyType = None def __init__(self): pass def validate(self): pass @property def io_type(self): return self.Parent.io_type @property def Type(self): return self.Parent.Type class Input(_IO_V3): ''' Base class for a V3 Input. ''' def __init__(self, id: str, display_name: str=None, optional=False, tooltip: str=None, lazy: bool=None, extra_dict=None, raw_link: bool=None, advanced: bool=None): super().__init__() self.id = id self.display_name = display_name self.optional = optional self.tooltip = tooltip self.lazy = lazy self.extra_dict = extra_dict if extra_dict is not None else {} self.rawLink = raw_link self.advanced = advanced def as_dict(self): return prune_dict({ "display_name": self.display_name, "optional": self.optional, "tooltip": self.tooltip, "lazy": self.lazy, "rawLink": self.rawLink, "advanced": self.advanced, }) | prune_dict(self.extra_dict) def get_io_type(self): return self.io_type def get_all(self) -> list[Input]: return [self] class WidgetInput(Input): ''' Base class for a V3 Input with widget. ''' def __init__(self, id: str, display_name: str=None, optional=False, tooltip: str=None, lazy: bool=None, default: Any=None, socketless: bool=None, widget_type: str=None, force_input: bool=None, extra_dict=None, raw_link: bool=None, advanced: bool=None): super().__init__(id, display_name, optional, tooltip, lazy, extra_dict, raw_link, advanced) self.default = default self.socketless = socketless self.widget_type = widget_type self.force_input = force_input def as_dict(self): return super().as_dict() | prune_dict({ "default": self.default, "socketless": self.socketless, "widgetType": self.widget_type, "forceInput": self.force_input, }) def get_io_type(self): return self.widget_type if self.widget_type is not None else super().get_io_type() class Output(_IO_V3): def __init__(self, id: str=None, display_name: str=None, tooltip: str=None, is_output_list=False): self.id = id self.display_name = display_name if display_name else id self.tooltip = tooltip self.is_output_list = is_output_list def as_dict(self): display_name = self.display_name if self.display_name else self.id return prune_dict({ "display_name": display_name, "tooltip": self.tooltip, "is_output_list": self.is_output_list, }) def get_io_type(self): return self.io_type class ComfyTypeI(_ComfyType): '''ComfyType subclass that only has a default Input class - intended for types that only have Inputs.''' class Input(Input): ... class ComfyTypeIO(ComfyTypeI): '''ComfyType subclass that has default Input and Output classes; useful for types with both Inputs and Outputs.''' class Output(Output): ... @comfytype(io_type="BOOLEAN") class Boolean(ComfyTypeIO): Type = bool class Input(WidgetInput): '''Boolean input.''' def __init__(self, id: str, display_name: str=None, optional=False, tooltip: str=None, lazy: bool=None, default: bool=None, label_on: str=None, label_off: str=None, socketless: bool=None, force_input: bool=None, extra_dict=None, raw_link: bool=None, advanced: bool=None): super().__init__(id, display_name, optional, tooltip, lazy, default, socketless, None, force_input, extra_dict, raw_link, advanced) self.label_on = label_on self.label_off = label_off self.default: bool def as_dict(self): return super().as_dict() | prune_dict({ "label_on": self.label_on, "label_off": self.label_off, }) @comfytype(io_type="INT") class Int(ComfyTypeIO): Type = int class Input(WidgetInput): '''Integer input.''' def __init__(self, id: str, display_name: str=None, optional=False, tooltip: str=None, lazy: bool=None, default: int=None, min: int=None, max: int=None, step: int=None, control_after_generate: bool | ControlAfterGenerate=None, display_mode: NumberDisplay=None, socketless: bool=None, force_input: bool=None, extra_dict=None, raw_link: bool=None, advanced: bool=None): super().__init__(id, display_name, optional, tooltip, lazy, default, socketless, None, force_input, extra_dict, raw_link, advanced) self.min = min self.max = max self.step = step self.control_after_generate = control_after_generate self.display_mode = display_mode self.default: int def as_dict(self): return super().as_dict() | prune_dict({ "min": self.min, "max": self.max, "step": self.step, "control_after_generate": self.control_after_generate, "display": self.display_mode.value if self.display_mode else None, }) @comfytype(io_type="FLOAT") class Float(ComfyTypeIO): Type = float class Input(WidgetInput): '''Float input.''' def __init__(self, id: str, display_name: str=None, optional=False, tooltip: str=None, lazy: bool=None, default: float=None, min: float=None, max: float=None, step: float=None, round: float=None, display_mode: NumberDisplay=None, gradient_stops: list[dict]=None, socketless: bool=None, force_input: bool=None, extra_dict=None, raw_link: bool=None, advanced: bool=None): super().__init__(id, display_name, optional, tooltip, lazy, default, socketless, None, force_input, extra_dict, raw_link, advanced) self.min = min self.max = max self.step = step self.round = round self.display_mode = display_mode self.gradient_stops = gradient_stops self.default: float def as_dict(self): return super().as_dict() | prune_dict({ "min": self.min, "max": self.max, "step": self.step, "round": self.round, "display": self.display_mode, "gradient_stops": self.gradient_stops, }) @comfytype(io_type="STRING") class String(ComfyTypeIO): Type = str class Input(WidgetInput): '''String input.''' def __init__(self, id: str, display_name: str=None, optional=False, tooltip: str=None, lazy: bool=None, multiline=False, placeholder: str=None, default: str=None, dynamic_prompts: bool=None, socketless: bool=None, force_input: bool=None, extra_dict=None, raw_link: bool=None, advanced: bool=None): super().__init__(id, display_name, optional, tooltip, lazy, default, socketless, None, force_input, extra_dict, raw_link, advanced) self.multiline = multiline self.placeholder = placeholder self.dynamic_prompts = dynamic_prompts self.default: str def as_dict(self): return super().as_dict() | prune_dict({ "multiline": self.multiline, "placeholder": self.placeholder, "dynamicPrompts": self.dynamic_prompts, }) @comfytype(io_type="COMBO") class Combo(ComfyTypeIO): Type = str class Input(WidgetInput): """Combo input (dropdown).""" Type = str def __init__( self, id: str, options: list[str] | list[int] | type[Enum] = None, display_name: str=None, optional=False, tooltip: str=None, lazy: bool=None, default: str | int | Enum = None, control_after_generate: bool | ControlAfterGenerate=None, upload: UploadType=None, image_folder: FolderType=None, remote: RemoteOptions=None, socketless: bool=None, extra_dict=None, raw_link: bool=None, advanced: bool=None, ): if isinstance(options, type) and issubclass(options, Enum): options = [v.value for v in options] if isinstance(default, Enum): default = default.value super().__init__(id, display_name, optional, tooltip, lazy, default, socketless, None, None, extra_dict, raw_link, advanced) self.multiselect = False self.options = options self.control_after_generate = control_after_generate self.upload = upload self.image_folder = image_folder self.remote = remote self.default: str def as_dict(self): return super().as_dict() | prune_dict({ "multiselect": self.multiselect, "options": self.options, "control_after_generate": self.control_after_generate, **({self.upload.value: True} if self.upload is not None else {}), "image_folder": self.image_folder.value if self.image_folder else None, "remote": self.remote.as_dict() if self.remote else None, }) class Output(Output): def __init__(self, id: str=None, display_name: str=None, options: list[str]=None, tooltip: str=None, is_output_list=False): super().__init__(id, display_name, tooltip, is_output_list) self.options = options if options is not None else [] @comfytype(io_type="COMBO") class MultiCombo(ComfyTypeI): '''Multiselect Combo input (dropdown for selecting potentially more than one value).''' Type = list[str] class Input(Combo.Input): def __init__(self, id: str, options: list[str], display_name: str=None, optional=False, tooltip: str=None, lazy: bool=None, default: list[str]=None, placeholder: str=None, chip: bool=None, control_after_generate: bool | ControlAfterGenerate=None, socketless: bool=None, extra_dict=None, raw_link: bool=None, advanced: bool=None): super().__init__(id, options, display_name, optional, tooltip, lazy, default, control_after_generate, socketless=socketless, extra_dict=extra_dict, raw_link=raw_link, advanced=advanced) self.multiselect = True self.placeholder = placeholder self.chip = chip self.default: list[str] def as_dict(self): # Frontend expects `multi_select` to be an object config (not a boolean). # Keep top-level `multiselect` from Combo.Input for backwards compatibility. return super().as_dict() | prune_dict({ "multi_select": prune_dict({ "placeholder": self.placeholder, "chip": self.chip, }), }) @comfytype(io_type="IMAGE") class Image(ComfyTypeIO): Type = torch.Tensor @comfytype(io_type="WAN_CAMERA_EMBEDDING") class WanCameraEmbedding(ComfyTypeIO): Type = torch.Tensor @comfytype(io_type="WEBCAM") class Webcam(ComfyTypeIO): Type = str class Input(WidgetInput): """Webcam input.""" Type = str def __init__( self, id: str, display_name: str=None, optional=False, tooltip: str=None, lazy: bool=None, default: str=None, socketless: bool=None, extra_dict=None, raw_link: bool=None, advanced: bool=None ): super().__init__(id, display_name, optional, tooltip, lazy, default, socketless, None, None, extra_dict, raw_link, advanced) @comfytype(io_type="MASK") class Mask(ComfyTypeIO): Type = torch.Tensor @comfytype(io_type="LATENT") class Latent(ComfyTypeIO): '''Latents are stored as a dictionary.''' class LatentDict(TypedDict): samples: torch.Tensor '''Latent tensors.''' noise_mask: NotRequired[torch.Tensor] batch_index: NotRequired[list[int]] type: NotRequired[str] '''Only needed if dealing with these types: audio, hunyuan3dv2''' Type = LatentDict @comfytype(io_type="CONDITIONING") class Conditioning(ComfyTypeIO): class PooledDict(TypedDict): pooled_output: torch.Tensor '''Pooled output from CLIP.''' control: NotRequired[ControlNet] '''ControlNet to apply to conditioning.''' control_apply_to_uncond: NotRequired[bool] '''Whether to apply ControlNet to matching negative conditioning at sample time, if applicable.''' cross_attn_controlnet: NotRequired[torch.Tensor] '''CrossAttn from CLIP to use for controlnet only.''' pooled_output_controlnet: NotRequired[torch.Tensor] '''Pooled output from CLIP to use for controlnet only.''' gligen: NotRequired[tuple[str, Gligen, list[tuple[torch.Tensor, int, ...]]]] '''GLIGEN to apply to conditioning.''' area: NotRequired[tuple[int, ...] | tuple[str, float, ...]] '''Set area of conditioning. First half of values apply to dimensions, the second half apply to coordinates. By default, the dimensions are based on total pixel amount, but the first value can be set to "percentage" to use a percentage of the image size instead. (1024, 1024, 0, 0) would apply conditioning to the top-left 1024x1024 pixels. ("percentage", 0.5, 0.5, 0, 0) would apply conditioning to the top-left 50% of the image.''' # TODO: verify its actually top-left strength: NotRequired[float] '''Strength of conditioning. Default strength is 1.0.''' mask: NotRequired[torch.Tensor] '''Mask to apply conditioning to.''' mask_strength: NotRequired[float] '''Strength of conditioning mask. Default strength is 1.0.''' set_area_to_bounds: NotRequired[bool] '''Whether conditioning mask should determine bounds of area - if set to false, latents are sampled at full resolution and result is applied in mask.''' concat_latent_image: NotRequired[torch.Tensor] '''Used for inpainting and specific models.''' concat_mask: NotRequired[torch.Tensor] '''Used for inpainting and specific models.''' concat_image: NotRequired[torch.Tensor] '''Used by SD_4XUpscale_Conditioning.''' noise_augmentation: NotRequired[float] '''Used by SD_4XUpscale_Conditioning.''' hooks: NotRequired[HookGroup] '''Applies hooks to conditioning.''' default: NotRequired[bool] '''Whether to this conditioning is 'default'; default conditioning gets applied to any areas of the image that have no masks/areas applied, assuming at least one area/mask is present during sampling.''' start_percent: NotRequired[float] '''Determines relative step to begin applying conditioning, expressed as a float between 0.0 and 1.0.''' end_percent: NotRequired[float] '''Determines relative step to end applying conditioning, expressed as a float between 0.0 and 1.0.''' clip_start_percent: NotRequired[float] '''Internal variable for conditioning scheduling - start of application, expressed as a float between 0.0 and 1.0.''' clip_end_percent: NotRequired[float] '''Internal variable for conditioning scheduling - end of application, expressed as a float between 0.0 and 1.0.''' attention_mask: NotRequired[torch.Tensor] '''Masks text conditioning; used by StyleModel among others.''' attention_mask_img_shape: NotRequired[tuple[int, ...]] '''Masks text conditioning; used by StyleModel among others.''' unclip_conditioning: NotRequired[list[dict]] '''Used by unCLIP.''' conditioning_lyrics: NotRequired[torch.Tensor] '''Used by AceT5Model.''' seconds_start: NotRequired[float] '''Used by StableAudio.''' seconds_total: NotRequired[float] '''Used by StableAudio.''' lyrics_strength: NotRequired[float] '''Used by AceStepAudio.''' width: NotRequired[int] '''Used by certain models (e.g. CLIPTextEncodeSDXL/Refiner, PixArtAlpha).''' height: NotRequired[int] '''Used by certain models (e.g. CLIPTextEncodeSDXL/Refiner, PixArtAlpha).''' aesthetic_score: NotRequired[float] '''Used by CLIPTextEncodeSDXL/Refiner.''' crop_w: NotRequired[int] '''Used by CLIPTextEncodeSDXL.''' crop_h: NotRequired[int] '''Used by CLIPTextEncodeSDXL.''' target_width: NotRequired[int] '''Used by CLIPTextEncodeSDXL.''' target_height: NotRequired[int] '''Used by CLIPTextEncodeSDXL.''' reference_latents: NotRequired[list[torch.Tensor]] '''Used by ReferenceLatent.''' guidance: NotRequired[float] '''Used by Flux-like models with guidance embed.''' guiding_frame_index: NotRequired[int] '''Used by Hunyuan ImageToVideo.''' ref_latent: NotRequired[torch.Tensor] '''Used by Hunyuan ImageToVideo.''' keyframe_idxs: NotRequired[list[int]] '''Used by LTXV.''' frame_rate: NotRequired[float] '''Used by LTXV.''' stable_cascade_prior: NotRequired[torch.Tensor] '''Used by StableCascade.''' elevation: NotRequired[list[float]] '''Used by SV3D.''' azimuth: NotRequired[list[float]] '''Used by SV3D.''' motion_bucket_id: NotRequired[int] '''Used by SVD-like models.''' fps: NotRequired[int] '''Used by SVD-like models.''' augmentation_level: NotRequired[float] '''Used by SVD-like models.''' clip_vision_output: NotRequired[ClipVisionOutput_] '''Used by WAN-like models.''' vace_frames: NotRequired[torch.Tensor] '''Used by WAN VACE.''' vace_mask: NotRequired[torch.Tensor] '''Used by WAN VACE.''' vace_strength: NotRequired[float] '''Used by WAN VACE.''' camera_conditions: NotRequired[Any] # TODO: assign proper type once defined '''Used by WAN Camera.''' time_dim_concat: NotRequired[torch.Tensor] '''Used by WAN Phantom Subject.''' time_dim_replace: NotRequired[torch.Tensor] '''Used by Kandinsky5 I2V.''' CondList = list[tuple[torch.Tensor, PooledDict]] Type = CondList @comfytype(io_type="SAMPLER") class Sampler(ComfyTypeIO): if TYPE_CHECKING: Type = Sampler @comfytype(io_type="SIGMAS") class Sigmas(ComfyTypeIO): Type = torch.Tensor @comfytype(io_type="NOISE") class Noise(ComfyTypeIO): Type = torch.Tensor @comfytype(io_type="GUIDER") class Guider(ComfyTypeIO): if TYPE_CHECKING: Type = CFGGuider @comfytype(io_type="CLIP") class Clip(ComfyTypeIO): if TYPE_CHECKING: Type = CLIP @comfytype(io_type="CONTROL_NET") class ControlNet(ComfyTypeIO): if TYPE_CHECKING: Type = ControlNet @comfytype(io_type="VAE") class Vae(ComfyTypeIO): if TYPE_CHECKING: Type = VAE @comfytype(io_type="MODEL") class Model(ComfyTypeIO): if TYPE_CHECKING: Type = ModelPatcher @comfytype(io_type="BACKGROUND_REMOVAL") class BackgroundRemoval(ComfyTypeIO): if TYPE_CHECKING: Type = BackgroundRemovalModel @comfytype(io_type="CLIP_VISION") class ClipVision(ComfyTypeIO): if TYPE_CHECKING: Type = ClipVisionModel @comfytype(io_type="CLIP_VISION_OUTPUT") class ClipVisionOutput(ComfyTypeIO): if TYPE_CHECKING: Type = ClipVisionOutput_ @comfytype(io_type="STYLE_MODEL") class StyleModel(ComfyTypeIO): if TYPE_CHECKING: Type = StyleModel_ @comfytype(io_type="GLIGEN") class Gligen(ComfyTypeIO): '''ModelPatcher that wraps around a 'Gligen' model.''' if TYPE_CHECKING: Type = ModelPatcher @comfytype(io_type="UPSCALE_MODEL") class UpscaleModel(ComfyTypeIO): if TYPE_CHECKING: Type = ImageModelDescriptor @comfytype(io_type="LATENT_UPSCALE_MODEL") class LatentUpscaleModel(ComfyTypeIO): Type = Any @comfytype(io_type="AUDIO") class Audio(ComfyTypeIO): class AudioDict(TypedDict): waveform: torch.Tensor sampler_rate: int Type = AudioDict @comfytype(io_type="VIDEO") class Video(ComfyTypeIO): if TYPE_CHECKING: Type = VideoInput @comfytype(io_type="SVG") class SVG(ComfyTypeIO): Type = _SVG @comfytype(io_type="LORA_MODEL") class LoraModel(ComfyTypeIO): Type = dict[str, torch.Tensor] @comfytype(io_type="LOSS_MAP") class LossMap(ComfyTypeIO): class LossMapDict(TypedDict): loss: list[torch.Tensor] Type = LossMapDict @comfytype(io_type="VOXEL") class Voxel(ComfyTypeIO): Type = VOXEL @comfytype(io_type="MESH") class Mesh(ComfyTypeIO): Type = MESH @comfytype(io_type="SPLAT") class Splat(ComfyTypeIO): Type = SPLAT @comfytype(io_type="FILE_3D") class File3DAny(ComfyTypeIO): """General 3D file type - accepts any supported 3D format.""" Type = File3D @comfytype(io_type="FILE_3D_GLB") class File3DGLB(ComfyTypeIO): """GLB format 3D file - binary glTF, best for web and cross-platform.""" Type = File3D @comfytype(io_type="FILE_3D_GLTF") class File3DGLTF(ComfyTypeIO): """GLTF format 3D file - JSON-based glTF with external resources.""" Type = File3D @comfytype(io_type="FILE_3D_FBX") class File3DFBX(ComfyTypeIO): """FBX format 3D file - best for game engines and animation.""" Type = File3D @comfytype(io_type="FILE_3D_OBJ") class File3DOBJ(ComfyTypeIO): """OBJ format 3D file - simple geometry format.""" Type = File3D @comfytype(io_type="FILE_3D_STL") class File3DSTL(ComfyTypeIO): """STL format 3D file - best for 3D printing.""" Type = File3D @comfytype(io_type="FILE_3D_USDZ") class File3DUSDZ(ComfyTypeIO): """USDZ format 3D file - Apple AR format.""" Type = File3D @comfytype(io_type="FILE_3D_PLY") class File3DPLY(ComfyTypeIO): """PLY format 3D file - point cloud or Gaussian splat.""" Type = File3D @comfytype(io_type="FILE_3D_SPLAT") class File3DSPLAT(ComfyTypeIO): """SPLAT format 3D file - 3D Gaussian splat.""" Type = File3D @comfytype(io_type="FILE_3D_SPZ") class File3DSPZ(ComfyTypeIO): """SPZ format 3D file - compressed 3D Gaussian splat.""" Type = File3D @comfytype(io_type="FILE_3D_KSPLAT") class File3DKSPLAT(ComfyTypeIO): """KSPLAT format 3D file - 3D Gaussian splat.""" Type = File3D @comfytype(io_type="FILE_3D_SPLAT_ANY") class File3DSplatAny(ComfyTypeIO): """General 3D Gaussian splat file type - accepts any supported splat container (.ply / .spz / .splat / .ksplat).""" Type = File3D @comfytype(io_type="FILE_3D_POINT_CLOUD_ANY") class File3DPointCloudAny(ComfyTypeIO): """General point cloud file type - accepts any supported point cloud container (currently .ply).""" Type = File3D @comfytype(io_type="HOOKS") class Hooks(ComfyTypeIO): if TYPE_CHECKING: Type = HookGroup @comfytype(io_type="HOOK_KEYFRAMES") class HookKeyframes(ComfyTypeIO): if TYPE_CHECKING: Type = HookKeyframeGroup @comfytype(io_type="TIMESTEPS_RANGE") class TimestepsRange(ComfyTypeIO): '''Range defined by start and endpoint, between 0.0 and 1.0.''' Type = tuple[int, int] @comfytype(io_type="LATENT_OPERATION") class LatentOperation(ComfyTypeIO): Type = Callable[[torch.Tensor], torch.Tensor] @comfytype(io_type="FLOW_CONTROL") class FlowControl(ComfyTypeIO): # NOTE: only used in testing_nodes right now Type = tuple[str, Any] @comfytype(io_type="ACCUMULATION") class Accumulation(ComfyTypeIO): # NOTE: only used in testing_nodes right now class AccumulationDict(TypedDict): accum: list[Any] Type = AccumulationDict @comfytype(io_type="LOAD3D_CAMERA") class Load3DCamera(ComfyTypeIO): class CameraInfo(TypedDict): # Coordinate system: right-handed, Y-up, camera looks down -Z position: dict[str, float | int] # scene units target: dict[str, float | int] # scene units; OrbitControls focus point zoom: float | int # dimensionless, 1 = 100% cameraType: str # 'perspective' | 'orthographic' quaternion: NotRequired[dict[str, float | int]] # normalized, dimensionless; camera world rotation useCustomUp: NotRequired[bool] # honor the quaternion's up as camera up instead of world +Y fov: NotRequired[float | int] # degrees, vertical FOV (perspective only) aspect: NotRequired[float | int] # width / height (perspective only) near: NotRequired[float | int] # scene units far: NotRequired[float | int] # scene units frustum: NotRequired[dict[str, float | int]] # orthographic only: {left, right, top, bottom} in scene units Type = CameraInfo @comfytype(io_type="LOAD3D_MODEL_INFO") class Load3DModelInfo(ComfyTypeIO): class Model3DTransform(TypedDict): # Coordinate system: right-handed, Y-up, world space position: dict[str, float | int] # scene units quaternion: dict[str, float | int] # normalized, dimensionless; world rotation scale: dict[str, float | int] # dimensionless multiplier Type = list[Model3DTransform] @comfytype(io_type="LOAD_3D") class Load3D(ComfyTypeIO): """3D models are stored as a dictionary.""" class Model3DDict(TypedDict): image: str mask: str normal: str camera_info: Load3DCamera.CameraInfo recording: NotRequired[str] model_3d_info: NotRequired[list[Load3DModelInfo.Model3DTransform]] Type = Model3DDict @comfytype(io_type="LOAD_3D_ANIMATION") class Load3DAnimation(Load3D): ... @comfytype(io_type="LAYERS") class Layers(ComfyTypeIO): BlendMode = Literal[ "normal", "multiply", "screen", "overlay", "darken", "lighten", "color-dodge", "color-burn", "hard-light", "soft-light", "difference", "exclusion", "linear-dodge", "linear-burn", "vivid-light", "pin-light", "linear-light", "hard-mix", "subtract", "divide", "grain-extract", "grain-merge", "hue", "saturation", "color", "luminosity", ] class LayerItem(TypedDict): image: torch.Tensor type: Literal["raster"] x: NotRequired[int] y: NotRequired[int] mask: NotRequired[torch.Tensor] z_index: int name: NotRequired[str] opacity: NotRequired[float] blend_mode: NotRequired["Layers.BlendMode"] visible: NotRequired[bool] flip_h: NotRequired[bool] flip_v: NotRequired[bool] rotation: NotRequired[float] w: NotRequired[int] h: NotRequired[int] class Document(TypedDict): version: int canvas: NotRequired[tuple[int, int]] layers: list["Layers.LayerItem"] Type = Document @comfytype(io_type="COMPOSITOR") class Compositor(ComfyTypeIO): class LayerState(TypedDict): version: NotRequired[int] canvas: dict background: NotRequired[dict] inputs: NotRequired[list[str]] order: NotRequired[list[int]] layers: list[dict] Type = LayerState class Input(WidgetInput): def __init__(self, id: str, display_name: str=None, optional=False, tooltip: str=None, socketless: bool=True, default: dict=None, advanced: bool=None): super().__init__(id, display_name, optional, tooltip, None, default, socketless, None, None, None, None, advanced) if default is None: self.default = {} @comfytype(io_type="CAMERA_INFO_STATE") class CameraInfoState(ComfyTypeI): Type = dict class Input(WidgetInput): def __init__(self, id: str, display_name: str=None, optional=False, tooltip: str=None, socketless: bool=True, advanced: bool=None): super().__init__(id, display_name, optional, tooltip, None, None, socketless, None, None, None, None, advanced) @comfytype(io_type="PHOTOMAKER") class Photomaker(ComfyTypeIO): Type = Any @comfytype(io_type="POINT") class Point(ComfyTypeIO): Type = Any # NOTE: I couldn't find any references in core code to POINT io_type. Does this exist? @comfytype(io_type="FACE_ANALYSIS") class FaceAnalysis(ComfyTypeIO): Type = Any # NOTE: I couldn't find any references in core code to POINT io_type. Does this exist? @comfytype(io_type="BBOX") class BBOX(ComfyTypeIO): Type = Any # NOTE: I couldn't find any references in core code to POINT io_type. Does this exist? @comfytype(io_type="SEGS") class SEGS(ComfyTypeIO): Type = Any # NOTE: I couldn't find any references in core code to POINT io_type. Does this exist? @comfytype(io_type="*") class AnyType(ComfyTypeIO): Type = Any @comfytype(io_type="MODEL_PATCH") class ModelPatch(ComfyTypeIO): Type = Any @comfytype(io_type="AUDIO_ENCODER") class AudioEncoder(ComfyTypeIO): Type = Any @comfytype(io_type="AUDIO_ENCODER_OUTPUT") class AudioEncoderOutput(ComfyTypeIO): Type = Any @comfytype(io_type="TRACKS") class Tracks(ComfyTypeIO): class TrackDict(TypedDict): track_path: torch.Tensor track_visibility: torch.Tensor Type = TrackDict @comfytype(io_type="DICT") class Dict(ComfyTypeIO): Type = dict @comfytype(io_type="ARRAY") class Array(ComfyTypeIO): Type = list @comfytype(io_type="COMFY_MULTITYPED_V3") class MultiType: Type = Any class Input(Input): ''' Input that permits more than one input type; if `id` is an instance of `ComfyType.Input`, then that input will be used to create a widget (if applicable) with overridden values. ''' def __init__(self, id: str | Input, types: list[type[_ComfyType] | _ComfyType], display_name: str=None, optional=False, tooltip: str=None, lazy: bool=None, extra_dict=None, raw_link: bool=None, advanced: bool=None): # if id is an Input, then use that Input with overridden values self.input_override = None if isinstance(id, Input): self.input_override = copy.copy(id) optional = id.optional if id.optional is True else optional tooltip = id.tooltip if id.tooltip is not None else tooltip display_name = id.display_name if id.display_name is not None else display_name lazy = id.lazy if id.lazy is not None else lazy id = id.id # if is a widget input, make sure widget_type is set appropriately if isinstance(self.input_override, WidgetInput): self.input_override.widget_type = self.input_override.get_io_type() super().__init__(id, display_name, optional, tooltip, lazy, extra_dict, raw_link, advanced) self._io_types = types @property def io_types(self) -> list[type[Input]]: ''' Returns list of Input class types permitted. ''' io_types = [] for x in self._io_types: if not is_class(x): io_types.append(type(x)) else: io_types.append(x) return io_types def get_io_type(self): # ensure types are unique and order is preserved str_types = [x.io_type for x in self.io_types] if self.input_override is not None: str_types.insert(0, self.input_override.get_io_type()) return ",".join(list(dict.fromkeys(str_types))) def as_dict(self): if self.input_override is not None: return self.input_override.as_dict() | super().as_dict() else: return super().as_dict() @comfytype(io_type="COMFY_MATCHTYPE_V3") class MatchType(ComfyTypeIO): class Template: def __init__(self, template_id: str, allowed_types: _ComfyType | list[_ComfyType] = AnyType): self.template_id = template_id # account for syntactic sugar if not isinstance(allowed_types, Iterable): allowed_types = [allowed_types] for t in allowed_types: if not isinstance(t, type): if not isinstance(t, _ComfyType): raise ValueError(f"Allowed types must be a ComfyType or a list of ComfyTypes, got {t.__class__.__name__}") else: if not issubclass(t, _ComfyType): raise ValueError(f"Allowed types must be a ComfyType or a list of ComfyTypes, got {t.__name__}") self.allowed_types = allowed_types def as_dict(self): return { "template_id": self.template_id, "allowed_types": ",".join([t.io_type for t in self.allowed_types]), } class Input(Input): def __init__(self, id: str, template: MatchType.Template, display_name: str=None, optional=False, tooltip: str=None, lazy: bool=None, extra_dict=None, raw_link: bool=None, advanced: bool=None): super().__init__(id, display_name, optional, tooltip, lazy, extra_dict, raw_link, advanced) self.template = template def as_dict(self): return super().as_dict() | prune_dict({ "template": self.template.as_dict(), }) class Output(Output): def __init__(self, template: MatchType.Template, id: str=None, display_name: str=None, tooltip: str=None, is_output_list=False): if not id and not display_name: display_name = "MATCHTYPE" super().__init__(id, display_name, tooltip, is_output_list) self.template = template def as_dict(self): return super().as_dict() | prune_dict({ "template": self.template.as_dict(), }) class DynamicInput(Input, ABC): ''' Abstract class for dynamic input registration. ''' pass class DynamicOutput(Output, ABC): ''' Abstract class for dynamic output registration. ''' pass def handle_prefix(prefix_list: list[str] | None, id: str | None = None) -> list[str]: if prefix_list is None: prefix_list = [] if id is not None: prefix_list = prefix_list + [id] return prefix_list def finalize_prefix(prefix_list: list[str] | None, id: str | None = None) -> str: assert not (prefix_list is None and id is None) if prefix_list is None: return id elif id is not None: prefix_list = prefix_list + [id] return ".".join(prefix_list) @comfytype(io_type="COMFY_AUTOGROW_V3") class Autogrow(ComfyTypeI): Type = dict[str, Any] _MaxNames = 100 # NOTE: max 100 names for sanity class _AutogrowTemplate: def __init__(self, input: Input): # dynamic inputs are not allowed as the template input assert(not isinstance(input, DynamicInput)) self.input = copy.copy(input) if isinstance(self.input, WidgetInput): self.input.force_input = True self.names: list[str] = [] self.cached_inputs = {} def _create_input(self, input: Input, name: str): new_input = copy.copy(self.input) new_input.id = name return new_input def _create_cached_inputs(self): for name in self.names: self.cached_inputs[name] = self._create_input(self.input, name) def get_all(self) -> list[Input]: return list(self.cached_inputs.values()) def as_dict(self): return prune_dict({ "input": create_input_dict_v1([self.input]), }) def validate(self): self.input.validate() class TemplatePrefix(_AutogrowTemplate): def __init__(self, input: Input, prefix: str, min: int=1, max: int=10): super().__init__(input) self.prefix = prefix assert(min >= 0) assert(max >= 1) assert(max <= Autogrow._MaxNames) self.min = min self.max = max self.names = [f"{self.prefix}{i}" for i in range(self.max)] self._create_cached_inputs() def as_dict(self): return super().as_dict() | prune_dict({ "prefix": self.prefix, "min": self.min, "max": self.max, }) class TemplateNames(_AutogrowTemplate): def __init__(self, input: Input, names: list[str], min: int=1): super().__init__(input) self.names = names[:Autogrow._MaxNames] assert(min >= 0) self.min = min self._create_cached_inputs() def as_dict(self): return super().as_dict() | prune_dict({ "names": self.names, "min": self.min, }) class Input(DynamicInput): def __init__(self, id: str, template: Autogrow.TemplatePrefix | Autogrow.TemplateNames, display_name: str=None, optional=False, tooltip: str=None, lazy: bool=None, extra_dict=None): super().__init__(id, display_name, optional, tooltip, lazy, extra_dict) self.template = template def as_dict(self): return super().as_dict() | prune_dict({ "template": self.template.as_dict(), }) def get_all(self) -> list[Input]: return [self] + self.template.get_all() def validate(self): self.template.validate() @staticmethod def _expand_schema_for_dynamic(out_dict: dict[str, Any], live_inputs: dict[str, Any], value: tuple[str, dict[str, Any]], input_type: str, curr_prefix: list[str] | None): # NOTE: purposely do not include self in out_dict; instead use only the template inputs # need to figure out names based on template type is_names = ("names" in value[1]["template"]) is_prefix = ("prefix" in value[1]["template"]) input = value[1]["template"]["input"] if is_names: min = value[1]["template"]["min"] names = value[1]["template"]["names"] max = len(names) elif is_prefix: prefix = value[1]["template"]["prefix"] min = value[1]["template"]["min"] max = value[1]["template"]["max"] names = [f"{prefix}{i}" for i in range(max)] # need to create a new input based on the contents of input template_input = None template_required = True for _input_type, dict_input in input.items(): # for now, get just the first value from dict_input; if not required, min can be ignored if len(dict_input) == 0: continue template_input = list(dict_input.values())[0] template_required = _input_type == "required" break if template_input is None: raise Exception("template_input could not be determined from required or optional; this should never happen.") new_dict = {} new_dict_added_to = False # first, add possible inputs into out_dict for i, name in enumerate(names): expected_id = finalize_prefix(curr_prefix, name) # required if i < min and template_required: out_dict["required"][expected_id] = template_input type_dict = new_dict.setdefault("required", {}) # optional else: out_dict["optional"][expected_id] = template_input type_dict = new_dict.setdefault("optional", {}) if expected_id in live_inputs: # NOTE: prefix gets added in parse_class_inputs type_dict[name] = template_input new_dict_added_to = True # account for the edge case that all inputs are optional and no values are received if not new_dict_added_to: finalized_prefix = finalize_prefix(curr_prefix) out_dict["dynamic_paths"][finalized_prefix] = finalized_prefix out_dict["dynamic_paths_default_value"][finalized_prefix] = DynamicPathsDefaultValue.EMPTY_DICT parse_class_inputs(out_dict, live_inputs, new_dict, curr_prefix) @comfytype(io_type="COMFY_DYNAMICCOMBO_V3") class DynamicCombo(ComfyTypeI): Type = dict[str, Any] class Option: def __init__(self, key: str, inputs: list[Input]): self.key = key self.inputs = inputs def as_dict(self): return { "key": self.key, "inputs": create_input_dict_v1(self.inputs), } class Input(DynamicInput): def __init__(self, id: str, options: list[DynamicCombo.Option], display_name: str=None, optional=False, tooltip: str=None, lazy: bool=None, extra_dict=None): super().__init__(id, display_name, optional, tooltip, lazy, extra_dict) self.options = options def get_all(self) -> list[Input]: return [self] + [input for option in self.options for input in option.inputs] def as_dict(self): return super().as_dict() | prune_dict({ "options": [o.as_dict() for o in self.options], }) def validate(self): # make sure all nested inputs are validated for option in self.options: for input in option.inputs: input.validate() @staticmethod def _expand_schema_for_dynamic(out_dict: dict[str, Any], live_inputs: dict[str, Any], value: tuple[str, dict[str, Any]], input_type: str, curr_prefix: list[str] | None): finalized_id = finalize_prefix(curr_prefix) if finalized_id in live_inputs: key = live_inputs[finalized_id] selected_option = None # get options from dict options: list[dict[str, str | dict[str, Any]]] = value[1]["options"] for option in options: if option["key"] == key: selected_option = option break if selected_option is not None: parse_class_inputs(out_dict, live_inputs, selected_option["inputs"], curr_prefix) # add self to inputs out_dict[input_type][finalized_id] = value out_dict["dynamic_paths"][finalized_id] = finalize_prefix(curr_prefix, curr_prefix[-1]) @comfytype(io_type="COMFY_DYNAMICSLOT_V3") class DynamicSlot(ComfyTypeI): Type = dict[str, Any] class Input(DynamicInput): def __init__(self, slot: Input, inputs: list[Input], display_name: str=None, tooltip: str=None, lazy: bool=None, extra_dict=None): assert(not isinstance(slot, DynamicInput)) self.slot = copy.copy(slot) self.slot.display_name = slot.display_name if slot.display_name is not None else display_name optional = True self.slot.tooltip = slot.tooltip if slot.tooltip is not None else tooltip self.slot.lazy = slot.lazy if slot.lazy is not None else lazy self.slot.extra_dict = slot.extra_dict if slot.extra_dict is not None else extra_dict super().__init__(slot.id, self.slot.display_name, optional, self.slot.tooltip, self.slot.lazy, self.slot.extra_dict) self.inputs = inputs self.force_input = None # force widget inputs to have no widgets, otherwise this would be awkward if isinstance(self.slot, WidgetInput): self.force_input = True self.slot.force_input = True def get_all(self) -> list[Input]: return [self.slot] + self.inputs def as_dict(self): return super().as_dict() | prune_dict({ "slotType": str(self.slot.get_io_type()), "inputs": create_input_dict_v1(self.inputs), "forceInput": self.force_input, }) def validate(self): self.slot.validate() for input in self.inputs: input.validate() @staticmethod def _expand_schema_for_dynamic(out_dict: dict[str, Any], live_inputs: dict[str, Any], value: tuple[str, dict[str, Any]], input_type: str, curr_prefix: list[str] | None): finalized_id = finalize_prefix(curr_prefix) if finalized_id in live_inputs: inputs = value[1]["inputs"] parse_class_inputs(out_dict, live_inputs, inputs, curr_prefix) # add self to inputs out_dict[input_type][finalized_id] = value out_dict["dynamic_paths"][finalized_id] = finalize_prefix(curr_prefix, curr_prefix[-1]) class DynamicInputError(ValueError): def __init__(self, input_name: str, message: str): super().__init__(message) self.input_name = input_name @comfytype(io_type="COMFY_DYNAMICGROUP_V3") class _DynamicGroup(ComfyTypeI): """Internal, unstable widget template repeated as a list of row dicts. Template fields must be widget inputs without force_input or nested dynamic inputs. Submit fields as '..', using indices below max without leading zeros. The '.' prefix is reserved for group fields, not separate sibling inputs. min/max are integers that count submitted rows (defaults: 0/20), even when optional=True. max also bounds the reconstructed list length and cannot exceed 20. Each submitted row follows the template's required/optional field declarations. Missing positions are dicts whose fields are None. Missing optional fields are also None; widget defaults are not injected. With INPUT_IS_LIST, missing fields are [None], matching the lists supplied for submitted fields. Empty groups are [] in execute and check_lazy_status. Nonempty lazy groups contain (value, original_key) tuples at each field. PriceBadge widget dependencies use the same indexed field names as prompt keys. """ Type = list[dict[str, Any]] _MaxRows = 20 class Input(DynamicInput): def __init__(self, id: str, template: list[WidgetInput], min: int=0, max: int=20, display_name: str=None, optional: bool=False, tooltip: str=None, lazy: bool=None, extra_dict=None, group_name: str="Group"): super().__init__(id, display_name, optional, tooltip, lazy, extra_dict) if not template: raise ValueError("DynamicGroup template must have at least one field.") for t in template: if isinstance(t, DynamicInput): raise ValueError("Nesting dynamic inputs inside DynamicGroup is not supported.") if not isinstance(t, WidgetInput): raise TypeError("DynamicGroup template fields must be WidgetInputs.") if t.force_input: raise ValueError(f"DynamicGroup template field '{t.id}' must not use force_input.") if not t.id or "." in t.id: raise ValueError(f"DynamicGroup template field id must be nonempty and must not contain '.'. Got: '{t.id}'") field_ids = [t.id for t in template] if len(field_ids) != len(set(field_ids)): raise ValueError("DynamicGroup template field ids must be unique within a row.") if not id or "." in id: raise ValueError(f"DynamicGroup id must be nonempty and must not contain '.'. Got: '{id}'") if isinstance(min, bool) or not isinstance(min, int) or isinstance(max, bool) or not isinstance(max, int): raise TypeError("DynamicGroup min and max must be integers.") if not 1 <= max <= _DynamicGroup._MaxRows: raise ValueError(f"DynamicGroup max must be between 1 and {_DynamicGroup._MaxRows}.") if not 0 <= min <= max: raise ValueError("DynamicGroup min must be between 0 and max.") self.template = template self.min = min self.max = max self.group_name = group_name def get_all(self) -> list[Input]: return [self] + list(self.template) def as_dict(self): return super().as_dict() | prune_dict({ "template": create_input_dict_v1(self.template), "min": self.min, "max": self.max, "group_name": self.group_name, }) def validate(self): for t in self.template: t.validate() @staticmethod def _expand_schema_for_dynamic(out_dict: dict[str, Any], live_inputs: dict[str, Any], value: tuple[str, dict[str, Any]], input_type: str, curr_prefix: list[str] | None): info = value[1] min_rows = info.get("min", 0) max_rows = info.get("max", _DynamicGroup._MaxRows) template = info.get("template", {}) field_specs = { field_id: (field_value, category) for category in ("required", "optional") for field_id, field_value in template.get(category, {}).items() } finalized_prefix = finalize_prefix(curr_prefix) prefix = finalized_prefix + "." present_rows = set() for key in live_inputs: if not key.startswith(prefix): continue index, separator, field_id = key[len(prefix):].partition(".") if (not separator or not index.isascii() or not index.isdecimal() or (len(index) > 1 and index.startswith("0")) or field_id not in field_specs): raise DynamicInputError(key, f"Invalid DynamicGroup input key '{key}'; expected '{finalized_prefix}..