Mayavi: Mayavi 离屏渲染不适用于 VTK 7.1.0

创建于 2017-01-18  ·  21评论  ·  资料来源: enthought/mayavi

我将 vtk 与 MesaOS 驱动程序一起使用,因为我想在无头模式下将渲染作为 jupyter notebook 的一部分运行,可以在此处找到有关包配置的更多详细信息:
https://github.com/conda-forge/vtk-feedstock/issues/20

当我使用 VTK 屏幕外示例时,它工作正常:

#! ./local/bin/python

from vtk import (vtkSphereSource, vtkPolyDataMapper, vtkActor, vtkRenderer,
        vtkRenderWindow, vtkWindowToImageFilter, vtkPNGWriter)

sphereSource = vtkSphereSource()
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(sphereSource.GetOutputPort())

actor = vtkActor()
actor.SetMapper(mapper)

renderer = vtkRenderer()
renderWindow = vtkRenderWindow()
renderWindow.SetOffScreenRendering(1)
renderWindow.AddRenderer(renderer)

renderer.AddActor(actor)
renderer.SetBackground(1, 1, 1)

renderWindow.Render()

windowToImageFilter = vtkWindowToImageFilter()
windowToImageFilter.SetInput(renderWindow)
windowToImageFilter.Update()

writer = vtkPNGWriter()
writer.SetFileName("sphere.png")
writer.SetInputConnection(windowToImageFilter.GetOutputPort())
writer.Write()

但是当我测试 mayavi 屏幕外示例时,它不起作用:

# Author: Prabhu Ramachandran <[email protected]>
# Copyright (c) 2007, Enthought, Inc.
# License: BSD Style.

from os.path import join, abspath, dirname

# The offscreen Engine.
from mayavi.api import OffScreenEngine

# Usual MayaVi imports
from mayavi.scripts.util import get_data_dir
from mayavi.sources.api import VTKXMLFileReader
from mayavi.modules.api import Outline, ScalarCutPlane, Streamline


def main():
    # Create the MayaVi offscreen engine and start it.
    e = OffScreenEngine()
    # Starting the engine registers the engine with the registry and
    # notifies others that the engine is ready.
    e.start()

    # Create a new scene.
    win = e.new_scene()

    # Now setup a normal MayaVi pipeline.
    src = VTKXMLFileReader()
    src.initialize(join(get_data_dir(dirname(abspath(__file__))),
                        'fire_ug.vtu'))
    e.add_source(src)
    e.add_module(Outline())
    e.add_module(ScalarCutPlane())
    e.add_module(Streamline())
    win.scene.isometric_view()
    # Change the size argument to anything you want.
    win.scene.save('offscreen.png', size=(800, 800))


if __name__ == '__main__':
    main()

我得到的错误信息是:

ERROR: In ../Rendering/OpenGL2/vtkXOpenGLRenderWindow.cxx, line 301
vtkXOpenGLRenderWindow (0x30ced20): bad X server connection. DISPLAY=Aborted

所以对我来说,屏幕外初始化似乎没有正确转发到 VTK。 不幸的是,我自己无法在 mayavi 中找到 VTK 的初始化。 对我来说,它似乎发生在tvtk_access.py ,但我仍然没有看到直接强制离屏渲染的选项。

最有用的评论

在远程 Jupyter-Hub 服务器提供的 Jupyter-Notebook 中使用 Mayavi 时,我遇到了类似的问题。 问题是没有运行 X 服务器。

我通过安装xvfbwrapper和 Xvfb 后端解决了这个问题。 在用 mayavi 做任何事情之前,先启动一个显示。

例子

#The following 3 lines must be before doing anything with mayavi (even importing)
from xvfbwrapper import Xvfb
vdisplay = Xvfb(width=1920, height=1080)
vdisplay.start()

from mayavi import mlab
mlab.init_notebook()
s = mlab.test_plot3d()
s

如果没有更优雅的方式,我认为最好在手册中提及这一点。

所有21条评论

我发现通过设置ETS_TOOLKIT='null'我可以使用以下方法重现此错误:

>>> from mayavi import mlab
>>> mlab.options.offscreen = True
>>> mlab.test_plot3d()
ERROR: In ../Rendering/OpenGL2/vtkXOpenGLRenderWindow.cxx, line 301
vtkXOpenGLRenderWindow (0x2e027d0): bad X server connection. DISPLAY=Aborted (core dumped)

如果未设置ETS_TOOLKIT='null'则即使导入也无效。 所以目前,当我尝试创建一个场景new_scene()时,上面的脚本失败

@jan-janssen - 你能用最新的大师测试吗,现在应该修复了。 您仍然需要export ETS_TOOLKIT=null但其余的现在应该可以工作了。

我已经使用 osmesa conda-forge vtk 包对此进行了测试,如果没有 X 服务器,我无法让它工作。 我想我的环境设置正确。

我所做的是:

$ export ETS_TOOLKIT='null'
$ conda create -n mayavi_osmesa -c conda-forge mesalib pyqt numpy vtk
$ pip install git+https://github.com/enthought/mayavi
$ python -c "from mayavi import mlab; mlab.options.offscreen=True; mlab.test_plot3d(); mlab.savefig('test.png')"
ERROR: In ../Rendering/OpenGL2/vtkXOpenGLRenderWindow.cxx, line 445
vtkXOpenGLRenderWindow (0x1c58c80): bad X server connection. DISPLAY=Aborted (core dumped)

好吧,实际上 OP 中给出的纯 VTK 示例也不起作用,所以我认为我的环境有问题。 我用直接的pip install git+https://github.com/enthought/mayavi得到同样的东西,所以我假设我在 vtk 的某个地方遗漏了一些东西。

感谢测试,你能检查一下安装的VTK是否有vtkOSOpenGLRenderWindow ,即
import vtk; print(hasattr(vtk, 'vtkOSOpenGLRenderWindow')) 。 如果不是,则 VTK 可能不是在 osmesa 支持下构建的。 如果是这样,我将需要仔细调查。

它没有那个。 我想你不知道控制是否存在?!

构建 VTK 时,您需要将VTK_OPENGL_HAS_OSMESA为 ON。

嗯,据我所知 conda-forge osmesa 包应该这样做。 我会去那里打开一个问题。

我认为较新的 mesa 版本不允许同时使用 mesa 和 osmesa,因此最近的 VTK 版本可能会在打开 vtk_use_x 时自动关闭 osmesa。 但我对此并不完全确定,因为很多事情可能已经改变了......

如果可能,您可以尝试使用像 VTK-7.x 这样的旧版本,或者在已知可以正常工作的任何时候尝试吗?

我在 Ubuntu 18、vtk 8.1.2、mayavi 4.6.2 上遇到相同的错误消息

我的mayavi也有这个问题。 有没有人找到任何解决方案? 我在服务器中使用 mayavi 时遇到了这个问题,尽管我将显示转发到我的本地计算机。

在远程 Jupyter-Hub 服务器提供的 Jupyter-Notebook 中使用 Mayavi 时,我遇到了类似的问题。 问题是没有运行 X 服务器。

我通过安装xvfbwrapper和 Xvfb 后端解决了这个问题。 在用 mayavi 做任何事情之前,先启动一个显示。

例子

#The following 3 lines must be before doing anything with mayavi (even importing)
from xvfbwrapper import Xvfb
vdisplay = Xvfb(width=1920, height=1080)
vdisplay.start()

from mayavi import mlab
mlab.init_notebook()
s = mlab.test_plot3d()
s

如果没有更优雅的方式,我认为最好在手册中提及这一点。

@max9111你还记得做其他事情吗? 我在安装xvfbwrapper后尝试了你的代码。 但是在使用 -X 标志 ssh 连接到我的服务器后,我仍然收到错误消息:

错误:在 ../Rendering/OpenGL2/vtkXOpenGLRenderWindow.cxx,第 606 行
vtkXOpenGLRenderWindow (0x55b43e61d590):无法创建 GLX 上下文。 中止。

@tjiagoM也许您忘记安装 xfb 本身(sudo apt-get install xvfb)?

@max9111哦,不,最初我犯了那个错误,但他们会抛出一个错误,说没有找到/活动的 xvfb(或类似的东西)

@ max9111我正在为远程 jupyterhub 上的相同设置xvfbwrapper ,但它对我没有用。 提前致谢!

@mirestrepo
不幸的是没有 docker 镜像。

我的设置是:

  • 在 CentOS7 上运行的远程工作站上的 Kubuntu 18.04(使用 Virtual Box)上运行的虚拟机。

  • 安装了最小的 JupyterHub

  • 使 Jupyterhub 从外部可用(端口转发、防火墙设置)

  • 这样在虚拟机中安装XfVB
    这里的关键点是获得一个有效的 X-Server(因此如何在 Docker 映像中执行与 GUI 相关的一般教程也可能有所帮助)我建议尝试使用链接中的简单脚本来检查这一点。 在这种情况下,您还会收到一条有意义的错误消息。

  • 如果这有效,那么在执行任何与 Mayavi 相关的事情之前启动 X-Server 很重要,如上面的示例所示。 如果在初始化 X-Server 之前导入 mayavi from mayavi import mlab ,mayavi 将崩溃。

@max9111我设法通过在我的 docker ENTRYPOINT ["tini", "-g", "--", "xvfb-run"]入口点启动xvfb来跳过 xvfb 包装器,但我只能让mlabpng后端...虽然我还没有尝试用OSMesa构建vtk ...

xvfb-run 可能会为您提供帮助:

 xvfb-run python your-py-script.py

请注意,您需要在运行上述命令之前安装 xvfb。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

Kekushke picture Kekushke  ·  9评论

Make42 picture Make42  ·  7评论

rambalachandran picture rambalachandran  ·  9评论

ktavabi picture ktavabi  ·  15评论

stefanoborini picture stefanoborini  ·  11评论