# Make-It-Animatable (MIA) v2: one-shot ML auto-rigger for humanoid meshes (GLB in, 52-bone
# Mixamo-style skeleton + skin weights out). Run by scripts/mia-run on fv-ml1 GPU 3, for dread-dev
# (Dread Naught), Prime's request 2026-10-01. NOT a seat: each run is a `docker run --rm` that
# rigs, writes the job's out/, exits and hands the GPU back.
#
# Code: github.com/jasongzy/Make-It-Animatable, branch v2 @ MIA_COMMIT (MIT), cloned with the
# submodules that commit pins (util/Hunyuan3D_21 @ b691197, util/auto_rig_pro, util/3dgs-render-
# blender-addon). Python: requirements.lock (dread-dev's proven CPU venv, torch family swapped to
# cu129). Upstream app_v2.py is UNPATCHED: dread-dev's CPU patch only upcasts on CPU.
#
# Weights are NOT baked in. /tank/aimodels/huggingface is bind-mounted read-only at /hf, and the two
# pinned snapshots are reached through the symlinks made below:
#   repo/output -> jasongzy/Make-It-Animatable @ MIA_WEIGHTS_REV  (output/best/v2/*.pth, Apache-2.0)
#   repo/data/Standard Run.fbx, repo/data/examples -> the same snapshot (init_blocks() reads
#     data/examples/log.csv even headless; without it the driver dies before the first mesh)
#   /opt/mia/hy3dgen/tencent/Hunyuan3D-2.1 -> tencent/Hunyuan3D-2.1 @ HY3D_REV (hunyuan3d-vae-v2-1)
# repo/data/Mixamo/bones.fbx -> "Standard Run.fbx": the official template sits in the GATED HF
# dataset jasongzy/Mixamo, whose terms were not accepted on Prime's account. It sets bone roll
# only, never joints or weights (dread-dev, 2026-10-01). Accepting those terms is Prime's call.
FROM python:3.11-slim-bookworm

ARG MIA_COMMIT=bbd8b158d88879c310ad130f9b25056935d221e9
ARG MIA_WEIGHTS_REV=ca0daf6cb164f939e77bf32667513fc7558d5f98
ARG HY3D_REV=0b94677654c57bb9a6b6845cd7b704ccf551d327

ENV DEBIAN_FRONTEND=noninteractive \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    HF_HOME=/hf \
    HF_HUB_OFFLINE=1 \
    GRADIO_ANALYTICS_ENABLED=False \
    MPLCONFIGDIR=/tmp/mpl \
    HY3DGEN_MODELS=/opt/mia/hy3dgen \
    HOME=/tmp

# git for the clone; the X11/GL libs are what the bpy 4.3 wheel links against (import fails without).
RUN apt-get update && apt-get install -y --no-install-recommends \
      git ca-certificates libgl1 libegl1 libglib2.0-0 libx11-6 libxrender1 libxxf86vm1 libxfixes3 \
      libxi6 libxkbcommon0 libsm6 libice6 libgomp1 \
    && rm -rf /var/lib/apt/lists/*

# UV_LINK_MODE=copy: uv's default clone (reflink) fails on the build's overlay fs, "os error 22".
COPY requirements.lock /opt/mia/requirements.lock
RUN pip install -q uv \
    && UV_LINK_MODE=copy uv pip install --system --no-cache --index-strategy unsafe-best-match \
         --extra-index-url https://download.pytorch.org/whl/cu129 \
         --extra-index-url https://download.blender.org/pypi/ \
         --extra-index-url https://miropsota.github.io/torch_packages_builder \
         --find-links https://data.pyg.org/whl/torch-2.8.0+cu129.html \
         -r /opt/mia/requirements.lock \
    && uv pip freeze --system > /opt/mia/freeze.txt

RUN git clone --single-branch --branch v2 https://github.com/jasongzy/Make-It-Animatable /opt/mia/repo \
    && cd /opt/mia/repo && git checkout -q "$MIA_COMMIT" && git submodule update --init --recursive -q \
    && git -C util/Hunyuan3D_21 rev-parse HEAD > /opt/mia/hunyuan3d_21.commit \
    && rm -rf .git util/*/.git output \
    && W=/hf/hub/models--jasongzy--Make-It-Animatable/snapshots/$MIA_WEIGHTS_REV \
    && ln -s "$W/output" output \
    && ln -s "$W/data/Standard Run.fbx" "data/Standard Run.fbx" \
    && ln -s "$W/data/examples" data/examples \
    && mkdir -p data/Mixamo && ln -s "../Standard Run.fbx" data/Mixamo/bones.fbx \
    && mkdir -p /opt/mia/hy3dgen/tencent \
    && ln -s /hf/hub/models--tencent--Hunyuan3D-2.1/snapshots/$HY3D_REV /opt/mia/hy3dgen/tencent/Hunyuan3D-2.1 \
    && printf 'MIA_COMMIT=%s\nMIA_WEIGHTS_REV=%s\nHY3D_REV=%s\n' "$MIA_COMMIT" "$MIA_WEIGHTS_REV" "$HY3D_REV" > /opt/mia/provenance

COPY mia_driver.py /opt/mia/mia_driver.py
WORKDIR /job
ENTRYPOINT ["python", "/opt/mia/mia_driver.py"]
