{"record":{"id":"f352a02832276220","repo":"Comfy-Org/ComfyUI","slug":"compositor-supports-at-most-max-layers-layers-g","errorCode":null,"errorMessage":"Compositor supports at most {MAX_LAYERS} layers, got {len(frames)}","messagePattern":"Compositor supports at most (.+?) layers, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"comfy_extras/nodes_compositor.py","lineNumber":124,"sourceCode":"            frames.append({\n                \"tensor\": image[index : index + 1],\n                \"mask\": _item_mask_frame(item.get(\"mask\"), index),\n                \"name\": item.get(\"name\") if isinstance(item.get(\"name\"), str) else None,\n                \"x\": _int(item.get(\"x\"), 0),\n                \"y\": _int(item.get(\"y\"), 0),\n                \"w\": width if width > 0 else int(image.shape[2]),\n                \"h\": height if height > 0 else int(image.shape[1]),\n                \"rotation\": float(rotation)\n                if isinstance(rotation, (int, float)) and not isinstance(rotation, bool)\n                else 0.0,\n                \"opacity\": item.get(\"opacity\", 1.0),\n                \"blend\": item.get(\"blend_mode\", \"normal\"),\n                \"visible\": item.get(\"visible\", True),\n                \"flip_h\": bool(item.get(\"flip_h\", False)),\n                \"flip_v\": bool(item.get(\"flip_v\", False)),\n            })\n    if len(frames) > MAX_LAYERS:\n        raise ValueError(\n            f\"Compositor supports at most {MAX_LAYERS} layers, got {len(frames)}\"\n        )\n    return frames\n\n\ndef frame_alpha(\n    tensor: torch.Tensor, mask: torch.Tensor | None\n) -> torch.Tensor | None:\n    alpha = tensor[:1, :, :, 3] if tensor.shape[-1] == 4 else None\n    if mask is None:\n        return alpha\n    h, w = tensor.shape[1], tensor.shape[2]\n    m = mask[:1].to(device=tensor.device, dtype=torch.float32)\n    if m.shape[1] != h or m.shape[2] != w:\n        m = torch.nn.functional.interpolate(\n            m.unsqueeze(1), size=(h, w), mode=\"bilinear\"\n        ).squeeze(1)\n    inv = torch.clamp(1.0 - m, 0.0, 1.0)","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy_extras/nodes_compositor.py#L106-L142","documentation":"After collecting layer frames from a LAYERS document, the compositor enforces MAX_LAYERS = 50 and refuses longer lists with this message. This bounds the per-composite numpy canvas work and UI complexity; the count is of frames that survived filtering (valid raster tensors), not of all raw document entries.","triggerScenarios":"A LAYERS document (or aggregated documents) whose valid raster layers total more than 50 — e.g. 60 image layers each carrying a torch.Tensor image. Non-dict items and items without tensor images are skipped before the count, so it takes 51 real layers to trip it.","commonSituations":"Programmatically generated documents (one layer per detection, tile, or animation frame) that scale past 50; concatenating multiple documents' layers lists into one composite.","solutions":["Reduce the document to at most 50 raster layers — drop invisible or fully occluded layers first.","Split the work into multiple compositor nodes (e.g. 2 x 30 layers) and composite their outputs together.","Pre-flatten groups of layers into single raster images upstream to shrink the layer count."],"exampleFix":"# before\ndoc[\"layers\"] = all_80_layers\n# after\ndoc[\"layers\"] = [l for l in all_80_layers if l.get(\"visible\", True)][:50]","handlingStrategy":"validation","validationCode":"MAX_LAYERS = 50\ndef clamp_layers(doc, keep_visible_first=True):\n    layers = doc.get(\"layers\") or []\n    if keep_visible_first:\n        layers = sorted(layers, key=lambda l: not l.get(\"visible\", True))\n    doc = dict(doc, layers=layers[:MAX_LAYERS])\n    return doc","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Count valid raster layers before compositing; drop invisible ones first.","Chunk large jobs into multiple compositor nodes of <=50 layers.","Flatten layer groups upstream to reduce counts."],"tags":["limits","layers-document","comfyui"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}