Hunyuan3D 2.1
在 Clore.ai 上使用腾讯 Hunyuan3D 2.1 从文本或图像生成 3D 网格
最后更新于
这有帮助吗?
这有帮助吗?
git clone https://github.com/Tencent/Hunyuan3D-2.git
cd Hunyuan3D-2
# 创建环境
conda create -n hunyuan3d python=3.10 -y
conda activate hunyuan3d
# 安装 PyTorch
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121
# 安装依赖
pip install -r requirements.txt
# 下载模型权重(首次运行会自动下载,总约 15 GB)
python -c "from hy3dgen.shapegen import Hunyuan3DDiTFlowMatchingPipeline; Hunyuan3DDiTFlowMatchingPipeline.from_pretrained('tencent/Hunyuan3D-2')"python gradio_app.py --port 7860 --sharefrom hy3dgen.shapegen import Hunyuan3DDiTFlowMatchingPipeline
from hy3dgen.texgen import Hunyuan3DPaintPipeline
# 第 1 阶段:形状生成
shape_pipeline = Hunyuan3DDiTFlowMatchingPipeline.from_pretrained(
"tencent/Hunyuan3D-2",
subfolder="shapegen",
)
shape_pipeline.to("cuda")
# 从文本生成网格
mesh = shape_pipeline(
prompt="a detailed medieval sword with ornate handle",
增加推理步数以提高稳定性
guidance_scale=7.5,
seed=42,
)[0]
mesh.export("sword_shape.glb")# 第 2 阶段:贴图合成
texture_pipeline = Hunyuan3DPaintPipeline.from_pretrained(
"tencent/Hunyuan3D-2",
subfolder="texgen",
)
texture_pipeline.to("cuda")
textured_mesh = texture_pipeline(
mesh=mesh,
prompt="a detailed medieval sword with ornate handle",
num_inference_steps=20,
seed=42,
)[0]
textured_mesh.export("sword_textured.glb")from PIL import Image
# 加载参考图像
image = Image.open("reference_chair.png")
# 从图像生成形状
mesh = shape_pipeline(
image=image,
增加推理步数以提高稳定性
guidance_scale=7.5,
seed=42,
)[0]
# 应用贴图
textured = texture_pipeline(
mesh=mesh,
image=image,
num_inference_steps=20,
seed=42,
)[0]
textured.export("chair.glb")from pathlib import Path
prompts = [
"a red sports car, low-poly game asset",
"a wooden treasure chest, PBR material",
"a sci-fi helmet with visor, hard surface",
]
output_dir = Path("/workspace/3d-output")
output_dir.mkdir(exist_ok=True)
for i, prompt in enumerate(prompts):
mesh = shape_pipeline(prompt=prompt, num_inference_steps=30, seed=42)[0]
textured = texture_pipeline(mesh=mesh, prompt=prompt, num_inference_steps=20, seed=42)[0]
textured.export(str(output_dir / f"asset_{i:03d}.glb"))
print(f"Generated: asset_{i:03d}.glb — {prompt[:40]}")# GLB(推荐 — 包含贴图,通用格式)
textured_mesh.export("model.glb")
# OBJ(带 MTL 材质文件)
textured_mesh.export("model.obj")
# PLY(顶点颜色,兼容点云)
textured_mesh.export("model.ply")