I would like to write 3D scalar data out to *.vtu file (using python) to be later picked up in Paraview and viewed volumetrically. I can write the *.vtu file, but Paraview crashes upon loading it. Have I used the vtk API incorrectly?
My approach: (Python 2.7.12, VTK_MAJOR_VERSION 6, Paraview 5.0.1)
I followed the only example I could find using PolyVertex objects here.
and came up with the following class:
import vtk
import numpy as np
class VtkPolyVertCloud(object):
def __init__(self):
self.points= vtk.vtkPoints()
self.grid = vtk.vtkUnstructuredGrid()
self.values = vtk.vtkDoubleArray()
self.values.SetName('point_values_array')
self.grid.SetPoints(self.points)
self.grid.GetPointData().SetScalars(self.values)
def add_polyVertex_cell(self, points, data):
"""
adds points according to user-supplied numpy arrays
@param points: numpy array of 3d point coords -- points.shape = (npoints, 3)
@param data: scalar-valued data belonging to each point -- data.shape = (npoints,)
"""
npts = points.shape[0]
pv = vtk.vtkPolyVertex()
pv.GetPointIds().SetNumberOfIds(npts)
for idx, point in enumerate(points):
pointID = self.points.InsertNextPoint(point)
pv.GetPointIds().SetId(idx, pointID)
self.values.InsertNextValue(data[idx])
self.grid.InsertNextCell(pv.GetCellType(), pv.GetPointIds())
I instantiate the class and attempt to write a simple random PolyVertex cell to XML file:
def test_vtkPolyVertexCloud_writeToFile():
""" adds a set of polyvertices meant to represent a finite element """
pc = vtku.VtkPolyVertCloud()
points, data = np.random.rand(10, 3), np.random.rand(10)
pc.add_polyVertex_cell(points, data)
# write
fn = 'test_PolyVertexCloud.vtu'
writer = vtk.vtkUnstructuredGridWriter()
writer.SetFileName(fn)
writer.SetInputData(pc.grid)
writer.Write()
Upon opening the file in Paraview, the Paraview Gui is unresponsive, then crashes.