cut_interactive.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """Manipulate a box to cut a mesh interactively.
  2. Use mouse buttons to zoom and pan. Press:
  3. o to toggle the cutter on/off
  4. i to invert the selection
  5. t to print the cutter's transform
  6. x to get the cut mesh
  7. q to break interaction"""
  8. from vedo import Mesh, settings, dataurl, Plotter
  9. from vedo import BoxCutter, PlaneCutter, SphereCutter
  10. def func(evt):
  11. """Callback function to handle key presses."""
  12. k = evt.keypress
  13. if k == "o":
  14. print("toggle the cutter on/off")
  15. cutter.toggle()
  16. if k == "i":
  17. print("invert the selection")
  18. cutter.invert().render()
  19. if k == "t":
  20. print(cutter.transform)
  21. if k == "q":
  22. print("break interaction")
  23. plt.break_interaction()
  24. if k == "x":
  25. print("get the cut mesh")
  26. cut_mesh = cutter.get_cut_mesh(invert=False)
  27. print(cut_mesh.clean())
  28. if k == "Escape":
  29. print("exit program")
  30. plt.close()
  31. ##################################################
  32. settings.enable_default_keyboard_callbacks = False
  33. settings.enable_default_mouse_callbacks = False
  34. settings.default_font = "Calco"
  35. msh = Mesh(dataurl + "mouse_brain.stl")
  36. msh.subdivide().backcolor("purple8")
  37. # Create the plotter with the mesh, do not block the execution
  38. plt = Plotter(bg="blackboard")
  39. plt.add_callback("on key press", func)
  40. # ######## Create the cutter object
  41. # cutter = PlaneCutter(msh)
  42. cutter = BoxCutter(msh)
  43. # cutter = SphereCutter(msh)
  44. cutter.enable_rotation(True)
  45. cutter.enable_translation(True)
  46. cutter.enable_scaling(True)
  47. plt.add(cutter, __doc__)
  48. cutter.on() # enable the cutter after adding it to the plotter
  49. plt.show(viewup="z")
  50. plt.close()