Appending data field to a vtk file (Python VTK)

Thumbs up for the use of numpy containers. A pity that the vtk examples use that slow paradigm, so that the python folks are misled into writing unnecessarily slow code… I didn’t know that you can modify the returned numpy-array directly, that’s a nice!

Just want to point out that this question is about vtkPointData (or vtkCellData), as returned by poly.GetPointData() or poly.GetCellData(). Your pasted code acts on the points themselves, as returned by poly.GetPoints(). No big deal, just a couple of modifications required.

@Shahid My sample code simplifies to the following:

    data = np.asarray(data)
    data = np.atleast_2d(data)
    nPoints = source.GetNumberOfPoints()
    assert(nPoints==len(data))
    from vtk.util.numpy_support import numpy_to_vtk
    # Setting deep=True is a bit safer, but you lose a bit of performance.
    array = numpy_to_vtk(data, deep=True)
    array.SetName("arrayName")
    dataset = source.GetPointData()
    dataset.AddArray(array)
    dataset.Modified() # As suggested by Andras

See here for how the utils are implemented.