I have a simple VTK Python code that plots a tetrahedron:
# 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()
I would now like to add continuous integration testing for this code (which runs in in a headless environment), but all I'm getting is
Received 'aborted' signal
Adding code for closing the window
# renderWindowInteractor.Start()
render_window.Finalize()
del render_window, render_window_interactor
does not help.
How to use VTK in a headless environment?
It turns out that it's the Render
call that makes VTK throw. For unit tests, I could just comment out those lines
if render:
renderWindow.Render()
renderWindowInteractor.Start()
The rest of the code will execute properly even in a headless environment.