3dgenstudio-mirror/python-server/README.md
2026-07-08 23:08:30 +02:00

108 lines
4.5 KiB
Markdown

# 3D Gen Studio — Mesh Tools (Python service)
A small FastAPI service for CPU/GPU-heavy mesh operations implemented in Python.
The Node backend (`server.js`) proxies browser requests here, exactly like it
proxies to ComfyUI — so this service can run on the same machine or a different
host/port, configured under **Settings → Mesh Tools (Python) server**.
```
Browser ──POST /api/meshes/auto-uv──▶ Node (server.js) ──POST /meshes/auto-uv──▶ Python (this service)
◀── processed GLB ──────────────────────┘
```
## Endpoints
| Method | Path | Purpose |
| ------ | -------------------- | ------------------------------------ |
| GET | `/health` | Readiness probe |
| POST | `/meshes/auto-uv` | Auto UV unwrap |
| POST | `/meshes/auto-retopo`| Auto retopology |
Both mesh endpoints take `multipart/form-data`:
- `meshFile` — the mesh (GLB/OBJ/PLY/STL)
- `options` — JSON string of operation options (optional)
- `format` — output format, default `glb` (optional)
They respond with the processed mesh as a binary body and stats in headers:
`X-Vertex-Count`, `X-Face-Count`, `X-Has-Uv`.
Interactive API docs at `http://<host>:<port>/docs`.
## Run
```bat
run.bat
```
First run creates `.venv`, installs `requirements.txt`, and serves on
`0.0.0.0:8200`.
### GPU acceleration (NVIDIA)
On first setup `run.bat` runs `detect_cuda.py`: if an NVIDIA GPU is present it
installs `requirements-nvidia.txt` (NVIDIA Warp) plus the `cupy-cudaXXx` wheel
matching the CUDA version reported by `nvidia-smi` (e.g. CUDA 13.x → `cupy-cuda13x`).
These accelerate the Auto Retopo **watertight-shell** (CuPy) and **surface-projection**
(Warp) stages; the **remesh** stage has no GPU port and always runs on the CPU.
Both are auto-detected at runtime, so the service falls back to CPU when they are
absent. Two setup env vars:
| Variable | Effect |
| -------------------------- | --------------------------------------------------- |
| `MESHTOOLS_SKIP_GPU=1` | Skip GPU detection/install (CPU-only setup) |
| `MESHTOOLS_CUPY_PACKAGE` | Force a specific CuPy wheel, e.g. `cupy-cuda12x` |
To add GPU support to an existing `.venv`, activate it and run
`pip install -r requirements-nvidia.txt` then `pip install cupy-cuda13x` (match
your CUDA major).
Override host/port etc. with env vars:
| Variable | Default |
| -------------------------- | ------------------------ |
| `MESHTOOLS_HOST` | `0.0.0.0` |
| `MESHTOOLS_PORT` | `8200` |
| `MESHTOOLS_ALLOWED_ORIGINS`| dev origins |
| `MESHTOOLS_MAX_UPLOAD_BYTES`| `536870912` (512 MB) |
| `MESHTOOLS_WORK_DIR` | OS temp / scratch folder |
## Pipelines
The two operations are backed by the bundled packages:
- `app/services/autouv/` — the AutoUV unwrapper (`autouv.unwrap`)
- `app/services/autoretopo/` — the Auto-Retopo pipeline (`autoretopo.AutoRetopo`)
The thin bridges that the routes call:
- `app/services/auto_uv.py` → `run_auto_uv(mesh, options)` → `(trimesh, stats)`
- `app/services/auto_retopo.py` → `run_auto_retopo(mesh, options)` → `(trimesh, stats)`
Each receives a loaded `trimesh.Trimesh` and parsed options, and returns the
processed mesh plus a stats dict. Serialization, uploads, and error handling are
done in `app/routes/meshes.py`; the stats dict is returned to the browser in the
`X-Stats` response header. Every pipeline parameter is exposed in `app/schemas.py`
(`AutoUvOptions` / `AutoRetopoOptions`), which mirror `autouv.unwrap()` and
`autoretopo.RetopoConfig` 1:1.
## Layout
```
python-server/
├── main.py # FastAPI app + CORS + router wiring
├── requirements.txt
├── run.bat
└── app/
├── config.py # host/port/limits from env vars
├── schemas.py # option + stats models
├── meshio.py # trimesh load/export/stats
├── routes/
│ ├── health.py
│ └── meshes.py # /meshes/auto-uv, /meshes/auto-retopo
└── services/
├── auto_uv.py # bridge -> autouv.unwrap
├── auto_retopo.py # bridge -> autoretopo.AutoRetopo
├── autouv/ # bundled AutoUV package
└── autoretopo/ # bundled Auto-Retopo package
```