无头VTK测试

时间:2019-05-21 15:23:05

标签: python vtk headless

我有一个绘制四面体的简单VTK Python代码:

# plot a tetrahedron
import numpy
import vtk

node_coords = numpy.array(
    [
        [1.0, 0.0, -1.0 / numpy.sqrt(8)],
        [-0.5, +numpy.sqrt(3.0) / 2.0, -1.0 / numpy.sqrt(8)],
        [-0.5, -numpy.sqrt(3.0) / 2.0, -1.0 / numpy.sqrt(8)],
        [0.0, 0.0, numpy.sqrt(2.0) - 1.0 / numpy.sqrt(8)],
    ]
) / numpy.sqrt(3.0)
cells = numpy.array([[0, 1, 2, 3]])
cell_id = 0
line_width = 3.0


def get_line_actor(x0, x1, line_width=1.0):
    source = vtk.vtkLineSource()
    source.SetPoint1(x0)
    source.SetPoint2(x1)
    # mapper
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(source.GetOutputPort())
    # actor
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    # color actor
    actor.GetProperty().SetColor(0, 0, 0)
    actor.GetProperty().SetLineWidth(line_width)
    return actor


# Visualize
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)

for ij in [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]:
    x0, x1 = node_coords[cells[cell_id][ij]]
    renderer.AddActor(get_line_actor(x0, x1, line_width))
renderer.SetBackground(1.0, 1.0, 1.0)

renderWindow.Render()
renderWindowInteractor.Start()

enter image description here

我现在想为此代码添加连续的集成测试(在无头环境中运行),但是我得到的只是

Received 'aborted' signal

添加代码以关闭窗口

# renderWindowInteractor.Start()

render_window.Finalize()
del render_window, render_window_interactor

没有帮助。

如何在无头环境中使用VTK?

2 个答案:

答案 0 :(得分:1)

事实证明,是Render调用导致VTK抛出。对于单元测试,我可以将那些行注释掉

if render:
    renderWindow.Render()
    renderWindowInteractor.Start()

即使在无头环境中,其余代码也可以正确执行。

答案 1 :(得分:0)

对此,我认为答案是renderWindowrenderwindowInteractor并未按照此答案被删除

Close VTK window (Python)

在下面的代码行中添加代码即可解决此问题。

del renderWindow, renderWindowInteractor