vtk based on python scripts

Some tips about using vtk based on python.

When there are no requirments about the completion time, using the python binding of the vtk is usually more efficient, we do not need to write the cmake command and we can also integrate it with the numpy quickly. Actually, if you look at the blog of thr kitware, there is a trend to integrate the paraview with all kinds of python based library such as the jupetor notebook.

For most cases, we only need to execute the pip install vtk to get that depedency quickly. However, for some specific platform such as amd platform, the pip install python may not work well, we need to build the vtk manually with the option of python wrap and then update the $PYTHONPATH to add the specific python binding into the library, such as /home/ubuntu/cworkspace/install/vtk/lib/python3.10/site-packages, this should be the absolute path.

There are all kinds of examples online regarding the python script based on vtk especially for the vtk examples website, we do not cover all details and we only list several examples which are commonly used in general.

In most cases, if there are cpp code, we can transfer it to the python code.

Extracting the field

This example reads a vtk data set, extracts specific field, creates the dataset which have the same dimention with the original dataset and write the new data set out. Be careful about the ways to create a new class in python.

Transfering data fields back and forth with numpy

This example shows how to extract the field from the original vtk data set, do some operations based on numpy, sets the field back to the dataset and then write it out. Be careful about how to use the vtk.util.numpy_support to convert the array back and forth between numpy and vtk data set.

Comparision between two datasets

This operation loads two data set, and then extract the specific field and transfer them to numpy, after executing the numpy operation, it generates a new dataset with new property. This is one exmaple, be careful about the type of the field, we may use the point field or cell field as needed. This is an online example

Dividing the whole data set into multiple small datasets

Sometimes, we try to decompose the whole data set into multiple small datasets for testing the capability of program to process multiple datasets. This operations requires the voi filter. This is one associated example. One thing be careful here is that the vtk extracting voi use the extend of the data set as the input, in order to avoid the empty region between two adjacent blocks, the data block should be closed with each other. Assuming the estent is [0,1,2] for the first block, the input should be [0,1] for the second block, the input should be [1,2] bu this way, there will be not empty space between two blocks. This is an online example.

推荐文章