mousehighlight.py 710 B

12345678910111213141516171819202122232425
  1. """Click a sphere to highlight it"""
  2. from vedo import Text2D, Sphere, Plotter
  3. import numpy as np
  4. spheres = []
  5. for i in range(25):
  6. p = np.random.rand(2)
  7. s = Sphere(r=0.05).pos(p).color('k5')
  8. s.name = f"sphere nr.{i} at {p}"
  9. spheres.append(s)
  10. def func(evt):
  11. if not evt.object:
  12. return
  13. sil = evt.object.silhouette().linewidth(6).c('red5')
  14. sil.name = "silu" # give it a name so we can remove the old one
  15. msg.text("You clicked: " + evt.object.name)
  16. plt.remove('silu').add(sil)
  17. msg = Text2D("", pos="bottom-center", c='k', bg='r9', alpha=0.8)
  18. plt = Plotter(axes=1, bg='black')
  19. plt.add_callback('mouse click', func)
  20. plt.show(spheres, msg, __doc__, zoom=1.2)
  21. plt.close()