# 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({
"14": {
"class_type": "CheckpointLoaderSimple",
"inputs": {
"ckpt_name": "sam3.1_multiplex_fp16.safetensors"
},
"_meta": {
"title": "Load Checkpoint"
}
},
"15": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"14",
1
],
"text": "person"
},
"_meta": {
"title": "CLIP Text Encode (Prompt)"
}
},
"18": {
"class_type": "SAM3_VideoTrack",
"inputs": {
"conditioning": [
"15",
0
],
"detect_interval": 1,
"detection_threshold": 0.5,
"images": [
"86",
0
],
"max_objects": 4,
"model": [
"14",
0
]
},
"_meta": {
"title": "Run SAM3 Video Track"
}
},
"85": {
"class_type": "LoadVideo",
"inputs": {
"file": "UPLOADED_video_FILENAME"
},
"_meta": {
"title": "Load Video"
}
},
"86": {
"class_type": "GetVideoComponents",
"inputs": {
"video": [
"125",
0
]
},
"_meta": {
"title": "Get Video Components"
}
},
"109": {
"class_type": "RTDETR_detect",
"inputs": {
"class_name": "person",
"image": [
"86",
0
],
"max_detections": 100,
"model": [
"111",
0
],
"threshold": 0.5
},
"_meta": {
"title": "Run Real-Time Detection (RT-DETR)"
}
},
"111": {
"class_type": "UNETLoader",
"inputs": {
"unet_name": "rt_detr_v4-x-hgnet_fp32.safetensors",
"weight_dtype": "default"
},
"_meta": {
"title": "Load Diffusion Model"
}
},
"114": {
"class_type": "MoGeInference",
"inputs": {
"apply_mask": True,
"batch_size": 4,
"force_projection": True,
"fov_x_degrees": 0,
"image": [
"86",
0
],
"moge_model": [
"115",
0
],
"refine_steps": 3,
"resolution_level": 9
},
"_meta": {
"title": "Run MoGe Inference"
}
},
"115": {
"class_type": "LoadMoGeModel",
"inputs": {
"model_name": "moge_2_vitl_normal_fp16.safetensors"
},
"_meta": {
"title": "Load MoGe Model"
}
},
"119": {
"class_type": "SAM3DBody_Loader",
"inputs": {
"model_file": "sam_3d_body_dinov3_bf16.safetensors"
},
"_meta": {
"title": "Load SAM3D Body Model"
}
},
"125": {
"class_type": "Video Slice",
"inputs": {
"duration": 5,
"start_time": 0,
"strict_duration": False,
"video": [
"85",
0
]
},
"_meta": {
"title": "Trim Video"
}
},
"126": {
"class_type": "MoGeGeometryToFOV",
"inputs": {
"axis": "vertical",
"moge_geometry": [
"114",
0
],
"unit": "degrees"
},
"_meta": {
"title": "Get FoV from MoGe Geometry"
}
},
"127": {
"class_type": "BuildPoseFile",
"inputs": {
"camera_translation": "off",
"format": "glb",
"format.bone_smooth_window": 0,
"format.mesh_style": "scail",
"format.mesh_style.face_style": "full",
"format.mesh_style.hand_marker_radius_m": 0.005,
"format.mesh_style.hand_stick_radius_m": 0.003,
"format.mesh_style.include_hands": False,
"format.mesh_style.marker_radius_m": 0,
"format.mesh_style.material_roughness": 0.3,
"format.mesh_style.stick_radius_m": 0.022,
"fps": 24,
"pose_data": [
"130",
0
],
"sam3d_body_model": [
"119",
0
],
"track_index": -1
},
"_meta": {
"title": "Create 3D Animation File"
}
},
"128": {
"class_type": "SAM3DBody_FaceExpression",
"inputs": {
"blendshape_smooth_window": 7,
"brow_strength": 2,
"eye_strength": 2,
"image": [
"86",
0
],
"input_threshold": 0.02,
"mhr_pose_data": [
"129",
0
],
"mouth_strength": 1,
"sam3d_body_model": [
"119",
0
],
"strength": 1
},
"_meta": {
"title": "Face Expression to SAM3D Body"
}
},
"129": {
"class_type": "SAM3DBody_Predict",
"inputs": {
"batch_size": 64,
"bboxes": [
"109",
0
],
"fov": [
"126",
0
],
"image": [
"86",
0
],
"run_hand_refinement": True,
"sam3d_body_model": [
"119",
0
],
"track_data": [
"18",
0
]
},
"_meta": {
"title": "Run SAM3D Body Prediction"
}
},
"130": {
"class_type": "SAM3DBody_Smooth",
"inputs": {
"method": "gaussian",
"mhr_pose_data": [
"128",
0
],
"rotation_threshold_degrees": 15,
"strength": 1,
"window": 7
},
"_meta": {
"title": "Smooth SAM3D Body Pose Data"
}
},
"131": {
"class_type": "SAM3DBody_Render",
"inputs": {
"background": [
"86",
0
],
"height": 0,
"pose_data": [
"130",
0
],
"render_style": "mesh",
"render_style.opacity": 1,
"render_style.person_palette_falloff": 0.6,
"render_style.region": "full_body",
"render_style.shader": "default",
"width": 0
},
"_meta": {
"title": "Render 3D Body Pose"
}
},
"134": {
"class_type": "CreateVideo",
"inputs": {
"bit_depth": 8,
"chroma_subsampling": "auto",
"codec": "none",
"color_space": "sRGB",
"fps": [
"86",
2
],
"images": [
"131",
0
]
},
"_meta": {
"title": "Create Video"
}
},
"135": {
"class_type": "SaveVideo",
"inputs": {
"codec": "auto",
"filename_prefix": "video/SAM3D_body",
"format": "auto",
"format.codec": "auto",
"video": [
"134",
0
]
},
"_meta": {
"title": "Save Video"
}
},
"143": {
"class_type": "Save3DAdvanced",
"inputs": {
"filename_prefix": "3d/ComfyUI",
"height": 1024,
"model_3d": [
"127",
0
],
"viewport_state": "",
"width": 1024
},
"_meta": {
"title": "Save 3D (Advanced)"
}
}
})
workflow.set_input("85", "file", client.assets.from_url("https://comfy.org/catalog-media/e1cb8b80-9432-4843-972e-ba32d4b6b78c/0d9e9cc0-4cb8-42ef-a403-62a149b6812e"))
# 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("135"):
output.to_file(output.name)