@echo off
setlocal
title 3D Maker Place - install TripoSG (Tripo's open weights)
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%\venv-triposg"
set "REPO=%ROOT%\TripoSG"
set "ADAPTER=%ROOT%\triposg-run.py"
set "VPY=%VENV%\Scripts\python.exe"

echo.
echo   3D Maker Place - TripoSG installer
echo   ---------------------------------
echo.
echo   Installs Tripo's own open-weight model - the fidelity of the hosted Tripo engine,
echo   running on this machine's GPU with no key and no per-run fee. The first run downloads
echo   around 12 GB of weights - 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 model's Python environment...
%PY% -m venv "%VENV%"
if not exist "%VPY%" goto novenv
:havevenv
"%VPY%" -m pip install --upgrade pip setuptools wheel

echo   Installing PyTorch (CUDA 12.1)...
"%VPY%" -m pip install --upgrade torch torchvision --index-url https://download.pytorch.org/whl/cu121
if errorlevel 1 "%VPY%" -m pip install --upgrade torch torchvision

rem ---------- model code ----------
if exist "%REPO%\triposg" goto haverepo
echo   Fetching TripoSG...
git clone --depth 1 https://github.com/VAST-AI-Research/TripoSG "%REPO%"
if not exist "%REPO%\triposg" goto norepo
:haverepo
rem Installed in small groups: one unavailable package cannot block the rest.
echo   Installing the model's dependencies...
"%VPY%" -m pip install --upgrade diffusers transformers accelerate safetensors einops omegaconf
"%VPY%" -m pip install --upgrade trimesh pillow opencv-python-headless huggingface_hub
"%VPY%" -m pip install --upgrade "numpy<2" scikit-image rembg onnxruntime

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.
echo "%REPO%" > "%VENV%\Lib\site-packages\triposg-path.pth"

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/triposg-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_TRIPOSG_SCRIPT=" "agent-config.bat" > "agent-config.tmp"
move /y "agent-config.tmp" "agent-config.bat" >nul
:writeconfig
>>"agent-config.bat" echo set "MP_TRIPOSG_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. TripoSG is installed and the agent now points at it:
echo     %ADAPTER%
echo.
echo   Your other pipelines are untouched. In the studio, pick TripoSG - it runs on this
echo   machine's GPU, free.
echo.
echo   Next:
echo     1. Right-click the orange tray icon and choose Restart.
echo     2. Start a generation and choose TripoSG.
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/ 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   TripoSG 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
