read#
- read(
- filename: PathStrSeq,
- force_ext: str | None = None,
- file_format: str | None = None,
- progress_bar: bool = False,
- *,
- cls: type[DataObject] | None = None,
- validate: bool | None = None,
Read any file type supported by
vtkormeshio.Automatically determines the correct reader to use then wraps the corresponding mesh as a pyvista object. Attempts native
vtkreaders first then tries to usemeshio.Pickledmeshes ('.pkl'or'.pickle') are also supported.Remote URIs (
https://,s3://, etc.) are downloaded to a temporary file automatically. Installfsspecfor full protocol support (pip install pyvista[io]);poochis used as a fallback for HTTP(S). Third-party reader plugins registered viapyvista.register_reader()are also checked.See
pyvista.get_reader()for list of vtk formats supported.Tip
A native
.pvbinary format withzstdcompression is available via the pyvista-zstd companion package, included in theioextra (pip install pyvista[io]). It is a compact, multi-threaded alternative to the built-in VTK formats below when file size or I/O latency matters. Third-party packages can register additional custom writers viapyvista.register_writer().ImageDataFile FormatsFile Format
File Extension(s)
BMP
.bmpDEM
.demDICOM
.dcm,.imgGaussianCube
.cubeGESigna
.mrGIF
.gifHDF
.hdf,.vtkhdfHDR
.hdrJPEG
.jpeg,.jpgMeta
.mha,.mhdMINC
.mncNIFTI
.nii,.nii.gzNRRD
.nhdr,.nrrdPNG
.pngPNM
.pnmSegY
.segy,.sgySeries
.seriesSLC
.slcTIFF
.tif,.tiffVTK
.vtkVTKP
.pvtkXML
.vtiXMLP
.pvtiRectilinearGridFile FormatsStructuredGridFile FormatsPolyDataFile FormatsUnstructuredGridFile FormatsMultiBlockFile FormatsPartitionedDataSetFile FormatsNote
See nschloe/meshio for formats supported by
meshio. Be sure to installmeshiowithpip install meshioif you wish to use it.Added in version 0.45: Support reading pickled meshes.
Warning
The pickle module is not secure. Only read pickled mesh files (
'.pkl'or'.pickle') you trust. Seepicklefor details.- Parameters:
- filename
str,Path,Sequence[str|Path] The string path to the file to read. If a list of files is given, a
pyvista.MultiBlockdataset is returned with each file being a separate block in the dataset.- force_ext
str,optional If specified, the reader will be chosen by an extension which is different to its actual extension. For example,
'.vts','.vtu'.- file_format
str,optional Format of file to read with meshio.
- progress_barbool, default:
False Optionally show a progress bar. Ignored when using
meshio.- cls
type,optional Expected concrete type of the returned mesh. When given, the result is checked with
isinstance()and aTypeErroris raised on mismatch. Static type checkers (mypy,pyright) use this to narrow the return type toclsdirectly, so callers do not needtyping.castor a manualassert isinstanceto access subclass-specific attributes, e.g.pv.read('file.vtu', cls=pv.UnstructuredGrid).- validatebool,
optional Forwarded to
pyvista.wrap()as thevalidatekeyword when using avtkreader. WhenNone(the default), honorspyvista.core.config.Config.validate_on_wrap. PassFalseto skip the cheap array-length sanity check on very large trusted files. Has no effect formeshioor pickle code paths.Added in version 0.48.
- filename
- Returns:
pyvista.DataSet|pyvista.MultiBlockWrapped PyVista dataset. When
clsis given, an instance ofclsis returned instead.
See also
pyvista.DataObject.saveSave a mesh to file.
Examples
Load an example mesh.
>>> import pyvista as pv >>> from pyvista import examples >>> mesh = pv.read(examples.antfile) >>> mesh.plot(cpos='xz')
Narrow the return type to a specific class. This avoids the need for a manual
castwhen working with type checkers such asmypyorpyright.>>> mesh = pv.read('mesh.vtu', cls=pv.UnstructuredGrid)
Load a vtk file.
>>> mesh = pv.read('my_mesh.vtk')
Load a meshio file.
>>> mesh = pv.read('mesh.obj')
Load a pickled mesh file.
>>> mesh = pv.read('mesh.pkl')