# 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({
"9": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_image_FILENAME"
},
"_meta": {
"title": "Load Image"
}
},
"17": {
"class_type": "PreviewImage",
"inputs": {
"images": [
"49:23",
0
]
},
"_meta": {
"title": "Preview Image (depth_colored)"
}
},
"26": {
"class_type": "PreviewImage",
"inputs": {
"images": [
"49:25",
0
]
},
"_meta": {
"title": "Preview Image (depth)"
}
},
"69": {
"class_type": "MaskPreview",
"inputs": {
"mask": [
"49:68",
0
]
},
"_meta": {
"title": "Preview Mask"
}
},
"49:13": {
"class_type": "MoGeInference",
"inputs": {
"apply_mask": True,
"batch_size": 4,
"force_projection": True,
"fov_x_degrees": 0,
"image": [
"49:53",
0
],
"moge_model": [
"49:32",
0
],
"refine_steps": 3,
"resolution_level": 2
},
"_meta": {
"title": "Run MoGe Inference"
}
},
"49:23": {
"class_type": "MoGeRender",
"inputs": {
"moge_geometry": [
"49:13",
0
],
"output": "depth_colored"
},
"_meta": {
"title": "Render MoGe Geometry"
}
},
"49:25": {
"class_type": "MoGeRender",
"inputs": {
"moge_geometry": [
"49:13",
0
],
"output": "depth"
},
"_meta": {
"title": "Render MoGe Geometry"
}
},
"49:32": {
"class_type": "LoadMoGeModel",
"inputs": {
"model_name": "moge_2_vitl_normal_fp16.safetensors"
},
"_meta": {
"title": "Load MoGe Model"
}
},
"49:36": {
"class_type": "ComfyMathExpression",
"inputs": {
"expression": "a > 2048",
"values.a": [
"49:37",
0
]
},
"_meta": {
"title": "Math Expression"
}
},
"49:37": {
"class_type": "GetImageSize",
"inputs": {
"image": [
"9",
0
]
},
"_meta": {
"title": "Get Image Size"
}
},
"49:42": {
"class_type": "ComfySwitchNode",
"inputs": {
"on_false": [
"9",
0
],
"on_true": [
"49:70",
0
],
"switch": [
"49:36",
2
]
},
"_meta": {
"title": "If/Else Switch"
}
},
"49:45": {
"class_type": "MoGeRender",
"inputs": {
"moge_geometry": [
"49:13",
0
],
"output": "mask"
},
"_meta": {
"title": "Render MoGe Geometry"
}
},
"49:53": {
"class_type": "ComfySwitchNode",
"inputs": {
"on_false": [
"9",
0
],
"on_true": [
"49:42",
0
],
"switch": True
},
"_meta": {
"title": "If/Else Switch"
}
},
"49:68": {
"class_type": "ImageToMask",
"inputs": {
"channel": "red",
"image": [
"49:45",
0
]
},
"_meta": {
"title": "Convert Image to Mask"
}
},
"49:70": {
"class_type": "ResizeImageMaskNode",
"inputs": {
"input": [
"9",
0
],
"resize_type": "scale longer dimension",
"resize_type.longer_size": 2048,
"scale_method": "lanczos"
},
"_meta": {
"title": "Resize Image/Mask"
}
}
})
workflow.set_input("9", "image", client.assets.from_url("https://comfy.org/catalog-media/713d62d0-56d5-4ce9-9447-e7e77d22dde2/81307472-6337-4083-9035-697b3ae3f358"))
# 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("17"):
output.to_file(output.name)