注釈
Go to the end to download the full example code. or to run this example in your browser via Binder
ファイルからロードおよびプロットする#
既知のファイルタイプからデータセットを読み取ります.
データがサポートされている多くのファイルフォーマットのうちの1つであれば, pyvista.read()
を使って,空間参照されたデータセットを PyVista メッシュオブジェクトに読み込むだけで,メッシュをできるだけ簡単にロードできるようにします.
次のコードブロックでは,組み込みのサンプルファイルを使用して,飛行機のメッシュを表示します.
import pyvista as pv
from pyvista import examples
help(pv.read)
Help on function read in module pyvista.core.utilities.fileio:
read(filename, force_ext=None, file_format=None, progress_bar=False)
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``.
See :func:`pyvista.get_reader` for list of 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.
Parameters
----------
filename : 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
PyVistaは様々なファイル形式をサポートしています.サポートされているファイル拡張子は,内部関数にリストアップされています.
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` |
+----------------+---------------------------------------------+
| ``.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` |
+----------------+---------------------------------------------+
| ``.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` |
+----------------+---------------------------------------------+
| ``.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
>>> filename = examples.download_human(load=False)
>>> filename.split("/")[-1] # omit the path
'Human.vtp'
>>> reader = pv.get_reader(filename)
>>> reader
XMLPolyDataReader('.../Human.vtp')
>>> mesh = reader.read()
>>> mesh
PolyData ...
>>> mesh.plot(color='lightblue')
次のコードブロックでは,組み込みのサンプルファイルを使用して,飛行機のメッシュを表示し,カメラの位置を取得します.
# Get a sample file
filename = examples.planefile
filename
'/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/pyvista/examples/airplane.ply'
上記のファイル名に注意してください.これは .ply
ファイルで,PyVistaでサポートされている多くのフォーマットの1つです.
ファイルをメッシュとして読み込むには, pv.read
を使用します.
メッシュからの点は,NumPyの配列として直接アクセスできます.
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)
また,メッシュからの面はNumPyの配列として直接アクセスすることができます.
mesh.faces.reshape(-1, 4)[:, 1:] # triangular faces
array([[ 0, 1, 2],
[ 0, 2, 3],
[ 4, 5, 1],
...,
[1324, 1333, 1323],
[1325, 1216, 1334],
[1325, 1334, 1324]], shape=(2452, 3))
他のファイルタイプのロードも同じくらい簡単です.ファイルパスを pyvista.read()
関数に渡すだけです.
以下の例の examples.download_*
を pyvista.read('path/to/you/file.ext')
に置き換えるだけで,他のいくつかの例があります.
STLファイルの例:
OBJファイルの例:
BYUファイルの例:
VTKファイルの例:
演習#
pyvista.read()
を使って,自分でファイルを読み込んでください.サポートされているファイル形式があれば,それを使ってください.そうでない場合は,このファイルをダウンロードしてください: pyvista/pyvista-tutorial
# (your code here)
# mesh = pv.read('path/to/file.vtk)
Total running time of the script: (0 minutes 4.302 seconds)