PyVista: Update PolyData with additional points

1.4k views Asked by At

I would like to add points (and cells) to the pyvista.PolyData in the plotter. I tried using the Plotter.update_coordinates function however this is only useful for point data with equal size:

import numpy as np
import pyvista as pv
import pyvistaqt

points = np.array([[1, 0, 0],
                   [2, 0, 0],
                   [3, 0, 0]])


point_cloud = pv.PolyData(points)

plotter = pyvistaqt.BackgroundPlotter()
a = plotter.add_points(point_cloud)
plotter.show()

new_points = np.array([[1, 0, 0],
                       [2, 0, 0],
                       [5, 0, 0], 
                       [7, 0, 0]])  # Not updated in the plotter

plotter.update_coordinates(new_points, point_cloud, render=True)

It seems the points are updated, however not the cells. Hence, only the corresponding cells are modified in the plotter.

What is the best practice to update the PolyData, which includes new (additional) points?

1

There are 1 answers

1
Andras Deak -- Слава Україні On BEST ANSWER

You said

I would like to add points (and cells) to the pyvista.PolyData in the plotter

But this is not clear to me. You have two options: either add further points to a PolyData and then plot that one mesh, or you can add two different PolyData objects to the same plotter.

Here are the two options:

import pyvista as pv

sphere_a = pv.Sphere()
sphere_b = sphere_a.translate((1, 0, 0), inplace=False)

# option 1: merge the PolyData
spheres = sphere_a + sphere_b
# alternatively: sphere_a += sphere_b for an in-place operation
spheres.plot()  # plot the two spheres in one PolyData
# we could also use a Plotter for this

# option 2: add both PolyData to the same plotter
plotter = pv.Plotter()
plotter.add_mesh(sphere_a)  # or add_points() for a point cloud
plotter.add_mesh(sphere_b)
plotter.show()  # show the two spheres from two PolyData

And as a side note, if you only have a point cloud (i.e. no cells) you should consider using the new PointSet class. This needs PyVista 0.34.0 and VTK 9.1.0 or higher.