{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Load and Plot from a File {#read_file_example}\n\nRead a dataset from a known file type.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Loading a mesh is trivial - if your data is in one of the many supported\nfile formats, simply use `pyvista.read`{.interpreted-text role=\"func\"}\nto load your spatially referenced dataset into a PyVista mesh object.\n\nThe following code block uses a built-in example file and displays an\nairplane mesh.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pyvista as pv\nfrom pyvista import examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following code block uses a built-in example file, displays an\nairplane mesh and returns the camera\\'s position:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Get a sample file\nfilename = examples.planefile\nfilename" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note the above filename, it\\'s a `.ply` file - one of the many supported\nformats in PyVista.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh = pv.read(filename)\ncpos = mesh.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also take a screenshot without creating an interactive plot\nwindow using the `Plotter`:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plotter = pv.Plotter(off_screen=True)\nplotter.add_mesh(mesh)\nplotter.show(screenshot=\"myscreenshot.png\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The points from the mesh are directly accessible as a NumPy array:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh.points" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The faces from the mesh are also directly accessible as a NumPy array:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh.faces.reshape(-1, 4)[:, 1:] # triangular faces" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Loading other files types is just as easy. Simply pass your file path to\nthe `pyvista.read`{.interpreted-text role=\"func\"} function and that\\'s\nit.\n\nHere are a few other examples - simply replace `examples.download_*` in\nthe examples below with `pyvista.read('path/to/you/file.ext')`\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Example STL file:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh = examples.download_cad_model()\ncpos = [(107.0, 68.5, 204.0), (128.0, 86.5, 223.5), (0.45, 0.36, -0.8)]\nmesh.plot(cpos=cpos)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Example OBJ file\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh = examples.download_doorman()\nmesh.plot(cpos=\"xy\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Example BYU file\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh = examples.download_teapot()\nmesh.plot(cpos=[-1, 2, -5], show_edges=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Example VTK file\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh = examples.download_bunny_coarse()\ncpos = [(0.2, 0.3, 0.9), (0, 0, 0), (0, 1, 0)]\nmesh.plot(cpos=cpos, show_edges=True, color=True)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }