Spaces:
Running on Zero
Running on Zero
Download app.py from Upsampler/make-it-animatable: direct link, hf CLI and curl.
- Browser
- Download file 10 kB
-
https://huggingface.co/spaces/Upsampler/make-it-animatable/resolve/main/app.py
- Command line
-
hf download hf://spaces/Upsampler/make-it-animatable/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Upsampler/make-it-animatable/resolve/main/app.py
10 kB
| """ | |
| Make-It-Animatable for 3dvalley.com: the free lane of Auto-rig | |
| (https://3dvalley.com/tools/fix/auto-rig). A humanoid mesh in, the same mesh | |
| back with a Mixamo-named skeleton and skin weights, as a GLB (and the FBX it | |
| was exported as). One API endpoint for headless callers, plus a small demo UI. | |
| Differs from jasongzy/Make-It-Animatable-v2's Space (code at the commit | |
| deploy.sh pins) in these ways: | |
| - rigging only: no animation step, so no Auto-Rig Pro add-on and no Mixamo | |
| animation files; the only Mixamo file is the skeleton template the bones | |
| are laid out on | |
| - one GPU reservation per run instead of three: upstream's three | |
| @spaces.GPU calls would each reserve the 60 s default, 180 s in all, more | |
| than an anonymous visitor's daily quota, so they are imported without | |
| their decorators and run inside `_rig_on_gpu` | |
| - the weights and the template are downloaded file by file, not by cloning | |
| the model repo and the dataset | |
| - the output keeps the input's own position and size (Restore Global | |
| Transform) and its rest pose is reset to a T-pose | |
| """ | |
| import spaces # isort:skip | |
| import os | |
| import queue | |
| import shutil | |
| import sys | |
| import tempfile | |
| import time | |
| import types | |
| import gradio as gr | |
| import trimesh | |
| from huggingface_hub import hf_hub_download | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| CODE = os.path.join(HERE, "mia") | |
| TOKEN = os.environ.get("HF_TOKEN") | |
| os.environ["GRADIO_TEMP_DIR"] = os.path.join(tempfile.gettempdir(), "gradio") | |
| MAX_INPUT_FACES = 300_000 | |
| # What trimesh reads from one uploaded file: the input is loaded with it to merge duplicate vertices (below). | |
| # FBX it cannot read, and a .gltf usually keeps its data in files of its own. | |
| INPUT_TYPES = [".glb", ".obj", ".ply", ".stl"] | |
| # The upstream code reads its files relative to its own folder: the three | |
| # v2 checkpoints (Apache-2.0) and the Mixamo skeleton template (from the | |
| # author's gated dataset; the Space's token has accepted its terms). | |
| for name in ("bw_joints.pth", "joints_coarse.pth", "pose.pth"): | |
| target = os.path.join(CODE, "output", "best", "v2", name) | |
| if not os.path.isfile(target): | |
| os.makedirs(os.path.dirname(target), exist_ok=True) | |
| shutil.copy(hf_hub_download("jasongzy/Make-It-Animatable", f"output/best/v2/{name}", token=TOKEN), target) | |
| # FBX2glTF turns the rig's FBX into the GLB. An upload to the Space does not | |
| # keep a file's executable bit, so it is set again here (upstream's install.sh does the same). | |
| _converter = os.path.join(CODE, "util", "FBX2glTF") | |
| if os.path.isfile(_converter): | |
| os.chmod(_converter, 0o755) | |
| template = os.path.join(CODE, "data", "Mixamo", "bones.fbx") | |
| if not os.path.isfile(template): | |
| os.makedirs(os.path.dirname(template), exist_ok=True) | |
| shutil.copy(hf_hub_download("jasongzy/Mixamo", "bones.fbx", repo_type="dataset", token=TOKEN), template) | |
| # Upstream decorates each model call with @spaces.GPU. Imported under a | |
| # stand-in whose GPU decorator does nothing, they become plain functions that | |
| # `_rig_on_gpu` calls inside one reservation; the real module is put back. | |
| _real_spaces = sys.modules["spaces"] | |
| _stand_in = types.ModuleType("spaces") | |
| _stand_in.GPU = lambda fn=None, **_: fn if callable(fn) else (lambda inner: inner) | |
| sys.modules["spaces"] = _stand_in | |
| sys.path.insert(0, CODE) | |
| try: | |
| import app_v2 as mia # noqa: E402 | |
| finally: | |
| sys.modules["spaces"] = _real_spaces | |
| # The steps return Gradio updates keyed by the upstream UI's components, which | |
| # are built in its init_blocks(); this app has its own UI, so plain keys stand in. | |
| for _name in ( | |
| "state", | |
| "output_joints_coarse", | |
| "output_normed_input", | |
| "output_sample", | |
| "output_joints", | |
| "output_bw", | |
| "output_rest_vis", | |
| "output_rest_lbs", | |
| "output_anim_vis", | |
| "output_anim", | |
| ): | |
| setattr(mia, _name, f"_{_name}") | |
| # Models on cuda at module level: init_models() places them on the device, | |
| # which ZeroGPU emulates here and attaches for real inside the reservation. | |
| mia.init_models() | |
| os.chdir(CODE) | |
| from upsampler_theme import UPSAMPLER_CSS, UPSAMPLER_THEME, footer_html, header_html # noqa: E402 | |
| REST_POSES = {"As it stands": "No", "T-pose": "T-pose", "A-pose": "A-pose"} | |
| def _rig_duration(model_file, rest_pose="As it stands", fingers=True): | |
| # A warm run is a few seconds of network passes on 32,768 sampled points | |
| # plus the blend weights of every vertex; declare a typical run, not the | |
| # worst case (spaces/README.md, ZeroGPU rule 3). | |
| return 20 | |
| def _rig_on_gpu(db): | |
| """Joint localisation, blend weights, bones and pose: the three networks, one reservation.""" | |
| mia.preprocess(db) | |
| mia.infer(True, db) | |
| return db | |
| def rig(model_file, rest_pose="As it stands", fingers=True): | |
| """A humanoid model → the same model with a skeleton and skin weights: (GLB, FBX).""" | |
| if not model_file: | |
| raise gr.Error("Upload a 3D model of a humanoid character first.") | |
| path = model_file if isinstance(model_file, str) else model_file.name | |
| if os.path.splitext(path)[1].lower() not in INPUT_TYPES: | |
| raise gr.Error(f"Upload a {', '.join(t[1:].upper() for t in INPUT_TYPES)} file.") | |
| started = time.time() | |
| # A folder of its own per run: handlers run at once, and upstream names its outputs after the input file. | |
| work = tempfile.mkdtemp(prefix="rig-") | |
| source = os.path.join(work, "model.glb") | |
| # The weights are matched to Blender's mesh vertex by vertex, and Blender's glTF import merges vertices that | |
| # are exact duplicates (same place, normal and UV). A model with such duplicates (Quaternius' Casual, | |
| # 2026-11-08) then had more weights than vertices and failed. They are merged first, the same way; vertices | |
| # that differ in their UV or normal stay apart, so textures and hard edges are kept. | |
| mesh = trimesh.load(path, force="mesh") | |
| if not isinstance(mesh, trimesh.Trimesh) or len(mesh.faces) == 0: | |
| raise gr.Error("Upload a 3D model with a surface: this file has no faces.") | |
| mesh.merge_vertices(merge_tex=False, merge_norm=False) | |
| mesh.export(source) | |
| db = mia.DB() | |
| mia.fix_random() | |
| mia.prepare_input(source, is_gs=False, opacity_threshold=0.0, db=db, export_temp=False) | |
| if db.faces is not None and len(db.faces) > MAX_INPUT_FACES: | |
| raise gr.Error(f"The model has {len(db.faces):,} faces; rigging takes up to {MAX_INPUT_FACES:,}. Reduce it first.") | |
| db = _rig_on_gpu(db) | |
| mia.vis(bw_fix=True, bw_vis_bone="LeftArm", no_fingers=not fingers, restore_global=True, db=db) | |
| pose = REST_POSES.get(rest_pose, "No") | |
| mia.vis_blender( | |
| reset_to_rest=True, | |
| remove_fingers=not fingers, | |
| rest_pose_type=pose if pose != "No" else None, | |
| ignore_pose_parts=[], | |
| animation_file=None, | |
| retarget=False, | |
| inplace=False, | |
| restore_global=True, | |
| db=db, | |
| ) | |
| # The GLB is the one Blender writes itself, in the rest pose (upstream's "Rest Pose" preview): FBX2glTF's | |
| # conversion of the FBX came out mirrored, its faces inside out (2026-11-07). FBX2glTF's is the fallback. | |
| glb = db.rest_vis_path if db.rest_vis_path and os.path.isfile(db.rest_vis_path) else db.anim_vis_path | |
| fbx = db.anim_path | |
| if not (glb and os.path.isfile(glb) and fbx and os.path.isfile(fbx)): | |
| raise gr.Error("The rig could not be exported. Try a single, closed character model.") | |
| print(f"rigged {os.path.basename(path)} in {time.time() - started:.1f} s") | |
| return glb, fbx | |
| with gr.Blocks(title="Make-It-Animatable - Free AI Auto Rigging for 3D Characters") as demo: | |
| gr.HTML(header_html( | |
| "Make-It-Animatable: Free AI Auto Rigging", | |
| "Upload a humanoid character and get it back rigged: a Mixamo-named skeleton and skin weights, as GLB and FBX.", | |
| )) | |
| with gr.Row(): | |
| with gr.Column(): | |
| model_in = gr.File(label="3D model (GLB, OBJ, PLY or STL)", file_types=INPUT_TYPES) | |
| with gr.Accordion("Settings", open=False): | |
| rest_in = gr.Radio(list(REST_POSES), value="As it stands", label="The pose it stands in") | |
| fingers_in = gr.Checkbox(value=True, label="Finger bones") | |
| rig_btn = gr.Button("Rig", variant="primary") | |
| with gr.Column(): | |
| glb_out = gr.Model3D(label="Rigged model (GLB)", clear_color=[0, 0, 0, 0], height=420) | |
| fbx_out = gr.File(label="Rigged model (FBX)") | |
| rig_btn.click(rig, inputs=[model_in, rest_in, fingers_in], outputs=[glb_out, fbx_out], api_name="rig") | |
| gr.HTML(footer_html( | |
| "Make-It-Animatable rigs a humanoid 3D character in seconds: it places the joints of a Mixamo-compatible " | |
| "skeleton, fingers included, paints the skin weights of every vertex and returns the model as a GLB and an " | |
| "FBX ready to animate in Blender, Unity, Unreal or Godot, or with any Mixamo animation.", | |
| "https://3dvalley.com/tools/fix/auto-rig", | |
| "free AI auto rigging", | |
| )) | |
| if __name__ == "__main__": | |
| # No concurrency limit of our own: see spaces/README.md, ZeroGPU rules 1 and 2. | |
| demo.queue().launch( | |
| theme=UPSAMPLER_THEME, | |
| css=UPSAMPLER_CSS, | |
| ssr_mode=False, | |
| show_error=True, | |
| max_threads=200, | |
| allowed_paths=[tempfile.gettempdir()], | |
| prevent_thread_lock=True, | |
| ) | |
| # Blender (bpy) runs on the main thread only: upstream's export step hands | |
| # its work to this loop through a queue when called from a request thread. | |
| print("Main thread entering the Blender task loop") | |
| while True: | |
| try: | |
| task = mia.blender_task_queue.get() | |
| except KeyboardInterrupt: | |
| break | |
| if task is None: | |
| continue | |
| args, event, result_box = task | |
| try: | |
| import app_blender | |
| app_blender.main(args) | |
| except Exception as error: # handed back to the request that asked | |
| import traceback | |
| traceback.print_exc() | |
| result_box.append(error) | |
| finally: | |
| event.set() | |