Views
No views yet
| Resolution | Time |
|---|---|
| 512³ | ~3 seconds |
| 1024³ | ~17 seconds |
| 1536³ | ~60 seconds |
1import os
2os.environ['OPENCV_IO_ENABLE_OPENEXR'] = '1'
3os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True" # Can save GPU memory
4import cv2
5import imageio
6from PIL import Image
7import torch
8from trellis2.pipelines import Trellis2ImageTo3DPipeline
9from trellis2.utils import render_utils
10from trellis2.renderers import EnvMap
11import o_voxel
12
13# 1. Setup Environment Map
14envmap = EnvMap(torch.tensor(
15 cv2.cvtColor(cv2.imread('assets/hdri/forest.exr', cv2.IMREAD_UNCHANGED), cv2.COLOR_BGR2RGB),
16 dtype=torch.float32, device='cuda'
17))
18
19# 2. Load Pipeline
20pipeline = Trellis2ImageTo3DPipeline.from_pretrained("microsoft/TRELLIS.2-4B")
21pipeline.cuda()
22
23# 3. Load Image & Run
24image = Image.open("assets/example_image/T.png")
25mesh = pipeline.run(image)[0]
26mesh.simplify(16777216) # nvdiffrast limit
27
28# 4. Render Video
29video = render_utils.make_pbr_vis_frames(render_utils.render_video(mesh, envmap=envmap))
30imageio.mimsave("sample.mp4", video, fps=15)
31
32# 5. Export to GLB
33glb = o_voxel.postprocess.to_glb(
34 vertices = mesh.vertices,
35 faces = mesh.faces,
36 attr_volume = mesh.attrs,
37 coords = mesh.coords,
38 attr_layout = mesh.layout,
39 voxel_size = mesh.voxel_size,
40 aabb = [[-0.5, -0.5, -0.5], [0.5, 0.5, 0.5]],
41 decimation_target = 1000000,
42 texture_size = 4096,
43 remesh = True,
44 remesh_band = 1,
45 remesh_project = 0,
46 verbose = True
47)
48glb.export("sample.glb", extension_webp=True)@article{
xiang2025trellis2,
title={Native and Compact Structured Latents for 3D Generation},
author={Xiang, Jianfeng and Chen, Xiaoxue and Xu, Sicheng and Wang, Ruicheng and Lv, Zelong and Deng, Yu and Zhu, Hongyuan and Dong, Yue and Zhao, Hao and Yuan, Nicholas Jing and Yang, Jiaolong},
journal={Tech report},
year={2025}
}