pyvista.PolyData.save#
- PolyData.save(filename, binary=True, texture=None, recompute_normals=True)[source]#
Write a surface mesh to disk.
Written file may be an ASCII or binary ply, stl, or vtk mesh file.
- Parameters:
- filename
str
,Path
Filename of mesh to be written. File type is inferred from the extension of the filename unless overridden with ftype. Can be one of many of the supported the following types (
'.ply'
,'.vtp'
,'.stl'
,'.vtk
,'.geo'
,'.obj'
,'.iv'
).- binarybool, default:
True
Writes the file as binary when
True
and ASCII whenFalse
.- texture
str
,numpy.ndarray
,optional
Write a single texture array to file when using a PLY file. Texture array must be a 3 or 4 component array with the datatype
np.uint8
. Array may be a cell array or a point array, and may also be a string if the array already exists in the PolyData.If a string is provided, the texture array will be saved to disk as that name. If an array is provided, the texture array will be saved as
'RGBA'
if the array contains an alpha channel (i.e. 4 component array), or as'RGB'
if the array is just a 3 component array.Note
This feature is only available when saving PLY files.
- recompute_normalsbool, default:
True
When
True
, if ply or stl format is chosen, the face normals are computed in place to ensure the mesh is properly saved. Set this toFalse
to save instead the already existing normal array in the PolyData.
- filename
Notes
Binary files write much faster than ASCII and have a smaller file size.
Examples
Save a mesh as a STL.
>>> import pyvista as pv >>> sphere = pv.Sphere() >>> sphere.save('my_mesh.stl')
Save a mesh as a PLY.
>>> sphere = pv.Sphere() >>> sphere.save('my_mesh.ply')
Save a mesh as a PLY with a texture array. Here we also create a simple RGB array representing the texture.
>>> import numpy as np >>> sphere = pv.Sphere() >>> texture = np.zeros((sphere.n_points, 3), np.uint8) >>> # Just the green channel is set as a repeatedly >>> # decreasing value >>> texture[:, 1] = np.arange(sphere.n_points)[::-1] >>> sphere.point_data['my_texture'] = texture >>> sphere.save( ... 'my_mesh.ply', texture='my_texture' ... )
Alternatively, provide just the texture array. This will be written to the file as
'RGB'
since it does not contain an alpha channel.>>> sphere.save('my_mesh.ply', texture=texture)
Save a mesh as a VTK file.
>>> sphere = pv.Sphere() >>> sphere.save('my_mesh.vtk')