
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "tutorial/02_mesh/exercises/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_exercises_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_exercises_e_read-file.py:


.. _read_file_exercise:

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)


.. GENERATED FROM PYTHON SOURCE LINES 27-31

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

See https://docs.pyvista.org/api/readers/_autosummary/pyvista.get_reader#pyvista.get_reader

.. GENERATED FROM PYTHON SOURCE LINES 31-34

.. code-block:: Python

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



.. GENERATED FROM PYTHON SOURCE LINES 35-37

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 37-42

.. code-block:: Python


    # Get a sample file
    filename = examples.planefile
    filename


.. GENERATED FROM PYTHON SOURCE LINES 43-47

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 47-52

.. code-block:: Python


    mesh = ...
    cpos = mesh.plot()



.. GENERATED FROM PYTHON SOURCE LINES 53-54

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

.. GENERATED FROM PYTHON SOURCE LINES 54-57

.. code-block:: Python


    mesh.points


.. GENERATED FROM PYTHON SOURCE LINES 58-59

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

.. GENERATED FROM PYTHON SOURCE LINES 59-63

.. code-block:: Python


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



.. GENERATED FROM PYTHON SOURCE LINES 64-72

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')``

We have a gallery of the example datasets available here:
https://docs.pyvista.org/api/examples/dataset_gallery

.. GENERATED FROM PYTHON SOURCE LINES 74-75

Example STL file:

.. GENERATED FROM PYTHON SOURCE LINES 75-79

.. 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)


.. GENERATED FROM PYTHON SOURCE LINES 80-81

Example OBJ file

.. GENERATED FROM PYTHON SOURCE LINES 81-85

.. code-block:: Python

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



.. GENERATED FROM PYTHON SOURCE LINES 86-87

Example BYU file

.. GENERATED FROM PYTHON SOURCE LINES 87-91

.. code-block:: Python

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



.. GENERATED FROM PYTHON SOURCE LINES 92-93

Example VTK file

.. GENERATED FROM PYTHON SOURCE LINES 93-98

.. 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)



.. GENERATED FROM PYTHON SOURCE LINES 99-104

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 104-108

.. code-block:: Python


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


.. GENERATED FROM PYTHON SOURCE LINES 109-116

.. raw:: html

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


.. _sphx_glr_download_tutorial_02_mesh_exercises_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/exercises/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>`_
