Picking elements of a mesh

Picking elements of a mesh#

Download Python source code | Download Jupyter notebook

This example demonstrates how to pick different elements on meshes using enable_element_picking().

The different elements of a mesh are:

  • Mesh: pick the entire mesh (equivalent to enable_mesh_picking().)

  • Cell: pick a cell of the mesh (equivalent to enable_cell_picking().)

  • Face: pick a single face of a cell on the mesh

  • Edge: pick a single edge of a cell on the mesh

  • Point: pick a single point on the mesh

These types are captured in the pyvista.plotting.opts.ElementType enum class.

import pyvista as pv
from pyvista.plotting.opts import ElementType

Pick Face on Voxel Cell#

mesh = pv.Wavelet()

pl = pv.Plotter()
pl.add_mesh(mesh, show_edges=True, pickable=True)
pl.enable_element_picking(mode=ElementType.FACE)

pl.camera_position = pv.CameraPosition(
    position=(13.523728057554308, 9.910583926360937, 11.827103195167833),
    focal_point=(2.229008884793069, -2.782397236304676, 6.84282248642347),
    viewup=(-0.17641568583704878, -0.21978122178947299, 0.9594653304520027),
)

pl.show(auto_close=False)

# Programmatically pick a face to make example look nice
try:
    width, height = pl.window_size
    pl.iren._mouse_right_button_press(419, 263)
    pl.iren._mouse_right_button_release()
except AttributeError:
    # ignore this section when manually closing the window
    pass
element picking

Pick an Edge of a Cell#

sphere = pv.Sphere()

pl = pv.Plotter()
pl.add_mesh(sphere, show_edges=True, pickable=True)
pl.enable_element_picking(mode=ElementType.EDGE)

pl.camera_position = pv.CameraPosition(
    position=(0.7896646029990011, 0.7520805261169909, 0.5148524767495051),
    focal_point=(-0.014748048334009667, -0.0257133671899262, 0.07194025085895145),
    viewup=(-0.26016740957025775, -0.2603941863919363, 0.9297891087180916),
)

pl.show(auto_close=False)

# Programmatically pick a face to make example look nice
try:
    width, height = pl.window_size
    pl.iren._mouse_right_button_press(480, 300)
    pl.iren._mouse_right_button_release()
except AttributeError:
    # ignore this section when manually closing the window
    pass
element picking
/home/runner/_work/pyvista/pyvista/examples/02-plot/element_picking.py:75: UserWarning: Encountered issue in callback (most recent call last):
  File "/home/runner/_work/pyvista/pyvista/.tox/docs-build/lib/python3.14/site-packages/pyvista/plotting/picking.py", line 268, in __call__
    picked = self.get_edge(picked_point)
  File "/home/runner/_work/pyvista/pyvista/.tox/docs-build/lib/python3.14/site-packages/pyvista/plotting/picking.py", line 219, in get_edge
    cell = self.get_cell(picked_point).get_cell(0)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'get_cell'
  pl.iren._mouse_right_button_press(480, 300)

Tags: plot

Total running time of the script: (0 minutes 1.695 seconds)

Gallery generated by Sphinx-Gallery