Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import bpy
import sys
import os
import traceback
class ModalTimerOperator(bpy.types.Operator):
"""Operator which runs its self from a timer"""
bl_idname = "wm.modal_timer_operator"
bl_label = "Modal Timer Operator"
_timer = None
prev=''
def modal(self, context, event):
if event.type in {'RIGHTMOUSE', 'ESC'}:
self.cancel(context)
return {'CANCELLED'}
if event.type == 'TIMER':
curr= open(sys.argv[-1]).read()
if not self.prev==curr:
print("%s changed" % sys.argv[-1])
try:
exec(curr)
self.prev=curr
print('done ok');
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
print(traceback.format_exc())
self.prev=curr
pass
return {'PASS_THROUGH'}
def execute(self, context):
wm = context.window_manager
self._timer = wm.event_timer_add(0.1, window=context.window)
wm.modal_handler_add(self)
return {'RUNNING_MODAL'}
def cancel(self, context):
wm = context.window_manager
wm.event_timer_remove(self._timer)
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment