blender/release/scripts/templates_py/operator_modal_timer.py

47 lines
1.1 KiB
Python
Raw Normal View History

2011-03-28 13:53:53 +00:00
import bpy
class ModalTimerOperator(bpy.types.Operator):
"""Operator which runs its self from a timer"""
2011-03-28 13:53:53 +00:00
bl_idname = "wm.modal_timer_operator"
bl_label = "Modal Timer Operator"
_timer = None
def modal(self, context, event):
2013-10-22 00:25:15 +00:00
if event.type in {'RIGHTMOUSE', 'ESC'}:
2011-08-10 09:30:45 +00:00
return self.cancel(context)
2011-03-28 13:53:53 +00:00
if event.type == 'TIMER':
# change theme color, silly!
color = context.user_preferences.themes[0].view_3d.space.gradients.high_gradient
2011-03-28 13:53:53 +00:00
color.s = 1.0
color.h += 0.01
return {'PASS_THROUGH'}
def execute(self, context):
2013-10-22 00:25:15 +00:00
wm = context.window_manager
self._timer = wm.event_timer_add(0.1, context.window)
wm.modal_handler_add(self)
2011-03-28 13:53:53 +00:00
return {'RUNNING_MODAL'}
def cancel(self, context):
2013-10-22 00:25:15 +00:00
wm = context.window_manager
wm.event_timer_remove(self._timer)
2011-03-28 13:53:53 +00:00
def register():
bpy.utils.register_class(ModalTimerOperator)
def unregister():
bpy.utils.unregister_class(ModalTimerOperator)
if __name__ == "__main__":
register()
# test call
bpy.ops.wm.modal_timer_operator()