Using Python to generate XML files for visualization in Paraview

VTK is an open-source software system for “3D computer graphics, image processing, and visualization” developed by by Kitware. VTK is the foundation of Paraview, an industrial-strength CFD visualization tool that I have found to be very useful. I generate “second generation” XML-based files from my Python code and import them into Paraview for visualization. I am in the process of creating some Python classes to do, and I hope to publish them soon. Until then, I want to share some useful resources. The VTK file formats are specified in this document. It’s a pretty good specification, but it lacks some examples. Soon I will post an example of a valid unstructured, serial .vtu file. Each VTK file includes data from only one time step, so you have to keep track of time yourself (the filename is an easy solution). Paraview can read in data from multiple time steps, but you have to specify them in a .pvd file. This is also an XML file, with the following format: (reference)

<?xml version="1.0"?>
<VTKFile type="Collection" version="0.1" byte_order="LittleEndian">
<Collection>
<DataSet timestep="131" group="" part="0" file="particles_step131.vtu"/>
<DataSet timestep="417" group="" part="0" file="particles_step417.vtu"/>
<DataSet timestep="923" group="" part="0" file="particles_step923.vtu"/>
<DataSet timestep="1744" group="" part="0" file="particles_step1744.vtu"/>
<DataSet timestep="5113" group="" part="0" file="particles_step5113.vtu"/>
<DataSet timestep="17613" group="" part="0" file="particles_step17613.vtu"/>
<DataSet timestep="29999" group="" part="0" file="particles_step29999.vtu"/>
</Collection>
</VTKFile>

You can read in a single .pvd file from Paraview, and all the timestep files will automatically be read in.

In the near future, I plan to publish a Python class that handles the details of generating valid XML, which you can use as an example or as a foundation to create more advanced VTK output.

Leave a Reply