Seg Fault vtkXMLMultiBlockDataSetWriter with vtkPolyData

When I try to save a vtkMultiBlockDataSet that contains poly data that only has vertices, the vtkXMLMultiBlockDataSetWriter writer seg faults.

Below is a snippet to reproduce this segfault in Python. Does anyone know of a solution?

import vtk
import numpy as np
from vtk.util import numpy_support as nps

npoints = 100
points = np.random.rand(npoints, 3)

# Make VTK cells array
cells = np.hstack((np.ones((npoints, 1)),
                   np.arange(npoints).reshape(-1, 1)))
cells = np.ascontiguousarray(cells, dtype=np.int64)
vtkcells = vtk.vtkCellArray()
vtkcells.SetCells(npoints, nps.numpy_to_vtk(cells, deep=True, array_type=vtk.VTK_ID_TYPE))

# Convert points to vtk object
pts = vtk.vtkPoints()
pts.SetNumberOfPoints(npoints)
pts.SetData(nps.numpy_to_vtk(points))

# Create polydata
output = vtk.vtkPolyData()
output.SetPoints(pts)
output.SetVerts(vtkcells)

multi = vtk.vtkMultiBlockDataSet()
multi.SetNumberOfBlocks(1)
multi.SetBlock(0, output)

writer = vtk.vtkXMLMultiBlockDataWriter()
writer.SetFileName('foo.vtm')
writer.SetInputData(multi)

# AND THE SEG FAULT HAPPENS HERE
writer.Write()

If I reshape a cells array into a one-dimensional array with reshape call, it works fine for me.

# Make VTK cells array
cells = np.hstack((np.ones((npoints, 1)),
                   np.arange(npoints).reshape(-1, 1)))
cells = np.ascontiguousarray(cells, dtype=np.int64)
cells = np.reshape(cells, (2*npoints))
1 Like

That totally fixes it! Any insights for why it needs to be this way?