BUG: vtkContextScene and vtkContextView broken (at least in Python)
I adapted the 2D-in-3D example to Python, and it segfaults on the first render after the window appears:
import vtk
colors = vtk.vtkNamedColors()
renwin = vtk.vtkRenderWindow()
renwin.SetMultiSamples(4)
renwin.SetSize(640, 480)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renwin)
renderer = vtk.vtkRenderer()
renwin.AddRenderer(renderer)
renderer.ResetCamera()
renderer.GetActiveCamera().SetPosition(1.0, 1.0, -4.0)
renderer.GetActiveCamera().Azimuth(40)
cube = vtk.vtkCubeSource()
cubeMapper = vtk.vtkPolyDataMapper()
cubeMapper.SetInputConnection(cube.GetOutputPort())
cubeActor = vtk.vtkActor();
cubeActor.SetMapper(cubeMapper)
renderer.AddActor(cubeActor)
cubeActor.GetProperty().SetRepresentationToSurface()
chart = vtk.vtkChartXY()
chartScene = vtk.vtkContextScene()
chartActor = vtk.vtkContextActor()
chart.SetAutoSize(False)
chart.SetSize(vtk.vtkRectf(0.0, 0.0, 320, 220))
chartScene.AddItem(chart)
chartActor.SetScene(chartScene)
renderer.AddActor(chartActor)
chartScene.SetRenderer(renderer)
# there is usually more here to actually populate the chart but it's not needed to get it to die
renwin.Render()
iren.Initialize()
iren.Start()
This minimal version also segfaults:
import vtk
vtk.vtkLogger.Init()
vtk.vtkLogger.SetStderrVerbosity(vtk.vtkLogger.VERBOSITY_MAX)
colors = vtk.vtkNamedColors()
renwin = vtk.vtkRenderWindow()
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renwin)
renderer = vtk.vtkRenderer()
renwin.AddRenderer(renderer)
chart = vtk.vtkChartXY()
chartScene = vtk.vtkContextScene()
chartActor = vtk.vtkContextActor()
chartScene.AddItem(chart)
chartActor.SetScene(chartScene)
renderer.AddActor(chartActor)
chartScene.SetRenderer(renderer)
renwin.Render()
Omitting the renderer.AddActor(chartActor)
line allows the window to survive, though of course nothing is plotted.
I tested this on Python 3.9 on Linux using VTK compiled some months ago (20201117) on g50ea6141 and just now on e34549ab (20200210), as well as Python 3.8 + VTK 9.0.1 from PyPi. The 20200210 loguru output is:
Loguru caught a signal: SIGSEGV
Stack trace:
24 0x5ed82e _start + 46
23 0x7f9b9c572cb2 __libc_start_main + 242
22 0x5ed92d Py_BytesMain + 45
21 0x6111e7 Py_RunMain + 695
20 0x61e0d4 PyRun_SimpleFileExFlags + 372
19 0x61e37a PyRun_FileExFlags + 186
18 0x61a6d4 python() [0x61a6d4]
17 0x61febb python() [0x61febb]
16 0x5f9207 PyEval_EvalCode + 39
15 0x513c8b _PyEval_EvalCodeWithName + 75
14 0x513ef5 python() [0x513ef5]
13 0x51af75 _PyEval_EvalFrameDefault + 23781
12 0x52134b _PyObject_MakeTpCall + 779
11 0x542d82 python() [0x542d82]
10 0x7f9b964aa1bd /home/larsoner/.local/lib/python3.9/site-packages/vtkmodules/vtkRenderingOpenGL2.cpython-39-x86_64-linux-gnu.so(+0xf71bd) [0x7f9b964aa1bd]
9 0x7f9b96328f97 vtkXOpenGLRenderWindow::Render() + 55
8 0x7f9b9628a21b vtkOpenGLRenderWindow::Render() + 187
7 0x7f9b993e073e vtkRenderWindow::Render() + 462
6 0x7f9b993e0a05 vtkRenderWindow::DoStereoRender() + 437
5 0x7f9b993f4400 vtkRendererCollection::Render() + 160
4 0x7f9b993edad9 vtkRenderer::Render() + 2457
3 0x7f9b9628f82e vtkOpenGLRenderer::DeviceRender() + 686
2 0x7f9b9628c307 vtkOpenGLRenderer::UpdateGeometry(vtkFrameBufferObjectBase*) + 2903
1 0x7f9b98282852 vtkContextActor::RenderOverlay(vtkViewport*) + 690
0 0x7f9b9c8efbb0 /lib/x86_64-linux-gnu/libpthread.so.0(+0x14bb0) [0x7f9b9c8efbb0]
( 0.169s) [main thread ] :0 FATL| Signal: SIGSEGV
For Python 3.8 + 9.0.1 the line numbers just change a little bit. I also adapted the simpler LinePlot example, which also hits also a segfault with the same trace:
from math import cos, sin
import vtk
vtk.vtkLogger.Init()
vtk.vtkLogger.SetStderrVerbosity(vtk.vtkLogger.VERBOSITY_MAX)
table = vtk.vtkTable()
arrX = vtk.vtkFloatArray()
arrX.SetName("X Axis")
table.AddColumn(arrX)
arrC = vtk.vtkFloatArray()
arrC.SetName("Cosine")
table.AddColumn(arrC)
arrS = vtk.vtkFloatArray()
arrS.SetName("Sine")
table.AddColumn(arrS)
numPoints = 69
inc = 7.5 / (numPoints-1)
table.SetNumberOfRows(numPoints)
for i in range(numPoints):
table.SetValue(i, 0, i * inc)
table.SetValue(i, 1, cos(i * inc))
table.SetValue(i, 2, sin(i * inc))
view = vtk.vtkContextView()
view.GetRenderer().SetBackground(1.0, 1.0, 1.0)
chart = vtk.vtkChartXY()
view.GetScene().AddItem(chart)
line = chart.AddPlot(vtk.vtkChart.LINE)
line.SetInputData(table, 0, 1)
line.SetColor(0, 255, 0, 255)
line.SetWidth(1.0)
line = chart.AddPlot(vtk.vtkChart.LINE)
line.SetInputData(table, 0, 2)
line.SetColor(255, 0, 0, 255)
line.SetWidth(5.0)
view.GetRenderWindow().Render()
view.GetInteractor().Initialize()
view.GetInteractor().Start()
This is all on Ubuntu 20.10 with NVIDIA. Standard 3D plotting with these builds works fine.