
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "tutorial/02_mesh/solutions/e_read-file.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_tutorial_02_mesh_solutions_e_read-file.py>`
        to download the full example code. or to run this example in your browser via Binder

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_tutorial_02_mesh_solutions_e_read-file.py:


.. _read_file_example:

Load and Plot from a File
~~~~~~~~~~~~~~~~~~~~~~~~~

Read a dataset from a known file type.

.. GENERATED FROM PYTHON SOURCE LINES 12-18

We try to make loading a mesh as easy as possible - if your data is in one
of the many supported file formats, simply use :func:`pyvista.read` to
load your spatially referenced dataset into a PyVista mesh object.

The following code block uses a built-in example file and displays an
airplane mesh.

.. GENERATED FROM PYTHON SOURCE LINES 18-22

.. code-block:: Python


    import pyvista as pv
    from pyvista import examples








.. GENERATED FROM PYTHON SOURCE LINES 24-26

.. code-block:: Python

    help(pv.read)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Help on function read in module pyvista.core.utilities.fileio:

    read(filename: 'PathStrSeq', force_ext: 'str | None' = None, file_format: 'str | None' = None, progress_bar: 'bool' = False) -> 'DataObject'
        Read any file type supported by ``vtk`` or ``meshio``.

        Automatically determines the correct reader to use then wraps the
        corresponding mesh as a pyvista object.  Attempts native ``vtk``
        readers first then tries to use ``meshio``. :py:mod:`Pickled<pickle>`
        meshes (``'.pkl'`` or ``'.pickle'``) are also supported.

        See :func:`pyvista.get_reader` for list of vtk formats supported.

        .. note::
           See https://github.com/nschloe/meshio for formats supported by
           ``meshio``. Be sure to install ``meshio`` with ``pip install
           meshio`` if you wish to use it.

        .. versionadded:: 0.45

            Support reading pickled meshes.

        .. warning::

            The pickle module is not secure. Only read pickled mesh files
            (``'.pkl'`` or ``'.pickle'``) you trust. See :py:mod:`pickle`
            for details.

        See Also
        --------
        pyvista.DataObject.save
            Save a mesh to file.

        Parameters
        ----------
        filename : str, Path, Sequence[str | Path]
            The string path to the file to read. If a list of files is
            given, a :class:`pyvista.MultiBlock` dataset 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_bar : bool, default: False
            Optionally show a progress bar. Ignored when using ``meshio``.

        Returns
        -------
        pyvista.DataSet
            Wrapped PyVista dataset.

        Examples
        --------
        Load an example mesh.

        >>> import pyvista as pv
        >>> from pyvista import examples
        >>> mesh = pv.read(examples.antfile)
        >>> mesh.plot(cpos='xz')

        Load a vtk file.

        >>> mesh = pv.read('my_mesh.vtk')  # doctest:+SKIP

        Load a meshio file.

        >>> mesh = pv.read('mesh.obj')  # doctest:+SKIP

        Load a pickled mesh file.

        >>> mesh = pv.read('mesh.pkl')  # doctest:+SKIP





.. GENERATED FROM PYTHON SOURCE LINES 27-29

PyVista supports a wide variety of file formats. The supported file
extensions are listed in an internal function:

.. GENERATED FROM PYTHON SOURCE LINES 29-32

.. code-block:: Python

    help(pv.core.utilities.reader.get_reader)






.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Help on function get_reader in module pyvista.core.utilities.reader:

    get_reader(filename, force_ext=None)
        Get a reader for fine-grained control of reading data files.

        Supported file types and Readers:

        +----------------+---------------------------------------------+
        | File Extension | Class                                       |
        +================+=============================================+
        | ``.bmp``       | :class:`pyvista.BMPReader`                  |
        +----------------+---------------------------------------------+
        | ``.cas``       | :class:`pyvista.FluentReader`               |
        +----------------+---------------------------------------------+
        | ``.case``      | :class:`pyvista.EnSightReader`              |
        +----------------+---------------------------------------------+
        | ``.cgns``      | :class:`pyvista.CGNSReader`                 |
        +----------------+---------------------------------------------+
        | ``.cube``      | :class:`pyvista.GaussianCubeReader`         |
        +----------------+---------------------------------------------+
        | ``.dat``       | :class:`pyvista.TecplotReader`              |
        +----------------+---------------------------------------------+
        | ``.dcm``       | :class:`pyvista.DICOMReader`                |
        +----------------+---------------------------------------------+
        | ``.dem``       | :class:`pyvista.DEMReader`                  |
        +----------------+---------------------------------------------+
        | ``.e``         | :class:`pyvista.ExodusIIReader`             |
        +----------------+---------------------------------------------+
        | ``.exo``       | :class:`pyvista.ExodusIIReader`             |
        +----------------+---------------------------------------------+
        | ``.exii``      | :class:`pyvista.ExodusIIReader`             |
        +----------------+---------------------------------------------+
        | ``.ex2``       | :class:`pyvista.ExodusIIReader`             |
        +----------------+---------------------------------------------+
        | ``.facet``     | :class:`pyvista.FacetReader`                |
        +----------------+---------------------------------------------+
        | ``.foam``      | :class:`pyvista.POpenFOAMReader`            |
        +----------------+---------------------------------------------+
        | ``.g``         | :class:`pyvista.BYUReader`                  |
        +----------------+---------------------------------------------+
        | ``.gif``       | :class:`pyvista.GIFReader`                  |
        +----------------+---------------------------------------------+
        | ``.glb``       | :class:`pyvista.GLTFReader`                 |
        +----------------+---------------------------------------------+
        | ``.gltf``      | :class:`pyvista.GLTFReader`                 |
        +----------------+---------------------------------------------+
        | ``.hdf``       | :class:`pyvista.HDFReader`                  |
        +----------------+---------------------------------------------+
        | ``.img``       | :class:`pyvista.DICOMReader`                |
        +----------------+---------------------------------------------+
        | ``.inp``       | :class:`pyvista.AVSucdReader`               |
        +----------------+---------------------------------------------+
        | ``.jpg``       | :class:`pyvista.JPEGReader`                 |
        +----------------+---------------------------------------------+
        | ``.jpeg``      | :class:`pyvista.JPEGReader`                 |
        +----------------+---------------------------------------------+
        | ``.hdr``       | :class:`pyvista.HDRReader`                  |
        +----------------+---------------------------------------------+
        | ``.mha``       | :class:`pyvista.MetaImageReader`            |
        +----------------+---------------------------------------------+
        | ``.mhd``       | :class:`pyvista.MetaImageReader`            |
        +----------------+---------------------------------------------+
        | ``.nek5000``   | :class:`pyvista.Nek5000Reader`              |
        +----------------+---------------------------------------------+
        | ``.nii``       | :class:`pyvista.NIFTIReader`                |
        +----------------+---------------------------------------------+
        | ``.nii.gz``    | :class:`pyvista.NIFTIReader`                |
        +----------------+---------------------------------------------+
        | ``.nhdr``      | :class:`pyvista.NRRDReader`                 |
        +----------------+---------------------------------------------+
        | ``.nrrd``      | :class:`pyvista.NRRDReader`                 |
        +----------------+---------------------------------------------+
        | ``.obj``       | :class:`pyvista.OBJReader`                  |
        +----------------+---------------------------------------------+
        | ``.p3d``       | :class:`pyvista.Plot3DMetaReader`           |
        +----------------+---------------------------------------------+
        | ``.ply``       | :class:`pyvista.PLYReader`                  |
        +----------------+---------------------------------------------+
        | ``.png``       | :class:`pyvista.PNGReader`                  |
        +----------------+---------------------------------------------+
        | ``.pnm``       | :class:`pyvista.PNMReader`                  |
        +----------------+---------------------------------------------+
        | ``.pts``       | :class:`pyvista.PTSReader`                  |
        +----------------+---------------------------------------------+
        | ``.pvd``       | :class:`pyvista.PVDReader`                  |
        +----------------+---------------------------------------------+
        | ``.pvti``      | :class:`pyvista.XMLPImageDataReader`        |
        +----------------+---------------------------------------------+
        | ``.pvtk``      | :class:`pyvista.VTKPDataSetReader`          |
        +----------------+---------------------------------------------+
        | ``.pvtr``      | :class:`pyvista.XMLPRectilinearGridReader`  |
        +----------------+---------------------------------------------+
        | ``.pvtu``      | :class:`pyvista.XMLPUnstructuredGridReader` |
        +----------------+---------------------------------------------+
        | ``.res``       | :class:`pyvista.MFIXReader`                 |
        +----------------+---------------------------------------------+
        | ``.segy``      | :class:`pyvista.SegYReader`                 |
        +----------------+---------------------------------------------+
        | ``.sgy``       | :class:`pyvista.SegYReader`                 |
        +----------------+---------------------------------------------+
        | ``.slc``       | :class:`pyvista.SLCReader`                  |
        +----------------+---------------------------------------------+
        | ``.stl``       | :class:`pyvista.STLReader`                  |
        +----------------+---------------------------------------------+
        | ``.tif``       | :class:`pyvista.TIFFReader`                 |
        +----------------+---------------------------------------------+
        | ``.tiff``      | :class:`pyvista.TIFFReader`                 |
        +----------------+---------------------------------------------+
        | ``.tri``       | :class:`pyvista.BinaryMarchingCubesReader`  |
        +----------------+---------------------------------------------+
        | ``.vrt``       | :class:`pyvista.ProStarReader`              |
        +----------------+---------------------------------------------+
        | ``.vti``       | :class:`pyvista.XMLImageDataReader`         |
        +----------------+---------------------------------------------+
        | ``.vtk``       | :class:`pyvista.VTKDataSetReader`           |
        +----------------+---------------------------------------------+
        | ``.vtkhdf``    | :class:`pyvista.HDFReader`                  |
        +----------------+---------------------------------------------+
        | ``.vtm``       | :class:`pyvista.XMLMultiBlockDataReader`    |
        +----------------+---------------------------------------------+
        | ``.vtmb``      | :class:`pyvista.XMLMultiBlockDataReader`    |
        +----------------+---------------------------------------------+
        | ``.vtp``       | :class:`pyvista.XMLPolyDataReader`          |
        +----------------+---------------------------------------------+
        | ``.vtr``       | :class:`pyvista.XMLRectilinearGridReader`   |
        +----------------+---------------------------------------------+
        | ``.vts``       | :class:`pyvista.XMLStructuredGridReader`    |
        +----------------+---------------------------------------------+
        | ``.vtu``       | :class:`pyvista.XMLUnstructuredGridReader`  |
        +----------------+---------------------------------------------+
        | ``.xdmf``      | :class:`pyvista.XdmfReader`                 |
        +----------------+---------------------------------------------+
        | ``.vtpd``      | :class:`pyvista.XMLPartitionedDataSetReader`|
        +----------------+---------------------------------------------+

        Parameters
        ----------
        filename : str, Path
            The string path to the file to read.

        force_ext : str, optional
            An extension to force a specific reader to be chosen.

        Returns
        -------
        pyvista.BaseReader
            A subclass of :class:`pyvista.BaseReader` is returned based on file type.

        Examples
        --------
        >>> import pyvista as pv
        >>> from pyvista import examples
        >>> from pathlib import Path
        >>> filename = examples.download_human(load=False)
        >>> Path(filename).name
        'Human.vtp'
        >>> reader = pv.get_reader(filename)
        >>> reader
        XMLPolyDataReader('...Human.vtp')
        >>> mesh = reader.read()
        >>> mesh
        PolyData ...
        >>> mesh.plot(color='lightblue')





.. GENERATED FROM PYTHON SOURCE LINES 33-35

The following code block uses a built-in example
file, displays an airplane mesh and returns the camera's position:

.. GENERATED FROM PYTHON SOURCE LINES 35-40

.. code-block:: Python


    # Get a sample file
    filename = examples.planefile
    filename





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    '/opt/hostedtoolcache/Python/3.12.13/x64/lib/python3.12/site-packages/pyvista/examples/airplane.ply'



.. GENERATED FROM PYTHON SOURCE LINES 41-45

Note the above filename, it's a ``.ply`` file - one of the many supported
formats in PyVista.

Use ``pv.read`` to load the file as a mesh:

.. GENERATED FROM PYTHON SOURCE LINES 45-50

.. code-block:: Python


    mesh = pv.read(filename)
    cpos = mesh.plot()









.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/solutions/images/sphx_glr_e_read-file_001.png
        :alt: e read file
        :srcset: /tutorial/02_mesh/solutions/images/sphx_glr_e_read-file_001.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/solutions/images/sphx_glr_e_read-file_001.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 51-52

The points from the mesh are directly accessible as a NumPy array:

.. GENERATED FROM PYTHON SOURCE LINES 52-55

.. code-block:: Python


    mesh.points





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    pyvista_ndarray([[896.994  ,  48.7601 ,  82.2656 ],
                     [906.593  ,  48.7601 ,  80.7452 ],
                     [907.539  ,  55.4902 ,  83.6581 ],
                     ...,
                     [806.665  , 627.363  ,   5.11482],
                     [806.665  , 654.432  ,   7.51998],
                     [806.665  , 681.537  ,   9.48744]],
                    shape=(1335, 3), dtype=float32)



.. GENERATED FROM PYTHON SOURCE LINES 56-57

The faces from the mesh are also directly accessible as a NumPy array:

.. GENERATED FROM PYTHON SOURCE LINES 57-61

.. code-block:: Python


    mesh.faces.reshape(-1, 4)[:, 1:]  # triangular faces






.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    array([[   0,    1,    2],
           [   0,    2,    3],
           [   4,    5,    1],
           ...,
           [1324, 1333, 1323],
           [1325, 1216, 1334],
           [1325, 1334, 1324]], shape=(2452, 3))



.. GENERATED FROM PYTHON SOURCE LINES 62-67

Loading other files types is just as easy! Simply pass your file path to the
:func:`pyvista.read` function and that's it!

Here are a few other examples - simply replace ``examples.download_*`` in the
examples below with ``pyvista.read('path/to/you/file.ext')``

.. GENERATED FROM PYTHON SOURCE LINES 69-70

Example STL file:

.. GENERATED FROM PYTHON SOURCE LINES 70-74

.. code-block:: Python

    mesh = examples.download_cad_model()
    cpos = [(107.0, 68.5, 204.0), (128.0, 86.5, 223.5), (0.45, 0.36, -0.8)]
    mesh.plot(cpos=cpos)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/solutions/images/sphx_glr_e_read-file_002.png
        :alt: e read file
        :srcset: /tutorial/02_mesh/solutions/images/sphx_glr_e_read-file_002.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/solutions/images/sphx_glr_e_read-file_002.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 75-76

Example OBJ file

.. GENERATED FROM PYTHON SOURCE LINES 76-80

.. code-block:: Python

    mesh = examples.download_doorman()
    mesh.plot(cpos="xy")









.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/solutions/images/sphx_glr_e_read-file_003.png
        :alt: e read file
        :srcset: /tutorial/02_mesh/solutions/images/sphx_glr_e_read-file_003.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/solutions/images/sphx_glr_e_read-file_003.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 81-82

Example BYU file

.. GENERATED FROM PYTHON SOURCE LINES 82-86

.. code-block:: Python

    mesh = examples.download_teapot()
    mesh.plot(cpos=[-1, 2, -5], show_edges=True)









.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/solutions/images/sphx_glr_e_read-file_004.png
        :alt: e read file
        :srcset: /tutorial/02_mesh/solutions/images/sphx_glr_e_read-file_004.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/solutions/images/sphx_glr_e_read-file_004.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 87-88

Example VTK file

.. GENERATED FROM PYTHON SOURCE LINES 88-93

.. code-block:: Python

    mesh = examples.download_bunny_coarse()
    cpos = [(0.2, 0.3, 0.9), (0, 0, 0), (0, 1, 0)]
    mesh.plot(cpos=cpos, show_edges=True, color=True)









.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/solutions/images/sphx_glr_e_read-file_005.png
        :alt: e read file
        :srcset: /tutorial/02_mesh/solutions/images/sphx_glr_e_read-file_005.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/solutions/images/sphx_glr_e_read-file_005.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 94-99

Exercise
^^^^^^^^
Read a file yourself with :func:`pyvista.read`. If you have a supported file
format, use that! Otherwise, download this file:
https://github.com/pyvista/pyvista-tutorial/raw/main/tutorial/02_mesh/scipy.vtk

.. GENERATED FROM PYTHON SOURCE LINES 99-103

.. code-block:: Python


    # (your code here)
    # mesh = pv.read('path/to/file.vtk)








.. GENERATED FROM PYTHON SOURCE LINES 104-111

.. raw:: html

    <center>
      <a target="_blank" href="https://colab.research.google.com/github/pyvista/pyvista-tutorial/blob/gh-pages/notebooks/tutorial/02_mesh/solutions/e_read-file.ipynb">
        <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/ width="150px">
      </a>
    </center>


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 6.545 seconds)


.. _sphx_glr_download_tutorial_02_mesh_solutions_e_read-file.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: binder-badge

      .. image:: images/binder_badge_logo.svg
        :target: https://mybinder.org/v2/gh/pyvista/pyvista-tutorial/gh-pages?urlpath=lab/tree/notebooks/tutorial/02_mesh/solutions/e_read-file.ipynb
        :alt: Launch binder
        :width: 150 px

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: e_read-file.ipynb <e_read-file.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: e_read-file.py <e_read-file.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: e_read-file.zip <e_read-file.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
