mousehover1.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """Visualize scalar values interactively
  2. by hovering the mouse on a mesh
  3. Press c to clear the path"""
  4. from vedo import precision, Arrow, Text2D, ParametricShape, Plotter
  5. def func(evt): ### called every time mouse moves!
  6. msh = evt.object # get the mesh that triggered the event
  7. if not msh:
  8. return # mouse hits nothing, return.
  9. pt = evt.picked3d # 3d coords of point under mouse
  10. pid = msh.closest_point(pt, return_point_id=True)
  11. txt =(
  12. f"Point: {precision(pt[:2] ,2)}\n"
  13. f"Height: {precision(arr[pid],3)}\n"
  14. f"Ground speed: {precision(evt.speed3d*100,2)}"
  15. )
  16. msg.text(txt) # update text message
  17. ar = Arrow(pt - evt.delta3d, pt, s=0.001, c='orange5')
  18. fp = msh.flagpole(
  19. txt, point=pt,s=0.04, c='k', font="VictorMono",
  20. )
  21. fp.follow_camera() # make it always face the camera
  22. plt.remove("FlagPole").add(ar, fp) # remove the old flagpole, add the new
  23. plt.render()
  24. msg = Text2D(pos='bottom-left', font="VictorMono") # an empty text
  25. hil = ParametricShape('RandomHills').cmap('terrain').add_scalarbar()
  26. arr = hil.pointdata["Scalars"] # numpy array with heights
  27. plt = Plotter(axes=1, bg2='lightblue')
  28. plt.add_callback('mouse move', func) # add the callback function
  29. plt.add_callback('keyboard', lambda _: plt.remove("Arrow").render())
  30. plt.show(hil, msg, __doc__, viewup='z')
  31. plt.close()