pythonpyvista

Add point data to mesh and save as vtu


I am trying to add more properties to a *vtu file using pyvista. I have written a python code which generates 9 columns of data for all the points in the mesh. I want to add them to the existing *vtu file. I found add_field_data, but I want to add as point data, not field data or cell data.

I also considered converting the point data in the original *vtu file to a numpy array and concatenating them, but I do not know how to save this numpy array as a *vtu file.

Any help in any of the 2 directions is highly appreciated, although I would prefer the former.

Thanks in advance,


Solution

  • It's not clear to me why we have add_field_data() and no add_point_data() or add_cell_data(), but you can always use the point_data/cell_data/field_data attributes of mesh types, see for instance this tutorial page.

    If you already have a mesh and a 1d array of scalars scalars of shape (mesh.n_points,) (or a 2d array with shape (mesh.n_points, d), actually) then all you need to do with your mesh is

    mesh.point_data['name of your point data comes here'] = scalars
    

    You'll get an error in case the shape of scalars is invalid. You can then just

    1. read the .vtu file with something like mesh = pv.read('name_of_your_file.vtu')
    2. add the scalars as above
    3. call mesh.plot() or something more complicated to make sure the scalars look the way you expect them to (optional but recommended)
    4. call mesh.save('output_file_name.vtu')

    If you plot your mesh with your 9-component scalars of shape (n_points, 9) you'll probably want to choose a single component (a single column) to plot by. You can do this with

    mesh.plot(scalars='name of your point data comes here', component=3)
    

    to pick out column index 3.