blender/scripts/templates_py/operator_modal_timer.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

55 lines
1.5 KiB
Python
Raw Normal View History

2011-03-28 13:53:53 +00:00
import bpy
class ModalTimerOperator(bpy.types.Operator):
2022-01-23 22:34:56 -06:00
"""Operator which runs itself 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'}:
self.cancel(context)
return {'CANCELLED'}
2011-03-28 13:53:53 +00:00
if event.type == 'TIMER':
# change theme color, silly!
color = context.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, window=context.window)
2013-10-22 00:25:15 +00:00
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 menu_func(self, context):
self.layout.operator(ModalTimerOperator.bl_idname, text=ModalTimerOperator.bl_label)
2011-03-28 13:53:53 +00:00
2011-03-28 13:53:53 +00:00
def register():
bpy.utils.register_class(ModalTimerOperator)
bpy.types.VIEW3D_MT_view.append(menu_func)
2011-03-28 13:53:53 +00:00
# Register and add to the "view" menu (required to also use F3 search "Modal Timer Operator" for quick access).
2011-03-28 13:53:53 +00:00
def unregister():
bpy.utils.unregister_class(ModalTimerOperator)
bpy.types.VIEW3D_MT_view.remove(menu_func)
2011-03-28 13:53:53 +00:00
if __name__ == "__main__":
register()
# test call
bpy.ops.wm.modal_timer_operator()