3

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.

Priyantha
  • 4,839
  • 6
  • 26
  • 46
user3482876
  • 249
  • 2
  • 11
  • I managed to fix the problem. Note that `vtkUnstructuredGridWriter()` should be `vtkXMLUnstructuredGridWriter()`. Now my question is why does this crash Paraview? – user3482876 Aug 16 '17 at 16:20
  • It works fine for me, no crash, neither with the Legacy vtk file (vtkUnstructuredGridWriter()) nor with the XML. – mululu Aug 17 '17 at 07:07

1 Answers1

4

An arguably easier approach would be to use meshio (a project of mine). You can easily write unstructured meshes in a variety of formats, e.g., VTK/VTU. Install with

pip3 install meshio [--user]

(no dependency on vtk even) and use it like

import meshio
import numpy

points = numpy.array([
    [0.0, 0.0, 0.0],
    [0.0, 1.0, 0.0],
    [0.0, 0.0, 1.0],
    ])
cells = {
    "triangle": numpy.array([
        [0, 1, 2]
        ])
    }
meshio.write_points_cells(
    "foo.vtu",
    points,
    cells,
    # Optionally provide extra data on points, cells, etc.
    # point_data=point_data,
    # cell_data=cell_data,
    # field_data=field_data
    )
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249