File: mesh-picking.py

package info (click to toggle)
python-pyvista 0.44.1-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 159,804 kB
  • sloc: python: 72,164; sh: 118; makefile: 68
file content (75 lines) | stat: -rw-r--r-- 1,665 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
.. _mesh_picking_example:

Picking Meshes
~~~~~~~~~~~~~~
This example demonstrates how to pick meshes using
:func:`enable_mesh_picking() <pyvista.Plotter.enable_mesh_picking>`.

"""

from __future__ import annotations

import pyvista as pv

# %%
# Pick either a cube or a sphere using "p"
# ++++++++++++++++++++++++++++++++++++++++
#

sphere = pv.Sphere(center=(1, 0, 0))
cube = pv.Cube()

pl = pv.Plotter()
pl.add_mesh(sphere, color='r')
pl.add_mesh(cube, color='b')
pl.enable_mesh_picking()
pl.show()


# %%
# Deform the mesh after picking
# +++++++++++++++++++++++++++++
# Pick to trigger a callback that "shrinks" the mesh each time it's selected.


def callback(mesh):
    """Shrink the mesh each time it's clicked."""
    shrunk = mesh.shrink(0.9)
    mesh.copy_from(shrunk)  # make operation "in-place" by replacing the original mesh


pl = pv.Plotter()
pl.add_mesh(sphere, color='r')
pl.add_mesh(cube, color='b')
pl.enable_mesh_picking(callback=callback, show=False)
pl.show()


# %%
# Pick based on Actors
# ++++++++++++++++++++
# Return the picked actor to the callback

pl = pv.Plotter()
pl.add_mesh(pv.Cone(center=(0, 0, 0)), name='Cone')
pl.add_mesh(pv.Cube(center=(1, 0, 0)), name='Cube')
pl.add_mesh(pv.Sphere(center=(1, 1, 0)), name='Sphere')
pl.add_mesh(pv.Cylinder(center=(0, 1, 0)), name='Cylinder')


def reset():
    for a in pl.renderer.actors.values():
        if isinstance(a, pv.Actor):
            a.prop.color = 'lightblue'
            a.prop.show_edges = False


def callback(actor):
    reset()
    actor.prop.color = 'green'
    actor.prop.show_edges = True


pl.enable_mesh_picking(callback, use_actor=True, show=False)
pl.show()