Workspace: Move engines to workspace and Properties Editor cleanup
[blender.git] / release / scripts / templates_py / operator_modal_timer.py
1 import bpy
2
3
4 class ModalTimerOperator(bpy.types.Operator):
5     """Operator which runs its self from a timer"""
6     bl_idname = "wm.modal_timer_operator"
7     bl_label = "Modal Timer Operator"
8
9     _timer = None
10
11     def modal(self, context, event):
12         if event.type in {'RIGHTMOUSE', 'ESC'}:
13             self.cancel(context)
14             return {'CANCELLED'}
15
16         if event.type == 'TIMER':
17             # change theme color, silly!
18             color = context.user_preferences.themes[0].view_3d.space.gradients.high_gradient
19             color.s = 1.0
20             color.h += 0.01
21
22         return {'PASS_THROUGH'}
23
24     def execute(self, context):
25         wm = context.window_manager
26         self._timer = wm.event_timer_add(0.1, context.window)
27         wm.modal_handler_add(self)
28         return {'RUNNING_MODAL'}
29
30     def cancel(self, context):
31         wm = context.window_manager
32         wm.event_timer_remove(self._timer)
33
34
35 def register():
36     bpy.utils.register_class(ModalTimerOperator)
37
38
39 def unregister():
40     bpy.utils.unregister_class(ModalTimerOperator)
41
42
43 if __name__ == "__main__":
44     register()
45
46     # test call
47     bpy.ops.wm.modal_timer_operator()