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

如何将trimesh图形窗口居中显示在屏幕上?

如何让trimesh的3D窗口居中显示

trimesh的show()方法默认没有直接设置窗口位置的参数,但可以通过手动创建viewer实例并操作窗口对象来实现居中效果,具体步骤如下:

实现步骤

  1. 加载模型后,创建GlfwViewer实例(trimesh默认的3D viewer),同时指定窗口分辨率
  2. 获取当前屏幕的尺寸,计算窗口居中的坐标(屏幕宽高的一半减去窗口宽高的一半)
  3. 调用窗口的set_position()方法设置居中位置,最后启动viewer

完整代码示例

import trimesh
from trimesh.viewer import GlfwViewer

# 加载模型
mesh = trimesh.load("machinist.XAML", process=False)

# 指定窗口分辨率
window_width, window_height = 600, 600

# 创建viewer实例
viewer = GlfwViewer(mesh, resolution=(window_width, window_height))

# 获取屏幕尺寸,计算居中坐标
screen_width, screen_height = viewer.window.get_screen_size()
pos_x = (screen_width - window_width) // 2
pos_y = (screen_height - window_height) // 2

# 设置窗口居中
viewer.window.set_position(pos_x, pos_y)

# 启动viewer
viewer.run()

补充说明

  • 如果你的trimesh使用的是其他viewer(比如PygameViewer),可以参考对应viewer的窗口API调整位置,核心逻辑都是计算屏幕中心坐标后设置窗口位置
  • resolution参数虽然未文档化,但目前在多个viewer实现中都支持用来初始化窗口尺寸

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 07:14:57