timer_callback2.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Create a class which wraps the vedo.Plotter class and adds a timer callback
  2. # Credits: Nicolas Antille, https://github.com/nantille
  3. # Check out the simpler example: timer_callback1.py
  4. import vedo
  5. class Viewer:
  6. def __init__(self, *args, **kwargs):
  7. self.dt = kwargs.pop("dt", 100) # update every dt milliseconds
  8. self.timer_id = None
  9. self.isplaying = False
  10. self.counter = 0 # frame counter
  11. self.button = None
  12. self.plotter = vedo.Plotter(*args, **kwargs) # setup the Plotter object
  13. self.timerevt = self.plotter.add_callback('timer', self.handle_timer, enable_picking=False)
  14. def initialize(self):
  15. # initialize here extra elements like buttons etc..
  16. self.button = self.plotter.add_button(
  17. self._buttonfunc,
  18. states=["\u23F5 Play ","\u23F8 Pause"],
  19. font="Kanopus",
  20. size=32,
  21. )
  22. return self
  23. def show(self, *args, **kwargs):
  24. plt = self.plotter.show(*args, **kwargs)
  25. return plt
  26. def _buttonfunc(self, obj, ename):
  27. if self.timer_id is not None:
  28. self.plotter.timer_callback("destroy", self.timer_id)
  29. if not self.isplaying:
  30. self.timer_id = self.plotter.timer_callback("create", dt=100)
  31. self.button.switch()
  32. self.isplaying = not self.isplaying
  33. def handle_timer(self, event):
  34. #####################################################################
  35. ### Animate your stuff here ###
  36. #####################################################################
  37. #print(event) # info about what was clicked and more
  38. moon.color(self.counter) # change color to the Moon
  39. earth.rotate_z(2) # rotate the Earth
  40. moon.rotate_z(1)
  41. txt2d.text("Moon color is:").color(self.counter).background(self.counter,0.1)
  42. txt2d.text(vedo.get_color_name(self.counter), "top-center")
  43. txt2d.text("..press q to quit", "bottom-right")
  44. self.plotter.render()
  45. self.counter += 1
  46. viewer = Viewer(axes=1, dt=150).initialize()
  47. earth = vedo.Earth()
  48. moon = vedo.Sphere(r=0.1).x(1.5).color('k7')
  49. txt2d = vedo.CornerAnnotation().font("Kanopus")
  50. viewer.show(earth, moon, txt2d, viewup='z').close()