注釈
Go to the end to download the full example code.
Compositeピッキング#
pyvista.Plotter.enable_block_picking()
を使用して, pyvista.MultiBlock
の個々のブロックをピックする方法を示します.
from __future__ import annotations
import numpy as np
import pyvista as pv
MultiBlockデータセットの作成#
pyvista.ParametricSuperEllipsoid()
を使って100個のスーパーエリプソイドを作成する.
# Seed rng for reproducibility
rng = np.random.default_rng(seed=0)
def make_poly():
"""Create a superellipsoid in a random location."""
poly = pv.ParametricSuperEllipsoid(
n1=rng.random(),
n2=rng.random() * 2,
u_res=50,
v_res=50,
)
poly.points += rng.random(3) * 20
return poly
# Assemble the multiblock and plot it using the default plotting settings
blocks = pv.MultiBlock([make_poly() for _ in range(100)])
blocks.plot()

ブロックピッキングを有効にする#
pyvista.Plotter
に blocks
を追加し,ブロックのピッキングを有効にします. また,物理ベースのレンダリングを有効にして,ブロックがクリックされたときにブロックの色を赤に設定し,ブロックに既に色が設定されている場合は色を解除するようにコールバックを設定します.
pl = pv.Plotter()
actor, mapper = pl.add_composite(blocks, color='w', pbr=True, metallic=True)
def callback(index, *args):
"""Change a block to red if color is unset, and back to the actor color if set."""
if mapper.block_attr[index].color is None:
mapper.block_attr[index].color = 'r'
else:
mapper.block_attr[index].color = None
pl.enable_block_picking(callback, side='left')
pl.background_color = 'w'
pl.show()

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