operator_modal_timer.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import bpy
  2. class ModalTimerOperator(bpy.types.Operator):
  3. """Operator which runs its self from a timer"""
  4. bl_idname = "wm.modal_timer_operator"
  5. bl_label = "Modal Timer Operator"
  6. _timer = None
  7. def modal(self, context, event):
  8. if event.type in {'RIGHTMOUSE', 'ESC'}:
  9. self.cancel(context)
  10. return {'CANCELLED'}
  11. if event.type == 'TIMER':
  12. # change theme color, silly!
  13. color = context.preferences.themes[0].view_3d.space.gradients.high_gradient
  14. color.s = 1.0
  15. color.h += 0.01
  16. return {'PASS_THROUGH'}
  17. def execute(self, context):
  18. wm = context.window_manager
  19. self._timer = wm.event_timer_add(0.1, window=context.window)
  20. wm.modal_handler_add(self)
  21. return {'RUNNING_MODAL'}
  22. def cancel(self, context):
  23. wm = context.window_manager
  24. wm.event_timer_remove(self._timer)
  25. def register():
  26. bpy.utils.register_class(ModalTimerOperator)
  27. def unregister():
  28. bpy.utils.unregister_class(ModalTimerOperator)
  29. if __name__ == "__main__":
  30. register()
  31. # test call
  32. bpy.ops.wm.modal_timer_operator()