Point Data¶
The pyvista.PolyData
object adds additional functionality to the
vtk.vtkPolyData
object, to include direct array access through NumPy,
one line plotting, and other mesh functions.
PolyData Creation¶
See Create PolyData for an example on creating a pyvista.PolyData
object
from NumPy arrays.
Empty Object¶
A polydata object can be initialized with:
import pyvista
grid = pyvista.PolyData()
This creates an empty grid, and is not useful until points and cells are added
to it. VTK points and cells can be added with SetPoints
and SetCells
,
but the inputs to these need to be vtk.vtkCellArray
and vtk.vtkPoints
objects, which need to be populated with values.
Grid creation is simplified by initializing the grid directly from NumPy
arrays as in the following section.
Initialize from a File¶
Both binary and ASCII .ply, .stl, and .vtk files can be read using PyVista. For example, the PyVista package contains example meshes and these can be loaded with:
import pyvista
from pyvista import examples
# Load mesh
mesh = pyvista.PolyData(examples.planefile)
This mesh can then be written to a vtk file using:
mesh.save('plane.vtk')
These meshes are identical.
import numpy as np
mesh_from_vtk = pyvista.PolyData('plane.vtk')
print(np.allclose(mesh_from_vtk.points, mesh.points))
Mesh Manipulation and Plotting¶
Meshes can be directly manipulated using NumPy or with the builtin translation and rotation routines. This example loads two meshes and moves, scales, and copies them.
import pyvista
from pyvista import examples
# load and shrink airplane
airplane = pyvista.PolyData(examples.planefile)
airplane.points /= 10 # shrink by 10x
# rotate and translate ant so it is on the plane
ant = pyvista.PolyData(examples.antfile)
ant.rotate_x(90)
ant.translate([90, 60, 15])
# Make a copy and add another ant
ant_copy = ant.copy()
ant_copy.translate([30, 0, 10])
To plot more than one mesh a plotting class must be created to manage the plotting. The following code creates the class and plots the meshes with various colors.
# Create plotting object
plotter = pyvista.Plotter()
plotter.add_mesh(ant, 'r')
plotter.add_mesh(ant_copy, 'b')
# Add airplane mesh and make the color equal to the Y position. Add a
# scalar bar associated with this mesh
plane_scalars = airplane.points[:, 1]
plotter.add_mesh(airplane, scalars=plane_scalars, stitle='Airplane Y\nLocation')
# Add annotation text
plotter.add_text('Ants and Plane Example')
plotter.show(screenshot='AntsAndPlane.png')
pyvista.PolyData Grid Class Methods¶
The following is a description of the methods available to a pyvista.PolyData
object. It inherits all methods from the original vtk object,
vtk.vtkPolyData.
Attributes
Return the mesh surface area. 

Return the cell normals. 

Return the cell normals. 

Return a pointer to the points as a numpy object. 

Return a pointer to the lines as a numpy object. 

Return the number of cells. 

Return the number of open edges on this mesh. 

Return the number of cells. 

Return the obbTree of the polydata. 

Return the point normals. 

Get the vertex cells. 

Return the mesh volume. 
Methods
Return True if all the faces of the polydata are triangles. 


Write a surface mesh to disk. 

class
pyvista.
PolyData
(*args, **kwargs)¶ Bases:
vtkCommonDataModelPython.vtkPolyData
,pyvista.core.pointset.PointSet
,pyvista.core.filters.PolyDataFilters
Extend the functionality of a vtk.vtkPolyData object.
Can be initialized in several ways:
Create an empty mesh
Initialize from a vtk.vtkPolyData
Using vertices
Using vertices and faces
From a file
Examples
>>> import pyvista >>> from pyvista import examples >>> import vtk >>> import numpy as np
>>> surf = pyvista.PolyData() # Create an empty mesh
>>> # Initialize from a vtk.vtkPolyData object >>> vtkobj = vtk.vtkPolyData() >>> surf = pyvista.PolyData(vtkobj)
>>> # initialize from just vertices >>> vertices = np.array([[0, 0, 0], [1, 0, 0], [1, 0.5, 0], [0, 0.5, 0],]) >>> surf = pyvista.PolyData(vertices)
>>> # initialize from vertices and faces >>> faces = np.hstack([[3, 0, 1, 2], [3, 0, 3, 2]]).astype(np.int8) >>> surf = pyvista.PolyData(vertices, faces)
>>> # initialize from a filename >>> surf = pyvista.PolyData(examples.antfile)

property
area
¶ Return the mesh surface area.
 Returns
area – Total area of the mesh.
 Return type
float

property
cell_normals
¶ Return the cell normals.

property
face_normals
¶ Return the cell normals.

property
faces
¶ Return a pointer to the points as a numpy object.

is_all_triangles
()¶ Return True if all the faces of the polydata are triangles.

property
lines
¶ Return a pointer to the lines as a numpy object.

property
n_faces
¶ Return the number of cells.
Alias for
n_cells
.

property
n_open_edges
¶ Return the number of open edges on this mesh.

property
number_of_faces
¶ Return the number of cells.

property
obbTree
¶ Return the obbTree of the polydata.
An obbTree is an object to generate oriented bounding box (OBB) trees. An oriented bounding box is a bounding box that does not necessarily line up along coordinate axes. The OBB tree is a hierarchical tree structure of such boxes, where deeper levels of OBB confine smaller regions of space.

property
point_normals
¶ Return the point normals.

save
(filename, binary=True)¶ Write a surface mesh to disk.
Written file may be an ASCII or binary ply, stl, or vtk mesh file. If ply or stl format is chosen, the face normals are computed in place to ensure the mesh is properly saved.
 Parameters
filename (str) – Filename of mesh to be written. File type is inferred from the extension of the filename unless overridden with ftype. Can be one of the following types (.ply, .stl, .vtk)
binary (bool, optional) – Writes the file as binary when True and ASCII when False.
Notes
 Binary files write much faster than ASCII and have a smaller
file size.

property
verts
¶ Get the vertex cells.

property
volume
¶ Return the mesh volume.
This will throw a VTK error/warning if not a closed surface
 Returns
volume – Total volume of the mesh.
 Return type
float