CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyvista

Easier Pythonic interface to VTK for 3D scientific data visualization and mesh analysis

Pending
Overview
Eval results
Files

file-io.mddocs/

File Input/Output

PyVista provides comprehensive file I/O capabilities supporting over 20 file formats commonly used in scientific computing, CAD, and 3D graphics, with seamless integration to external mesh processing libraries.

Capabilities

Universal File Reading

Main Read Function

Universal file reader with automatic format detection.

def read(filename, force_ext=None, file_format=None, progress_bar=False, **kwargs):
    """
    Read any supported file format into appropriate PyVista dataset.
    
    Parameters:
        filename (str): Path to file to read
        force_ext (str): Force specific file extension for format detection
        file_format (str): Explicitly specify file format
        progress_bar (bool): Show progress bar for large files
        **kwargs: Reader-specific parameters
        
    Returns:
        DataSet: Appropriate PyVista dataset (PolyData, UnstructuredGrid, etc.)
    """

def get_reader(filename):
    """
    Get appropriate reader class for file format.
    
    Parameters:
        filename (str): Path to file
        
    Returns:
        BaseReader: Reader class instance
    """

VTK Format Support

Native VTK Files

Read VTK's native file formats with full feature support.

class VTKDataSetReader:
    """Reader for VTK legacy format files (.vtk)."""
    
    def __init__(self, filename):
        """Initialize reader with filename."""
    
    def read(self) -> 'DataSet':
        """Read file and return dataset."""

class XMLPolyDataReader:
    """Reader for VTK XML PolyData files (.vtp)."""
    
    def __init__(self, filename):
        """Initialize reader with filename."""
    
    def read(self) -> PolyData:
        """Read file and return PolyData."""

class XMLUnstructuredGridReader:
    """Reader for VTK XML UnstructuredGrid files (.vtu)."""
    
    def __init__(self, filename):
        """Initialize reader with filename."""
    
    def read(self) -> UnstructuredGrid:
        """Read file and return UnstructuredGrid."""

class XMLImageDataReader:
    """Reader for VTK XML ImageData files (.vti)."""
    
    def __init__(self, filename):
        """Initialize reader with filename."""
    
    def read(self) -> ImageData:
        """Read file and return ImageData."""

Common 3D Formats

STL Files

Support for STL (STereoLithography) format widely used in 3D printing and CAD.

class STLReader:
    """
    Reader for STL files (.stl).
    
    Supports both ASCII and binary STL formats.
    """
    
    def __init__(self, filename):
        """Initialize STL reader."""
    
    def read(self) -> PolyData:
        """Read STL file and return PolyData mesh."""

PLY Files

Support for PLY (Stanford Triangle Format) files.

class PLYReader:
    """
    Reader for PLY files (.ply).
    
    Supports both ASCII and binary PLY formats with custom properties.
    """
    
    def __init__(self, filename):
        """Initialize PLY reader."""
    
    def read(self) -> PolyData:
        """Read PLY file and return PolyData mesh."""

OBJ Files

Support for Wavefront OBJ format.

class OBJReader:
    """
    Reader for Wavefront OBJ files (.obj).
    
    Supports geometry, materials, and texture coordinates.
    """
    
    def __init__(self, filename):
        """Initialize OBJ reader."""
    
    def read(self) -> PolyData:
        """Read OBJ file and return PolyData mesh."""

Scientific and Engineering Formats

Exodus II Files

Support for Exodus II format used in finite element analysis.

class ExodusIIReader:
    """
    Reader for Exodus II files (.e, .exo, .g).
    
    Supports finite element meshes with time-varying data.
    """
    
    def __init__(self, filename):
        """Initialize Exodus reader."""
    
    def read(self) -> MultiBlock:
        """Read Exodus file and return MultiBlock dataset."""
    
    def set_object_status(self, object_type, object_id, status):
        """
        Set status for reading specific objects.
        
        Parameters:
            object_type (str): Type of object ('ELEM_BLOCK', 'NODE_SET', etc.)
            object_id (int): Object ID
            status (bool): Whether to read object
        """
    
    def get_times(self) -> list:
        """Get available time steps."""
    
    def set_active_time_point(self, time_point):
        """
        Set active time step.
        
        Parameters:
            time_point (int): Time step index
        """

def read_exodus(filename, **kwargs) -> MultiBlock:
    """
    Convenience function to read Exodus files.
    
    Parameters:
        filename (str): Path to Exodus file
        **kwargs: Reader options
        
    Returns:
        MultiBlock: Exodus dataset
    """

OpenFOAM Files

Support for OpenFOAM computational fluid dynamics format.

class OpenFOAMReader:
    """
    Reader for OpenFOAM case files.
    
    Reads OpenFOAM simulation results including mesh and field data.
    """
    
    def __init__(self, filename):
        """Initialize OpenFOAM reader."""
    
    def read(self) -> MultiBlock:
        """Read OpenFOAM case and return MultiBlock dataset."""
    
    def set_time_value(self, time_value):
        """
        Set time step to read.
        
        Parameters:
            time_value (float): Time value
        """

Image and Volume Formats

DICOM Medical Images

Support for medical imaging DICOM format.

class DICOMReader:
    """
    Reader for DICOM medical image files.
    
    Supports single files and DICOM series.
    """
    
    def __init__(self, filename):
        """Initialize DICOM reader."""
    
    def read(self) -> ImageData:
        """Read DICOM data and return ImageData volume."""

Common Image Formats

Support for standard image formats as textures or height maps.

class PNGReader:
    """Reader for PNG image files."""
    
    def read(self) -> ImageData:
        """Read PNG as ImageData."""

class JPEGReader:
    """Reader for JPEG image files."""
    
    def read(self) -> ImageData:
        """Read JPEG as ImageData."""

class TIFFReader:
    """Reader for TIFF image files."""
    
    def read(self) -> ImageData:
        """Read TIFF as ImageData."""

def read_texture(filename) -> ImageData:
    """
    Read image file as texture data.
    
    Parameters:
        filename (str): Path to image file
        
    Returns:
        ImageData: Image as volume data
    """

MeshIO Integration

External Library Integration

Integration with MeshIO library for additional format support.

def read_meshio(filename, **kwargs):
    """
    Read file using MeshIO library.
    
    Supports additional formats not natively supported by VTK.
    
    Parameters:
        filename (str): Path to file
        **kwargs: MeshIO reader options
        
    Returns:
        DataSet: Converted PyVista dataset
    """

def from_meshio(mesh):
    """
    Convert MeshIO mesh to PyVista dataset.
    
    Parameters:
        mesh (meshio.Mesh): MeshIO mesh object
        
    Returns:
        DataSet: PyVista dataset
    """

def to_meshio(mesh):
    """
    Convert PyVista dataset to MeshIO mesh.
    
    Parameters:
        mesh (DataSet): PyVista dataset
        
    Returns:
        meshio.Mesh: MeshIO mesh object
    """

def save_meshio(mesh, filename, **kwargs):
    """
    Save PyVista dataset using MeshIO.
    
    Parameters:
        mesh (DataSet): PyVista dataset to save
        filename (str): Output filename
        **kwargs: MeshIO writer options
    """

File Writing

Universal Save Methods

Save datasets in appropriate formats.

def save(self, filename, binary=True, texture=None):
    """
    Save dataset to file with automatic format detection.
    
    Parameters:
        filename (str): Output filename with extension
        binary (bool): Use binary format when available
        texture (Texture): Optional texture to save with mesh
    """

Format-Specific Writers

Specialized writers for different formats.

def save_vtk(mesh, filename, binary=True):
    """
    Save as VTK legacy format (.vtk).
    
    Parameters:
        mesh (DataSet): Dataset to save
        filename (str): Output filename
        binary (bool): Use binary format
    """

def save_stl(mesh, filename, binary=True):
    """
    Save as STL format (.stl).
    
    Parameters:
        mesh (PolyData): PolyData mesh to save
        filename (str): Output filename
        binary (bool): Use binary STL format
    """

def save_ply(mesh, filename, binary=True, texture_coordinates=None):
    """
    Save as PLY format (.ply).
    
    Parameters:
        mesh (PolyData): PolyData mesh to save
        filename (str): Output filename
        binary (bool): Use binary PLY format
        texture_coordinates (array): UV texture coordinates
    """

Serialization

Pickle Support

Native Python serialization support.

def save_pickle(mesh, filename):
    """
    Save dataset using Python pickle.
    
    Parameters:
        mesh (DataSet): Dataset to pickle
        filename (str): Output filename
    """

def read_pickle(filename):
    """
    Read pickled dataset.
    
    Parameters:
        filename (str): Pickle file to read
        
    Returns:
        DataSet: Unpickled dataset
    """

def set_pickle_format(file_format):
    """
    Set default pickle format for datasets.
    
    Parameters:
        file_format (str): Format ('vtk', 'xml', 'legacy')
    """

Advanced File Operations

Multi-file and Time Series

Handle complex file organizations.

class PVDReader:
    """
    Reader for ParaView Data files (.pvd).
    
    Handles time series and multi-file datasets.
    """
    
    def __init__(self, filename):
        """Initialize PVD reader."""
    
    def read(self) -> MultiBlock:
        """Read PVD file series."""
    
    def get_time_values(self) -> list:
        """Get available time values."""
    
    def set_active_time_value(self, time_value):
        """Set active time step."""

class TimeReader:
    """
    Generic time series reader wrapper.
    
    Provides unified interface for time-varying datasets.
    """
    
    def __init__(self, reader):
        """Initialize with base reader."""
    
    def update_time_step(self, time_step):
        """Update to specific time step."""
    
    def get_time_range(self) -> tuple:
        """Get time range (min, max)."""

Compression and Optimization

Options for file size optimization.

def set_vtkwriter_mode(mode):
    """
    Set VTK writer compression mode.
    
    Parameters:
        mode (int): Compression level (0=off, 1=on, 2=appended)
    """

Usage Examples

Basic file I/O

import pyvista as pv

# Read various file formats
mesh_vtk = pv.read('data.vtk')        # VTK format
mesh_stl = pv.read('model.stl')       # STL format  
mesh_ply = pv.read('scan.ply')        # PLY format
mesh_obj = pv.read('object.obj')      # OBJ format

# Automatic format detection
mesh = pv.read('unknown_format.mesh')

# Save in different formats
mesh.save('output.vtk')     # VTK legacy
mesh.save('output.vtp')     # VTK XML PolyData
mesh.save('output.stl')     # STL
mesh.save('output.ply')     # PLY

Working with scientific data

import pyvista as pv

# Read Exodus finite element data
exodus_data = pv.read_exodus('simulation.e')
print(f"Time steps: {exodus_data.get_time_values()}")

# Read specific time step
reader = pv.ExodusIIReader('simulation.e')
reader.set_active_time_point(10)
data = reader.read()

# Read OpenFOAM case
foam_data = pv.read('case.foam')

# Read medical DICOM images
dicom_volume = pv.read('brain_scan.dcm')

MeshIO integration

import pyvista as pv
import meshio

# Read format not natively supported by VTK
mesh_gmsh = pv.read_meshio('model.msh')  # Gmsh format

# Convert between PyVista and MeshIO
pv_mesh = pv.Sphere()
meshio_mesh = pv.to_meshio(pv_mesh)

# Save using MeshIO for additional formats
pv.save_meshio(pv_mesh, 'output.off')  # OFF format
pv.save_meshio(pv_mesh, 'output.mesh') # Medit format

Time series data

import pyvista as pv

# Read time series data
reader = pv.PVDReader('time_series.pvd')
time_values = reader.get_time_values()

# Process each time step
results = []
for time_val in time_values:
    reader.set_active_time_value(time_val)
    data = reader.read()
    # Process data for this time step
    results.append(data.compute_derivative())

# Create animation from time series
plotter = pv.Plotter()
plotter.open_gif('animation.gif')

for result in results:
    plotter.clear()
    plotter.add_mesh(result, scalars='derivative')
    plotter.write_frame()

plotter.close()

Install with Tessl CLI

npx tessl i tessl/pypi-pyvista

docs

data-processing.md

data-structures.md

examples.md

file-io.md

geometric-primitives.md

index.md

plotting.md

tile.json