TRELLIS.2 icon indicating copy to clipboard operation
TRELLIS.2 copied to clipboard

Texturing: make the keep-existing-UVs path reachable (and non-crashing once it is)

Open xocialize opened this issue 4 months ago • 0 comments

Problem

Trellis2TexturingPipeline.postprocess_mesh implements a keep-existing-UVs branch, but it is unreachable via run(): preprocess_mesh rebuilds the mesh as trimesh.Trimesh(vertices=vertices, faces=mesh.faces, process=False) and drops mesh.visual, so by the time postprocess_mesh checks mesh.visual.uv, the UVs are always gone and every input falls into the uv-unwrap branch. The shipped "texture an existing mesh preserving its UVs" capability silently never engages.

Fix

  1. preprocess_mesh: pass visual=mesh.visual through the rebuilt Trimesh. The rebuild doesn't change vertex order (only positions), so per-vertex UVs remain valid.
  2. postprocess_mesh: copy vertices/faces/normals with np.array() up front. mesh.vertex_normals is a read-only trimesh cache, and the glTF axis swap at the end writes it in place (normals[:, 1], normals[:, 2] = ...). Today only the uv-unwrap branch runs, and it happens to replace normals with a fresh indexed array — so this second bug is latent until fix (1) makes the UV-preserve branch reachable, at which point it crashes. The two fixes belong in the same PR.

Evidence

With these changes, re-texturing an existing UV-mapped mesh preserves its UVs bit-identically — we measured max UV delta 0.0 between input and output in production use (Apple Silicon port work, https://github.com/xocialize/trellis2-apple).

🤖 Generated with Claude Code

xocialize avatar Jul 17 '26 00:07 xocialize