Exporting a 3D numpy to a VTK file for viewing in Paraview/Mayavi Exporting a 3D numpy to a VTK file for viewing in Paraview/Mayavi numpy numpy

Exporting a 3D numpy to a VTK file for viewing in Paraview/Mayavi


It's been forever and I had entirely forgotten asking this question but I ended up figuring it out. I've written a post about it in my blog (PyScience) providing a tutorial on how to convert between NumPy and VTK. Do take a look if interested:

pyscience.wordpress.com/2014/09/06/numpy-to-vtk-converting-your-numpy-arrays-to-vtk-arrays-and-files/


It's not a direct answer to your question, but if you have tvtk (if you have mayavi, you should have it), you can use it to write your data to vtk format. (See: http://code.enthought.com/projects/files/ETS3_API/enthought.tvtk.misc.html )

It doesn't use PyEVTK, and it supports a broad range of data sources (more than just structured and unstructured grids), so it will probably work where other things aren't.

As a quick example (Mayavi's mlab interface can make this much less verbose, especially if you're already using it.):

import numpy as npfrom enthought.tvtk.api import tvtk, write_datadata = np.random.random((10,10,10))grid = tvtk.ImageData(spacing=(10, 5, -10), origin=(100, 350, 200),                       dimensions=data.shape)grid.point_data.scalars = np.ravel(order='F')grid.point_data.scalars.name = 'Test Data'# Writes legacy ".vtk" format if filename ends with "vtk", otherwise# this will write data using the newer xml-based format.write_data(grid, 'test.vtk')

And a portion of the output file:

# vtk DataFile Version 3.0vtk outputASCIIDATASET STRUCTURED_POINTSDIMENSIONS 10 10 10SPACING 10 5 -10ORIGIN 100 350 200POINT_DATA 1000SCALARS Test%20Data doubleLOOKUP_TABLE default0.598189 0.228948 0.346975 0.948916 0.0109774 0.30281 0.643976 0.17398 0.374673 0.295613 0.664072 0.307974 0.802966 0.836823 0.827732 0.895217 0.104437 0.292796 0.604939 0.96141 0.0837524 0.498616 0.608173 0.446545 0.364019 0.222914 0.514992 ......


TVTK of Mayavi has a beautiful way of writing vtk files. Here is a test example I have written for myself following @Joe and tvtk documentation. The advantage it has over evtk, is the support for both ascii and html.Hope it will help other people.

from tvtk.api import tvtk, write_dataimport numpy as np#data = np.random.random((3, 3, 3))##i = tvtk.ImageData(spacing=(1, 1, 1), origin=(0, 0, 0))#i.point_data.scalars = data.ravel()#i.point_data.scalars.name = 'scalars'#i.dimensions = data.shape##w = tvtk.XMLImageDataWriter(input=i, file_name='spoints3d.vti')#w.write()points = np.array([[0,0,0], [1,0,0], [1,1,0], [0,1,0]], 'f')(n1, n2)  = points.shapepoly_edge = np.array([[0,1,2,3]])print n1, n2## Scalar Data#temperature = np.array([10., 20., 30., 40.])#pressure = np.random.rand(n1)### Vector Data#velocity = np.random.rand(n1,n2)#force     = np.random.rand(n1,n2)###Tensor Data with comp = 5stress = np.random.rand(n1,comp)##print stress.shape## The TVTK dataset.mesh = tvtk.PolyData(points=points, polys=poly_edge)### Data 0 # scalar data#mesh.point_data.scalars = temperature#mesh.point_data.scalars.name = 'Temperature'### Data 1 # additional scalar data#mesh.point_data.add_array(pressure)#mesh.point_data.get_array(1).name = 'Pressure'#mesh.update()### Data 2 # Vector data#mesh.point_data.vectors = velocity#mesh.point_data.vectors.name = 'Velocity'#mesh.update()### Data 3 additional vector data#mesh.point_data.add_array( force)#mesh.point_data.get_array(3).name = 'Force'#mesh.update()mesh.point_data.tensors = stressmesh.point_data.tensors.name = 'Stress'# Data 4 additional tensor Data#mesh.point_data.add_array(stress)#mesh.point_data.get_array(4).name = 'Stress'#mesh.update()write_data(mesh, 'polydata.vtk')# XML format # Method 1#write_data(mesh, 'polydata')# Method 2#w = tvtk.XMLPolyDataWriter(input=mesh, file_name='polydata.vtk')#w.write()