| |
| """ |
| orchestrator.py — drives ComfyUI's REST API to generate a storyboard. |
| |
| WHAT THIS DOES: |
| 1. Reads shots.yaml (your storyboard, one block per shot) |
| 2. Loads single_shot_template.json (the reusable ~43-node pipeline) |
| 3. For each shot: overrides prompt text + character/location refs, submits |
| to ComfyUI's /prompt endpoint, waits for completion, moves to next shot |
| 4. No canvas editing, no manual node clicking — fully automated |
| |
| REQUIREMENTS: |
| pip install requests pyyaml --break-system-packages |
| |
| USAGE: |
| # 1. Make sure ComfyUI is running (python main.py --listen 0.0.0.0 --port 8188) |
| # 2. Edit shots.yaml to add/change shots |
| # 3. Run: |
| python orchestrator.py |
| |
| # Run only specific shots (comma-separated IDs): |
| python orchestrator.py --only B1_Jim_Seated_Table,B2_Jim_BarCounter_Facing |
| |
| # Point at a different ComfyUI instance: |
| python orchestrator.py --host http://localhost:8188 |
| """ |
| import argparse |
| import copy |
| import json |
| import sys |
| import time |
| from pathlib import Path |
|
|
| import requests |
| import yaml |
|
|
| |
| |
| |
| |
| |
| NODE = { |
| "pos": 20, |
| "neg": 21, |
| "ipa": 23, |
| "pulid_s1": 24, |
| "cn_apply": 25, |
| "face_pos": 35, |
| "face_neg": 36, |
| "pulid_s2": 34, |
| "save_final": 39, |
| "save_s1": 28, |
| } |
|
|
| |
| REF_NODE = { |
| "jim_body": 16, |
| "jim_face": 17, |
| "barbara_body": 18, |
| "barbara_face": 19, |
| } |
|
|
| |
| |
| |
| |
| DEPTH_NODE = {"loc1": "12", "loc2": "13"} |
|
|
| DEFAULT_NEG = ( |
| "low quality, bad anatomy, extra limbs, missing limbs, watermark, " |
| "blurry, deformed face, extra fingers, duplicate, ugly" |
| ) |
| DEFAULT_FACE_NEG = "low quality, bad anatomy, blurry, deformed face" |
|
|
|
|
| def load_template(path: str) -> dict: |
| with open(path) as f: |
| return json.load(f) |
|
|
|
|
| def workflow_to_api_format(wf: dict) -> dict: |
| """ |
| Convert the UI-style workflow (nodes/links arrays, used for canvas display) |
| into the API prompt format ComfyUI's /prompt endpoint expects |
| (a flat dict of node_id -> {class_type, inputs}). |
| """ |
| link_map = {l[0]: l for l in wf["links"]} |
| api = {} |
| for n in wf["nodes"]: |
| node_id = str(n["id"]) |
| inputs = {} |
| |
| for i, inp in enumerate(n.get("inputs", [])): |
| lid = inp.get("link") |
| if lid is not None: |
| l = link_map[lid] |
| src_node, src_slot = l[1], l[2] |
| inputs[inp["name"]] = [str(src_node), src_slot] |
| |
| widget_names = [i["name"] for i in n.get("inputs", []) if i.get("link") is None] |
| |
| |
| |
| wv = n.get("widgets_values", []) |
| |
| |
| api[node_id] = {"class_type": n["type"], "inputs": inputs, "_widgets": wv} |
| return api |
|
|
|
|
| def apply_shot_overrides(wf: dict, shot: dict, char_registry: dict) -> dict: |
| """Return a deep-copied workflow with this shot's values patched in.""" |
| wf = copy.deepcopy(wf) |
| by_id = {n["id"]: n for n in wf["nodes"]} |
|
|
| char = shot["character"] |
| guidance = shot.get("guidance", 4.5) |
| neg_guidance = shot.get("neg_guidance", 3.5) |
|
|
| |
| by_id[NODE["pos"]]["widgets_values"] = [ |
| shot["clip_l"].strip(), shot["t5"].strip(), guidance |
| ] |
| by_id[NODE["neg"]]["widgets_values"] = [ |
| "", shot.get("neg", DEFAULT_NEG), neg_guidance |
| ] |
| by_id[NODE["face_pos"]]["widgets_values"] = [ |
| shot["face_clip_l"].strip(), shot["face_t5"].strip(), guidance |
| ] |
| by_id[NODE["face_neg"]]["widgets_values"] = [ |
| "", shot.get("face_neg", DEFAULT_FACE_NEG), neg_guidance |
| ] |
|
|
| |
| body_ref_id = REF_NODE[f"{char}_body"] |
| face_ref_id = REF_NODE[f"{char}_face"] |
| body_fname = char_registry[char]["body_image"] |
| face_fname = char_registry[char]["face_image"] |
| by_id[body_ref_id]["widgets_values"] = [body_fname, "image"] |
| by_id[face_ref_id]["widgets_values"] = [face_fname, "image"] |
|
|
| |
| ipa_weight = shot.get("ipa_weight", char_registry[char].get("ipa_weight", 0.43)) |
| by_id[NODE["ipa"]]["widgets_values"] = [ipa_weight] |
|
|
| pulid_s1_weight = shot.get("pulid_s1_weight", 0.85) |
| by_id[NODE["pulid_s1"]]["widgets_values"][0] = pulid_s1_weight |
| pulid_s2_weight = shot.get("pulid_s2_weight", 0.95) |
| by_id[NODE["pulid_s2"]]["widgets_values"][0] = pulid_s2_weight |
|
|
| |
| cn_strength = shot.get("cn_strength", 0.45) |
| cn_end = shot.get("cn_end", 0.65) |
| by_id[NODE["cn_apply"]]["widgets_values"] = [cn_strength, 0.0, cn_end] |
|
|
| |
| depth_node_id = int(DEPTH_NODE[shot["location"]]) |
| cn_node = by_id[NODE["cn_apply"]] |
| |
| for link in wf["links"]: |
| if link[3] == NODE["cn_apply"] and link[4] == 3: |
| link[1] = depth_node_id |
| link[2] = 0 |
|
|
| |
| by_id[NODE["save_final"]]["widgets_values"] = [f"{shot['id']}_FINAL"] |
| by_id[NODE["save_s1"]]["widgets_values"] = [f"{shot['id']}_Stage1"] |
|
|
| return wf |
|
|
|
|
| def fetch_widget_schemas(host: str) -> dict: |
| """ |
| Fetch real widget name schemas from ComfyUI's /object_info endpoint. |
| This replaces the old hardcoded WIDGET_SCHEMA — it works regardless of |
| which version of custom nodes is installed, and handles ComfyUI's own |
| typos in node input names (e.g. 'ipadatper' in LoadFluxIPAdapter). |
| |
| Widget inputs are those whose type is a primitive (STRING, INT, FLOAT, |
| BOOLEAN) or a combo list — as opposed to node connection types like |
| MODEL, CLIP, LATENT which are satisfied by links, not widget values. |
| """ |
| resp = requests.get(f"{host}/object_info", timeout=10) |
| resp.raise_for_status() |
| info = resp.json() |
|
|
| PRIMITIVE_TYPES = {"STRING", "INT", "FLOAT", "BOOLEAN"} |
| schemas = {} |
|
|
| for node_type, node_info in info.items(): |
| widget_names = [] |
| all_inputs = {} |
| all_inputs.update(node_info.get("input", {}).get("required", {})) |
| all_inputs.update(node_info.get("input", {}).get("optional", {})) |
|
|
| for name, spec in all_inputs.items(): |
| if not spec: |
| continue |
| input_type = spec[0] |
| |
| if isinstance(input_type, list) or input_type in PRIMITIVE_TYPES: |
| widget_names.append(name) |
|
|
| schemas[node_type] = widget_names |
|
|
| return schemas |
|
|
|
|
| |
| WIDGET_SCHEMA: dict = {} |
|
|
| |
| |
| |
| |
| SKIP_WIDGET_INDICES: dict = { |
| "KSampler": {1}, |
| "KSamplerAdvanced": {1, 4}, |
| } |
|
|
|
|
| def ui_workflow_to_prompt_payload(wf: dict) -> dict: |
| """ |
| Convert UI graph format to ComfyUI's API prompt dict format. |
| ComfyUI needs: {node_id: {"class_type": ..., "inputs": {name: value_or_link}}} |
| WIDGET_SCHEMA must be populated before calling this (done in main()). |
| """ |
| link_map = {l[0]: l for l in wf["links"]} |
| prompt = {} |
|
|
| for n in wf["nodes"]: |
| node_id = str(n["id"]) |
| class_type = n["type"] |
| inputs = {} |
|
|
| |
| linked_names = set() |
| for inp in n.get("inputs", []): |
| lid = inp.get("link") |
| if lid is not None: |
| l = link_map[lid] |
| inputs[inp["name"]] = [str(l[1]), l[2]] |
| linked_names.add(inp["name"]) |
|
|
| |
| |
| |
| |
| |
| wv = n.get("widgets_values", []) |
| schema = WIDGET_SCHEMA.get(class_type, []) |
| skip = SKIP_WIDGET_INDICES.get(class_type, set()) |
| wi = 0 |
| for vi, val in enumerate(wv): |
| if vi in skip: |
| continue |
| if wi >= len(schema): |
| break |
| name = schema[wi] |
| wi += 1 |
| if name in linked_names: |
| continue |
| inputs[name] = val |
|
|
| prompt[node_id] = {"class_type": class_type, "inputs": inputs} |
|
|
| return prompt |
|
|
|
|
| def submit_and_wait(host: str, prompt_payload: dict, shot_id: str, timeout: int = 600): |
| """Submit to ComfyUI /prompt and poll /history until this prompt completes.""" |
| resp = requests.post(f"{host}/prompt", json={"prompt": prompt_payload}) |
| resp.raise_for_status() |
| data = resp.json() |
| prompt_id = data["prompt_id"] |
| print(f" [{shot_id}] submitted, prompt_id={prompt_id}") |
|
|
| start = time.time() |
| while time.time() - start < timeout: |
| h = requests.get(f"{host}/history/{prompt_id}").json() |
| if prompt_id in h: |
| status = h[prompt_id].get("status", {}) |
| if status.get("completed"): |
| print(f" [{shot_id}] done in {time.time()-start:.1f}s") |
| return h[prompt_id] |
| if status.get("status_str") == "error": |
| print(f" [{shot_id}] FAILED: {status}") |
| return None |
| time.sleep(3) |
| print(f" [{shot_id}] TIMEOUT after {timeout}s") |
| return None |
|
|
|
|
| def main(): |
| ap = argparse.ArgumentParser() |
| ap.add_argument("--host", default="http://localhost:8188") |
| ap.add_argument("--shots", default="shots.yaml") |
| ap.add_argument("--template", default="single_shot_template.json") |
| ap.add_argument("--characters", default="characters.yaml") |
| ap.add_argument("--only", default=None, |
| help="Comma-separated shot IDs to run (default: all)") |
| args = ap.parse_args() |
|
|
| with open(args.characters) as f: |
| char_registry = yaml.safe_load(f) |
| with open(args.shots) as f: |
| shots = yaml.safe_load(f) |
| template = load_template(args.template) |
|
|
| if args.only: |
| wanted = set(args.only.split(",")) |
| shots = [s for s in shots if s["id"] in wanted] |
|
|
| |
| global WIDGET_SCHEMA |
| print(f"Fetching node schemas from {args.host} ...") |
| WIDGET_SCHEMA = fetch_widget_schemas(args.host) |
| print(f" got schemas for {len(WIDGET_SCHEMA)} node types\n") |
|
|
| print(f"Running {len(shots)} shot(s) against {args.host}\n") |
|
|
| for shot in shots: |
| wf = apply_shot_overrides(template, shot, char_registry) |
| prompt_payload = ui_workflow_to_prompt_payload(wf) |
| submit_and_wait(args.host, prompt_payload, shot["id"]) |
|
|
| print("\nAll shots submitted.") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|