.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorial/06_vtk/a_2_pyvista_vtk.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` 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_06_vtk_a_2_pyvista_vtk.py: PyVista and VTK Together ~~~~~~~~~~~~~~~~~~~~~~~~ PyVista is best known for is easy to use plotting API -- being familiar to most Python users already experienced with libraries like Matplotlib. Many people benefit from combining the power of VTK's Python bindings for their data pipelines and the flexibility and simplicity of PyVista for 3D rendering. The following section demonstrates this usage scenario. .. tip:: In case it was not made clear in the :ref:`mesh` section, PyVista mesh classes are subclasses of their VTK counterparts - which means PyVista can be intermixed with VTK workflows. Nothing stops you from using VTK classes and then wrapping the output with PyVista for streamlined plotting. For example: .. _vtkDataArray: https://vtk.org/doc/nightly/html/classvtkDataArray.html .. _vtkPolyData: https://vtk.org/doc/nightly/html/classvtkPolyData.html .. _vtkImageData: https://vtk.org/doc/nightly/html/classvtkImageData.html .. _vtkpoints: https://vtk.org/doc/nightly/html/classvtkPoints.html .. GENERATED FROM PYTHON SOURCE LINES 20-25 .. code-block:: Python import pyvista as pv from pyvista import examples import vtk .. GENERATED FROM PYTHON SOURCE LINES 26-27 Create a circle using vtk .. GENERATED FROM PYTHON SOURCE LINES 27-34 .. code-block:: Python polygonSource = vtk.vtkRegularPolygonSource() polygonSource.GeneratePolygonOff() polygonSource.SetNumberOfSides(50) polygonSource.SetRadius(5.0) polygonSource.SetCenter(0.0, 0.0, 0.0) polygonSource.Update() .. GENERATED FROM PYTHON SOURCE LINES 35-36 wrap and plot using pyvista .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: Python mesh = pv.wrap(polygonSource.GetOutput()) mesh.plot(line_width=3, cpos='xy', color='k') .. image-sg:: /tutorial/06_vtk/images/sphx_glr_a_2_pyvista_vtk_001.png :alt: a 2 pyvista vtk :srcset: /tutorial/06_vtk/images/sphx_glr_a_2_pyvista_vtk_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 40-46 In this manner, you can get the "best of both worlds" should you need the flexibility of PyVista and the raw power of VTK. .. note:: You can use :func:`pyvista.Polygon` for a one line replacement of the above VTK code. .. GENERATED FROM PYTHON SOURCE LINES 48-52 VTK Algorithms ~~~~~~~~~~~~~~ Perhaps there is a VTK algorithm that is not (yet) exposed in PyVista that you'd like to use. This is easy enough to work with since PyVista objects are VTK objects. We can pass our PyVista meshes to the VTK algorithm, then wrap the output for plotting, further filtering, or anything. .. GENERATED FROM PYTHON SOURCE LINES 52-55 .. code-block:: Python mesh = examples.download_bunny_coarse() .. GENERATED FROM PYTHON SOURCE LINES 56-57 Initialize VTK algorithm .. GENERATED FROM PYTHON SOURCE LINES 57-59 .. code-block:: Python splatter = vtk.vtkGaussianSplatter() .. GENERATED FROM PYTHON SOURCE LINES 60-61 Pass PyVista object as input to VTK .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: Python splatter.SetInputData(mesh) .. GENERATED FROM PYTHON SOURCE LINES 64-65 Set parameters .. GENERATED FROM PYTHON SOURCE LINES 65-72 .. code-block:: Python n = 200 splatter.SetSampleDimensions(n, n, n) splatter.SetRadius(0.02) splatter.SetExponentFactor(-10) splatter.SetEccentricity(2) splatter.Update() .. GENERATED FROM PYTHON SOURCE LINES 73-74 Retrieve output and wrap with PyVista .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. code-block:: Python vol = pv.wrap(splatter.GetOutput()) .. GENERATED FROM PYTHON SOURCE LINES 77-78 Use PyVista to produce contours .. GENERATED FROM PYTHON SOURCE LINES 78-80 .. code-block:: Python cntrs = vol.contour([0.95 * splatter.GetRadius()]) .. GENERATED FROM PYTHON SOURCE LINES 81-82 Use PyVista to plot .. GENERATED FROM PYTHON SOURCE LINES 82-87 .. code-block:: Python p = pv.Plotter() p.add_mesh(mesh, style='wireframe') p.add_mesh(cntrs, color=True) p.show() .. image-sg:: /tutorial/06_vtk/images/sphx_glr_a_2_pyvista_vtk_002.png :alt: a 2 pyvista vtk :srcset: /tutorial/06_vtk/images/sphx_glr_a_2_pyvista_vtk_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 88-91 .. note:: The above example was adapted from VTK's `Embed Points Into Volume `_ .. GENERATED FROM PYTHON SOURCE LINES 93-100 .. raw:: html
Open In Colab
.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.247 seconds) .. _sphx_glr_download_tutorial_06_vtk_a_2_pyvista_vtk.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/06_vtk/a_2_pyvista_vtk.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: a_2_pyvista_vtk.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: a_2_pyvista_vtk.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_