Skip to content

Instantly share code, notes, and snippets.

@simonecesano
Created August 7, 2021 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonecesano/91b4a820ebb862c26438c532183ded64 to your computer and use it in GitHub Desktop.
Save simonecesano/91b4a820ebb862c26438c532183ded64 to your computer and use it in GitHub Desktop.
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