-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathe_read-file.py
More file actions
110 lines (81 loc) · 2.66 KB
/
Copy pathe_read-file.py
File metadata and controls
110 lines (81 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
.. _read_file_example:
Load and Plot from a File
~~~~~~~~~~~~~~~~~~~~~~~~~
Read a dataset from a known file type.
"""
# %%
# 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.
# sphinx_gallery_thumbnail_number = 5
import pyvista as pv
from pyvista import examples
# %%
help(pv.read)
# %%
# PyVista supports a wide variety of file formats. The supported file
# extensions are listed in an internal function:
help(pv.get_reader)
# %%
# The following code block uses a built-in example
# file, displays an airplane mesh and returns the camera's position:
# Get a sample file
filename = examples.planefile
filename
# %%
# 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:
mesh = pv.read(filename)
cpos = mesh.plot()
# %%
# The points from the mesh are directly accessible as a NumPy array:
mesh.points
# %%
# The faces from the mesh are also directly accessible as a NumPy array:
mesh.faces.reshape(-1, 4)[:, 1:] # triangular faces
# %%
# 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')``
# %%
# Example STL file:
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)
# %%
# Example OBJ file
mesh = examples.download_doorman()
mesh.plot(cpos="xy")
# %%
# Example BYU file
mesh = examples.download_teapot()
mesh.plot(cpos=[-1, 2, -5], show_edges=True)
# %%
# Example VTK file
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)
# %%
# 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
# (your code here)
# mesh = pv.read('path/to/file.vtk)
# %%
# .. raw:: html
#
# <center>
# <a target="_blank" href="https://colab.research.google.com/github/pyvista/pyvista-tutorial/blob/tutorial/notebooks/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>