# Python 3.10+: pip install comfy-sdk==0.4.0
import os
from comfy_sdk import Comfy
api_key = os.environ.get("COMFY_API_KEY", "").strip()
if not api_key: raise RuntimeError("Set COMFY_API_KEY")
os.environ.setdefault("COMFY_BASE_URL", "https://cloud.comfy.org")
client = Comfy(api_key=api_key)
workflow = client.workflows.from_json({
"6": {
"class_type": "SaveVideo",
"inputs": {
"codec": "auto",
"filename_prefix": "video/Vidu_q4_preview_r2v",
"format": "auto",
"format.codec": "auto",
"video": [
"9",
0
]
},
"_meta": {
"title": "Save Video"
}
},
"9": {
"class_type": "Vidu4ReferenceVideoNode",
"inputs": {
"model": "Vidu Q4 Preview",
"model.aspect_ratio": "16:9",
"model.audio": True,
"model.duration": 5,
"model.prompt": "5-second stylish 3D cartoon using both reference images. Preserve the fuzzy yellow character from image 1 and the white-haired character, purple smiley sweater and drink cup from image 2. Place them in one playful blue-and-purple world. 0–1s: The white-haired character takes a sip; a tiny yellow blur streaks past behind him. 1–2s: Hard cut to a low angle as the fuzzy character lands, squashes flat for a beat, then springs high with an exaggerated stretch. 2–3s: Whip cut to the white-haired character’s face—his eyes widen, he leans back, then grins. 3–4s: The fuzzy character makes one fast curved leap toward his shoulder, with a brief hand-animated motion smear. 4–5s: It lands softly; the character raises his free hand and they strike a cheeky, perfectly timed freeze-frame pose. Snappy cuts, bold camera angles, expressive squash-and-stretch, polished character animation and fashionable purple-yellow-blue color blocking. Keep the cup steady and both designs consistent. No dialogue, extra characters, text or subtitles.",
"model.reference_images.image_1": [
"11",
0
],
"model.reference_images.image_2": [
"10",
0
],
"model.resolution": "720p",
"model.seed": 1613348113
},
"_meta": {
"title": "Vidu Q4 Reference-to-Video Generation"
}
},
"10": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_reference_2_FILENAME"
},
"_meta": {
"title": "Load Image"
}
},
"11": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_reference_1_FILENAME"
},
"_meta": {
"title": "Load Image"
}
}
})
workflow.set_input("11", "image", client.assets.from_url("https://comfy.org/catalog-media/10d7c058-2db5-479e-a057-f8261adfe1c9/738c4f8f-869d-4e85-8d59-34d554b178ed"))
workflow.set_input("10", "image", client.assets.from_url("https://comfy.org/catalog-media/65515fdf-f22e-4440-9343-699891f13d36/0a170bad-c189-4431-9d7c-c3ca38339bee"))
# run() also passes api_key to the partner nodes in this workflow.
job = client.run(workflow, api_key=api_key)
for output in job.get_outputs("6"):
output.to_file(output.name)