You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在服务端使用Python或NodeJS为GLTF 3D模型生成截图

Python服务端实现GLTF模型截图的具体方案

服务端运行无需前端GUI,采用离屏离线渲染技术即可在内存中完成3D场景渲染并直接导出为图片,以下是两种可落地的实现方案:


方案1:Trimesh + Pyrender 轻量实现

适合轻量级简单GLTF模型,依赖少、集成速度快。

依赖安装

pip install trimesh pyrender pillow numpy
# Linux无桌面环境需要额外安装OSMesa依赖
# Debian/Ubuntu系执行:apt install libosmesa6-dev

示例代码

import os
# 适配无显示设备的服务端环境
os.environ["PYOPENGL_PLATFORM"] = "osmesa"
import trimesh
import pyrender
import numpy as np
from PIL import Image

# 1. 加载GLTF模型
mesh_scene = trimesh.load("your_model.gltf", force="scene")

# 2. 调整模型摆放位置:自定义变换矩阵实现平移、旋转、缩放
transform_matrix = np.array([
    [1, 0, 0, 0],  # 最后一位为x轴平移量
    [0, 1, 0, 0],  # 最后一位为y轴平移量
    [0, 0, 1, 0],  # 最后一位为z轴平移量
    [0, 0, 0, 1]
])
# 给场景内所有模型应用变换
for node in mesh_scene.geometry.values():
    node.apply_transform(transform_matrix)

# 3. 组装渲染场景
render_scene = pyrender.Scene.from_trimesh_scene(mesh_scene)

# 4. 配置相机,按需调整视角、位置
camera = pyrender.PerspectiveCamera(yfov=np.pi / 3.0, aspectRatio=1.0)
camera_pose = np.array([
    [1, 0, 0, 0],
    [0, 1, 0, 0],
    [0, 0, 1, 5], # 相机放在z轴5的位置,可按需调整
    [0, 0, 0, 1]
])
render_scene.add(camera, pose=camera_pose)

# 5. 添加光源
light = pyrender.DirectionalLight(color=np.ones(3), intensity=3.0)
render_scene.add(light, pose=camera_pose)

# 6. 离屏渲染,分辨率可自定义
renderer = pyrender.OffscreenRenderer(viewport_width=1024, viewport_height=1024)
color, depth = renderer.render(render_scene)

# 7. 保存图片
Image.fromarray(color).save("render_result.png")

方案2:Blender Python API 高精度实现

适合带PBR材质、动画、复杂纹理的GLTF模型,材质还原度更高。
首先确保服务器安装Blender,Debian/Ubuntu可直接执行apt install blender。

示例脚本(命名为render_gltf.py)

import bpy
import math

# 清空Blender默认场景
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()

# 1. 导入GLTF模型
bpy.ops.import_scene.gltf(filepath="your_model.gltf")

# 2. 调整模型位置、旋转、缩放
model = bpy.context.selected_objects[0]
model.location = (0, 0, 0) # 平移坐标
model.rotation_euler = (math.radians(45), 0, math.radians(30)) # 旋转角度
model.scale = (1, 1, 1) # 缩放比例

# 3. 添加相机
bpy.ops.object.camera_add(location=(0, -5, 2))
camera = bpy.context.object
bpy.context.scene.camera = camera
# 相机自动对准模型
track_constraint = camera.constraints.new(type='TRACK_TO')
track_constraint.target = model
track_constraint.track_axis = 'TRACK_NEGATIVE_Z'
track_constraint.up_axis = 'UP_Y'

# 4. 添加光源
bpy.ops.object.light_add(type='SUN', location=(5, -5, 10))
light = bpy.context.object
light.data.energy = 4.0

# 5. 配置渲染参数
bpy.context.scene.render.resolution_x = 1920
bpy.context.scene.render.resolution_y = 1080
bpy.context.scene.render.image_settings.file_format = 'PNG'
bpy.context.scene.render.filepath = 'render_result.png'

# 6. 执行渲染
bpy.ops.render.render(write_still=True)

后台运行命令

blender --background --python render_gltf.py

注意事项

  • Linux无桌面环境运行时,优先配置OSMesa或者VirtualGL作为离屏渲染后端,避免出现cannot open display类报错
  • 对材质还原度要求高的场景优先选择Blender方案,轻量快速调用优先选择Pyrender方案
  • 相机参数、光照参数、渲染分辨率都可以根据实际需求调整,控制最终导出图片的效果

内容的提问来源于stack exchange,提问作者Tom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 02:45:03