@echo off
setlocal
title 3D Maker Place - install Hunyuan3D-2 (high detail)
cd /d "%~dp0"
set "BASE=https://3dmakerplace.com"
echo %BASE% | findstr /i /c:"localhost" /c:"127.0.0.1" >nul 2>nul && set "BASE=https://3dmakerplace.base44.app"
set "ROOT=%USERPROFILE%\maker-pipelines"
set "VENV=%ROOT%\venv310"
set "REPO=%ROOT%\Hunyuan3D-2"
set "ADAPTER=%ROOT%\hunyuan3d-run.py"
set "VPY=%VENV%\Scripts\python.exe"

echo.
echo   3D Maker Place - Hunyuan3D-2 installer
echo   -------------------------------------
echo.
echo   Installs the high-detail model (the open-weight class Meshy and Tripo come from)
echo   into its own Python 3.10 environment, next to your TripoSR pipeline. The first run
echo   downloads around 10 GB - keep this window open.
echo.

if not exist "%ROOT%" mkdir "%ROOT%"

rem ---------- Git ----------
where git >nul 2>nul
if not errorlevel 1 goto havegit
if exist "%ProgramFiles%\Git\cmd\git.exe" set "PATH=%ProgramFiles%\Git\cmd;%PATH%"
where git >nul 2>nul
if not errorlevel 1 goto havegit
echo   Installing Git...
winget install --id Git.Git -e --source winget --silent --accept-package-agreements --accept-source-agreements >nul 2>nul
set "PATH=%ProgramFiles%\Git\cmd;%PATH%"
where git >nul 2>nul
if errorlevel 1 goto nogit
:havegit
echo   Git ready.

rem ---------- Python 3.10 ----------
set "PY="
py -3.10 -c "import sys" >nul 2>nul
if not errorlevel 1 set "PY=py -3.10"
if defined PY goto havepython
echo   Installing Python 3.10...
winget install --id Python.Python.3.10 -e --source winget --silent --accept-package-agreements --accept-source-agreements >nul 2>nul
set "PATH=%LocalAppData%\Programs\Python\Python310;%LocalAppData%\Programs\Python\Python310\Scripts;%PATH%"
py -3.10 -c "import sys" >nul 2>nul
if not errorlevel 1 set "PY=py -3.10"
if defined PY goto havepython
goto nopython
:havepython
echo   Python 3.10 ready.

rem ---------- environment ----------
if exist "%VPY%" goto havevenv
echo   Creating the Python environment...
%PY% -m venv "%VENV%"
if not exist "%VPY%" goto novenv
:havevenv
"%VPY%" -m pip install --upgrade pip setuptools wheel

echo   Installing PyTorch 2.5 (CUDA 12.1) - the build this model expects...
"%VPY%" -m pip install --upgrade torch==2.5.1 torchvision==0.20.1 --index-url https://download.pytorch.org/whl/cu121
if errorlevel 1 "%VPY%" -m pip install --upgrade torch torchvision

rem ---------- model code ----------
if exist "%REPO%\hy3dgen\shapegen" goto haverepo
echo   Fetching Hunyuan3D-2...
git clone --depth 1 https://github.com/Tencent-Hunyuan/Hunyuan3D-2 "%REPO%"
if not exist "%REPO%\hy3dgen\shapegen" goto norepo
:haverepo
rem Installed in small groups: one unavailable package cannot block the rest.
rem The versions are pinned to what still accepts torch 2.5.1 - the newest diffusers and
rem transformers want torch 2.6 and up, and opencv 5 wants numpy 2, which this model cannot
rem use. Unpinned, the install succeeds and then the model refuses to load.
echo   Installing the shape-stage dependencies...
"%VPY%" -m pip install --upgrade "numpy<2" "diffusers==0.30.0" "transformers>=4.46,<4.50" "huggingface_hub>=0.25,<0.31"
"%VPY%" -m pip install --upgrade accelerate omegaconf einops regex safetensors
"%VPY%" -m pip install --upgrade "trimesh>=4.4,<5" pygltflib pillow "opencv-python-headless<5"
"%VPY%" -m pip install --upgrade onnxruntime rembg "scipy<1.15"

rem The package is imported straight out of the checkout, so a build step that fails on this
rem PC cannot leave the model unreachable. The path is written unquoted and without a trailing
rem space: a .pth line is read literally, so a quoted one points nowhere and the model then
rem reads as "No module named 'hy3dgen'" however complete the install is.
"%VPY%" -c "import sys, pathlib; pathlib.Path(sys.argv[1]).write_text(sys.argv[2])" "%VENV%\Lib\site-packages\hunyuan3d-path.pth" "%REPO%"
echo   Model code: %REPO%

rem ---------- adapter ----------
echo   Fetching the runner adapter...
powershell -NoProfile -ExecutionPolicy Bypass -Command "try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -UseBasicParsing -Uri '%BASE%/api/agent/hunyuan-adapter' -OutFile '%ADAPTER%' } catch { exit 1 }"
if not exist "%ADAPTER%" goto noadapter

echo   Checking the install...
"%VPY%" "%ADAPTER%" --check
if errorlevel 1 goto checkfailed

rem ---------- wire it into the agent ----------
if exist "agent-config.bat" goto trimconfig
> "agent-config.bat" echo @echo off
goto writeconfig
:trimconfig
findstr /v /i /c:"MP_HUNYUAN3D_SCRIPT=" "agent-config.bat" > "agent-config.tmp"
move /y "agent-config.tmp" "agent-config.bat" >nul
:writeconfig
>>"agent-config.bat" echo set "MP_HUNYUAN3D_SCRIPT=%ADAPTER%"
findstr /i /c:"MP_GENERATE_COMMAND=" "agent-config.bat" >nul 2>nul
if not errorlevel 1 goto havegen
>>"agent-config.bat" echo set "MP_GENERATE_COMMAND=node %~dp0openmesh-runner.mjs"
:havegen

echo.
echo   Done. Hunyuan3D-2 is installed and the agent now points at it:
echo     %ADAPTER%
echo.
echo   Your TripoSR pipeline is untouched. In the studio, pick Hunyuan3D (or OpenMesh) for
echo   the high-detail model, and TripoSR stays the fast one.
echo.
echo   Next:
echo     1. Right-click the orange tray icon and choose Restart.
echo     2. Start a generation and choose Hunyuan3D - it runs on this machine's GPU, free.
echo.
if not "%MP_NO_PAUSE%"=="1" pause
exit /b 0

:nogit
echo.
echo   Git could not be installed. Install it from https://git-scm.com, then run this file again.
echo.
pause
exit /b 1

:nopython
echo.
echo   Python 3.10 could not be installed. Get it from https://www.python.org/downloads/release/python-31011/ and run this file again.
echo.
pause
exit /b 1

:novenv
echo.
echo   The Python environment could not be created at %VENV%.
echo   Run this file again from a normal (non-admin) prompt.
echo.
pause
exit /b 1

:norepo
echo.
echo   Hunyuan3D-2 could not be fetched from GitHub. Check your connection and run this file again.
echo.
pause
exit /b 1

:noadapter
echo.
echo   The runner adapter could not be downloaded from %BASE%.
echo   Check your connection and run this file again.
echo.
pause
exit /b 1

:checkfailed
echo.
echo   Python could not load the model. The install is incomplete:
echo     "%VPY%" "%ADAPTER%" --check
echo   Run that line to see the missing package, or run this file again.
echo.
pause
exit /b 1
