# 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({
"2": {
"class_type": "LoadVideo",
"inputs": {
"file": "UPLOADED_motion_FILENAME"
},
"_meta": {
"title": "Load Video"
}
},
"8": {
"class_type": "KlingMotionControl",
"inputs": {
"character_orientation": "video",
"keep_original_sound": True,
"mode": "pro",
"model": "kling-v2-6",
"prompt": "A sunlit botanical glasshouse in the late morning, with tall palms, hanging ferns and climbing vines rising toward a white iron-framed glass roof. The character dances on a wide stone path between raised beds of tropical plants, the whole body in view from head to feet. Warm sunlight streams through the glass panes and casts soft geometric shadows across the floor, while fine mist from overhead sprinklers drifts through the light beams and catches small rainbow glints. Lush green leaves sway gently behind the dancer, and a few orange butterflies flutter past. Natural, true-to-life colour, gentle shallow depth of field, crisp detail on the clothing and face, smooth realistic motion that follows the dancer's rhythm, a steady camera at chest height with a slow, subtle push-in.",
"reference_image": [
"11",
0
],
"reference_video": [
"2",
0
]
},
"_meta": {
"title": "Kling Motion Control"
}
},
"9": {
"class_type": "SaveVideo",
"inputs": {
"filename_prefix": "video/kling-motion-control",
"format": "auto",
"format.codec": "auto",
"video": [
"8",
0
]
},
"_meta": {
"title": "Save Video"
}
},
"11": {
"class_type": "LoadImage",
"inputs": {
"button": {
"clicked": False,
"computedDisabled": False,
"computedHeight": 24,
"label": "choose file to upload",
"last_y": 70,
"name": "upload",
"options": {
"canvasOnly": True,
"serialize": False
},
"type": "button",
"y": 70
},
"image": "UPLOADED_character_FILENAME"
},
"_meta": {
"title": "Load Image"
}
}
})
workflow.set_input("11", "image", client.assets.from_url("https://comfy.org/catalog-media/2136161c-7b56-44c1-8f25-cbfa8cbe01fc/42487951-1e24-496a-b42d-4ebfd3726efc"))
workflow.set_input("2", "file", client.assets.from_url("https://comfy.org/catalog-media/e54a3841-7438-40f4-8376-b158303bc3e4/a03c1d74-794c-4884-9c46-7cda6f4d561d"))
# 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("9"):
output.to_file(output.name)