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

Salabim 3D动画示例程序运行失败,求排查环境及代码问题

问题:运行Salabim 3D动画示例时出现GLUT初始化错误

我运行Salabim(Python离散事件仿真包)的3D动画示例程序时,在env.run(sim.inf)处触发错误,相关代码、错误信息及环境配置步骤如下:

示例代码

import salabim as sim

env = sim.Environment()
env.background_color("90%gray")
env.width(900)
env.height(700)
env.position((1000, 0))

env.width3d(900)
env.height3d(700)
env.position3d((0, 100))

env.animate(True)
env.animate(True)
env.animate3d(True)

env.show_camera_position()
env.show_camera_position(over3d=True)

sim.Animate3dGrid(x_range=range(-2, 3), y_range=range(-2, 3))
traj0 = sim.TrajectoryCircle(radius=2, vmax=1)
d0 = traj0.duration()
sim.Animate3dBox(x_len=1, y_len=1, z_len=1, color="red", x=lambda t: traj0.x(t % d0), y=lambda t: traj0.y(t % d0),
                 z=0.5, z_angle=lambda t: traj0.angle(t % d0))

traj1 = sim.TrajectoryCircle(radius=1, vmax=1, angle0=360, angle1=0)
d1 = traj1.duration()
sim.Animate3dBox(x_len=0.5, y_len=0.5, z_len=0.5, color="green", x=lambda t: traj1.x(t % d1),
                 y=lambda t: traj1.y(t % d1), z=0.25, z_angle=lambda t: traj1.angle(t % d1))

env.run(sim.inf)

错误信息

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\rtap\.conda\envs\test_env\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\rtap\.conda\envs\test_env\lib\tkinter\__init__.py", line 814, in callit
    func(*args)
  File "C:\Users\rtap\.conda\envs\test_env\lib\site-packages\salabim\salabim.py", line 12881, 
in simulate_and_animate_loop
     self.animation3d_init()
  File "C:\Users\rtap\.conda\envs\test_env\lib\site-packages\salabim\salabim.py", line 10298, in animation3d_init
    glut.glutInit()
  File "C:\Users\rtap\.conda\envs\test_env\lib\site-packages\OpenGL\GLUT\special.py", line 333, in glutInit
    _base_glutInit( ctypes.byref(count), holder )
  File "C:\Users\rtap\.conda\envs\test_env\lib\site-packages\OpenGL\platform\baseplatform.py", line 423, in __call__
    raise error.NullFunctionError(
OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit, check for bool(glutInit) before calling
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\rtap\.conda\envs\test_env\lib\site-packages\salabim\salabim.py", line 12849, in run
    raise SimulationStopped
salabim.salabim.SimulationStopped

环境配置步骤

conda create -n test_env python=3.9
conda activate test_env
conda install pip
pip install pillow
pip install pyopengl
pip install pyopengl_accelerate
pip install pywavefront
pip install pyglet==1.5.27
pip install opencv-python
pip install salabim
pip install greenlet

请问哪里操作有误?环境配置是否正确?


解决方法

这个错误的核心是系统缺少GLUT(OpenGL Utility Toolkit)的底层二进制依赖库——PyOpenGL只是Python绑定层,必须依赖本地的GLUT库才能正常调用glutInit函数。

具体修复步骤

  • 安装GLUT底层库(Windows系统)

    1. 下载与你的Python环境(32/64位)匹配的GLUT库文件,通常包含glut32.dll/glut64.dll、glut.h、glut.lib。
    2. 将glut32.dll(64位用glut64.dll)复制到系统目录:64位系统放C:\Windows\System32,32位系统放C:\Windows\SysWOW64。
    3. 将glut.h复制到你的conda环境的GL include目录,比如C:\Users\rtap\.conda\envs\test_env\Lib\site-packages\OpenGL\GL。
    4. 将glut.lib复制到环境的libs目录,比如C:\Users\rtap\.conda\envs\test_env\libs。
  • 验证GLUT加载状态
    在环境中运行以下代码,若输出True则表示库已正确加载:

    from OpenGL.GLUT import *
    print(bool(glutInit))
    
  • 简化环境(可选)
    你的环境安装了部分非必要依赖(如opencv-python、pywavefront),可以卸载减少冲突:

    pip uninstall opencv-python pywavefront
    

    Salabim 3D动画的核心依赖仅为pyopengl、pyglet和GLUT,保留这些即可。

替代方案(Anaconda环境)

尝试通过conda直接安装GLUT相关包,避免手动配置文件:

conda install -c conda-forge freeglut

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 21:24:53