Download scripts/gen_h3.py from paralaif/anima-motion-module: direct link, hf CLI and curl.
- Browser
- Download file 5.12 kB
-
https://huggingface.co/paralaif/anima-motion-module/resolve/main/scripts/gen_h3.py
- Command line
-
hf download hf://paralaif/anima-motion-module/scripts/gen_h3.py
-
curl -L -o gen_h3.py https://huggingface.co/paralaif/anima-motion-module/resolve/main/scripts/gen_h3.py
5.12 kB
| """Animate Anima keyframes (moss-charref-adapter/data varN.png) with MiniMax H3 I2V through the ComfyUI API.""" | |
| import json, random, sys, time, urllib.request, uuid | |
| from pathlib import Path | |
| API = "http://localhost:8188" | |
| DATA = Path.home() / "AI/moss-charref-adapter/data" | |
| OUT = Path(__file__).parent / "clips" | |
| MOTIONS = [ | |
| "turns her head toward the camera and smiles", "waves her hand and laughs", "her hair and clothes sway in the wind", | |
| "takes two steps forward", "looks down, then looks up and blinks", "nods and tilts her head", | |
| "raises one arm and stretches", "laughs and covers her mouth with one hand", "turns around slowly", | |
| "breathes and shifts her weight, blinking", "points forward excitedly", "brushes her hair behind her ear", | |
| ] | |
| def post(path, data, headers): | |
| return json.load(urllib.request.urlopen(urllib.request.Request(API + path, data, headers))) | |
| def upload(path): | |
| boundary = uuid.uuid4().hex | |
| body = (f"--{boundary}\r\nContent-Disposition: form-data; name=\"image\"; filename=\"{path.parent.name}_{path.name}\"\r\n" | |
| f"Content-Type: image/png\r\n\r\n").encode() + path.read_bytes() + f"\r\n--{boundary}\r\nContent-Disposition: form-data; name=\"overwrite\"\r\n\r\ntrue\r\n--{boundary}--\r\n".encode() | |
| return post("/upload/image", body, {"Content-Type": f"multipart/form-data; boundary={boundary}"})["name"] | |
| def graph(image, prompt, seed, prefix): | |
| return { | |
| "1": {"class_type": "UNETLoader", "inputs": {"unet_name": "minimax/DasiwaMinimaxH3_dasiwaHybridV2_3203130.safetensors", "weight_dtype": "default"}}, | |
| "2": {"class_type": "ModelAttentionBackend", "inputs": {"model": ["1", 0], "attention": "comfy kitchen attention"}}, | |
| "3": {"class_type": "LoraLoaderModelOnly", "inputs": {"model": ["2", 0], "lora_name": "minimax/accelerators/taomate_h3_3step_comfy.safetensors", "strength_model": 1.0}}, | |
| "4": {"class_type": "CLIPLoader", "inputs": {"clip_name": "minimax/qwen3vl_32b_minimax_h3_nvfp4_awq.safetensors", "type": "minimax", "device": "default"}}, | |
| "5": {"class_type": "VAELoader", "inputs": {"vae_name": "minimax/minimax_h3_video_vae_fp16.safetensors"}}, | |
| "6": {"class_type": "LoadImage", "inputs": {"image": image}}, | |
| "7": {"class_type": "ImageScale", "inputs": {"image": ["6", 0], "upscale_method": "lanczos", "width": 512, "height": 768, "crop": "center"}}, | |
| "8": {"class_type": "MiniMaxH3ImageToVideo", "inputs": {"clip": ["4", 0], "vae": ["5", 0], "prompt": prompt, "width": 512, "height": 768, "length": 56, "first_frame": ["7", 0]}}, | |
| "9": {"class_type": "RandomNoise", "inputs": {"noise_seed": seed}}, | |
| "10": {"class_type": "KSamplerSelect", "inputs": {"sampler_name": "lcm"}}, | |
| "11": {"class_type": "BasicScheduler", "inputs": {"model": ["3", 0], "scheduler": "simple", "steps": 3, "denoise": 1.0}}, | |
| "12": {"class_type": "BasicGuider", "inputs": {"model": ["3", 0], "conditioning": ["8", 0]}}, | |
| "13": {"class_type": "SamplerCustomAdvanced", "inputs": {"noise": ["9", 0], "guider": ["12", 0], "sampler": ["10", 0], "sigmas": ["11", 0], "latent_image": ["8", 1]}}, | |
| "14": {"class_type": "VAEDecode", "inputs": {"samples": ["13", 0], "vae": ["5", 0]}}, | |
| "15": {"class_type": "CreateVideo", "inputs": {"images": ["14", 0], "fps": 24.0}}, | |
| "16": {"class_type": "SaveVideo", "inputs": {"video": ["15", 0], "filename_prefix": prefix, "format": "mp4", "codec": "h264"}}, | |
| } | |
| def main(count): | |
| OUT.mkdir(exist_ok=True) | |
| rng = random.Random(0) | |
| chars = sorted(p for p in DATA.iterdir() if (p / "meta.json").exists())[:count] | |
| for i, char in enumerate(chars): | |
| meta = json.loads((char / "meta.json").read_text()) | |
| var = meta["variations"][i % len(meta["variations"])] | |
| motion = rng.choice(MOTIONS) | |
| if not meta["girl"]: | |
| motion = motion.replace("her ", "his ").replace(" her", " him") | |
| prompt = f"{var['moss_prompt']} Fixed camera. The character {motion}. Anime style, smooth animation." | |
| name = f"{char.name}_{Path(var['file']).stem}" | |
| if (OUT / f"{name}.json").exists(): | |
| continue | |
| image = upload(char / var["file"]) | |
| pid = post("/prompt", json.dumps({"prompt": graph(image, prompt, 1000 + i, f"anima_motion/{name}")}).encode(), {"Content-Type": "application/json"})["prompt_id"] | |
| t = time.time() | |
| while True: | |
| h = json.load(urllib.request.urlopen(f"{API}/history/{pid}")) | |
| if pid in h: | |
| break | |
| time.sleep(3) | |
| status = h[pid]["status"]["status_str"] | |
| files = [o["images"][0] for o in h[pid]["outputs"].values() if "images" in o] | |
| print(f"{name} {status} {time.time() - t:.0f}s {files[0]['filename'] if files else ''}", flush=True) | |
| if status == "success": | |
| (OUT / f"{name}.json").write_text(json.dumps({"prompt": prompt, "keyframe": str(char / var["file"]), | |
| "video": f"anima_motion/{files[0]['filename']}"})) | |
| if __name__ == "__main__": | |
| main(int(sys.argv[1])) | |