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