VTK/Writing VTK files using python
Before starting, you are strongly advised to read the VTK file formats documentation here.
Introduction
The purpose of this wiki is to show how one can write VTK files using python.
Why using python ? Because it's beautiful & powerful, of course ! ;-)
Obviously, you can use your favourite programming language to write such files. Mine is python. No more, no less.
In the following, the data you want to write to a VTK file are supposed to arise from scipy/numpy arrays. Please browse scipy.org to get more information if needed.
Before writing VTK files, you have to know that there are two kinds of VTK files: "legacy" VTK files and "XML" VTK files. The first kind is quite obsolete now, but still widely used. Although it is easier to write than the XML kind (understand: with low-level instructions like "printf" in C or "print" in python), this wiki will focus on the last kind, the XML format.
Datasets format
As you should already be aware if you have read the VTK file formats documentation, VTK can handle several kinds of datasets. The primary are:
- StructuredPoints (aka ImageData, see later);
- RectilinearGrid;
- StructuredGrid;
- UnstructuredGrid;
- PolyData.
Briefly:
- Structured Points is the "simplest" format: you only have to set the mesh dimensions (nx, ny, nz), mesh origin (x0, y0, z0) and cell dimensions (dx, dy, dz). Thus, points data are regularly and uniformly spaced.
- RectilinearGrid is also a regularly spaced points data but spacing can be not uniform. Thus, you have to specify the nodes coordinates along the three axes, Ox, Oy & Oz.
- StructuredGrid is a not regular and not uniform points data. You have to specify the coordinates for all mesh nodes.
- UnstructuredGrid is a not regular and not uniform points data, like the StrucuturedGrid dataset, but it can hold various cell types in addition.
- PolyData is the adequate format for datasets known as "scatter", ie without any mesh. Thus, you have to specify the coordinates for all points.
"legacy" vs. "XML" format
First, the "legacy" VTK format has only one file extension for all kind of datasets format: ".vtk", which is not the case for the "XML" as you will see later.
Writing "legacy" VTK files is well documented in the VTK file formats documentation and is quite straightforward. You have just to set a few parameters, such as:
- file format (ASCII or binary);
- data format (char, int, float, double, etc...)
- specific information related to the kind of the dataset you want to use.
We won't explicate these issues here, but give only a short example in concern with a simple StructuredPoints dataset.
BTW, using python, you can write your VTK file with your own methods or use pyvtk. The two ways will be illustrated. Note: The following issue is frequently asked on the vtkusers mailing-list, so please read it carefully before complaining about errors while reading your VTK file after you wrote it.
If you want to write your data in binary format in your "legacy" VTK file (XML files are not concerned), you have to write it in BigEndian, and not in LittleEndian, even if you use an intel or amd box which are known to be in LittleEndian. So, you have to convert your data in BigEndian before writing them in your VTK file. This is explicitly mentionned in the VTK file formats documentation.