Debian Codegraph @

examples/02-plot/mesh_picking.py

raw
1 """
2 .. _mesh_picking_example:
3
4 Picking Meshes
5 ~~~~~~~~~~~~~~
6 This example demonstrates how to pick meshes using
7 :func:`enable_mesh_picking() <pyvista.Plotter.enable_mesh_picking>`.
8
9 """
10
11 import pyvista as pv
12
13 # %%
14 # Pick either a cube or a sphere using "p"
15 # ++++++++++++++++++++++++++++++++++++++++
16 #
17
18 sphere = pv.Sphere(center=(1, 0, 0))
19 cube = pv.Cube()
20
21 pl = pv.Plotter()
22 pl.add_mesh(sphere, color='r')
23 pl.add_mesh(cube, color='b')
24 pl.enable_mesh_picking()
25 pl.show()
26
27
28 # %%
29 # Deform the mesh after picking
30 # +++++++++++++++++++++++++++++
31 # Pick to trigger a callback that "shrinks" the mesh each time it's selected.
32
33
34 def callback(mesh):
35 """Shrink the mesh each time it's clicked."""
36 shrunk = mesh.shrink(0.9)
37 mesh.copy_from(shrunk) # make operation "in-place" by replacing the original mesh
38
39
40 pl = pv.Plotter()
41 pl.add_mesh(sphere, color='r')
42 pl.add_mesh(cube, color='b')
43 pl.enable_mesh_picking(callback=callback, show=False)
44 pl.show()
45
46
47 # %%
48 # Pick based on Actors
49 # ++++++++++++++++++++
50 # Return the picked actor to the callback
51
52 pl = pv.Plotter()
53 pl.add_mesh(pv.Cone(center=(0, 0, 0)), name='Cone')
54 pl.add_mesh(pv.Cube(center=(1, 0, 0)), name='Cube')
55 pl.add_mesh(pv.Sphere(center=(1, 1, 0)), name='Sphere')
56 pl.add_mesh(pv.Cylinder(center=(0, 1, 0)), name='Cylinder')
57
58
59 def reset():
60 for a in pl.renderer.actors.values():
61 if isinstance(a, pv.Actor):
62 a.prop.color = 'lightblue'
63 a.prop.show_edges = False
64
65
66 def callback(actor):
67 reset()
68 actor.prop.color = 'green'
69 actor.prop.show_edges = True
70
71
72 pl.enable_mesh_picking(callback, use_actor=True, show=False)
73 pl.show()
74 # %%
75 # .. tags:: plot