As the title suggests, I would like to check if a modal operator is active, this using a threading.Thread function. This cola is not really easy. I have tried various techniques, but all of them lead to possible new problems. I haven't found an acceptable method, as in the threading.Thread function I absolutely don't want to access the bpy module, otherwise Blender will crash.
Example:
import bpy, threading
def thread_function():
while True:
# How to Check if the modal operator "ModalTimerOperator" is running?
print("Modal Operator is........")
class ModalTimerOperator(bpy.types.Operator):
"""Operator which runs itself from a timer"""
bl_idname = "wm.modal_timer_operator"
bl_label = "Modal Timer Operator"
_timer = None
def modal(self, context, event):
if event.type in {'RIGHTMOUSE', 'ESC'}:
self.cancel(context)
return {'CANCELLED'}
if event.type == 'TIMER':
# change theme color, silly!
color = context.preferences.themes[0].view_3d.space.gradients.high_gradient
color.s = 1.0
color.h += 0.01
return {'PASS_THROUGH'}
def execute(self, context):
thread = threading.Thread(target=thread_function)
thread.name = "Test"
thread.start()
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)
The main goal is to check if the Modal still running. Since blender could Crash, and the Thread function would go on indefinitely even if blender is no longer running (And in that case quit the thread function).
It could also happen that by running another process in Blender (such as rendering or some heavy operation) the Modal is Freezed for a few seconds, so I have to take this into consideration too.
if event.type
statements. Then you can see in the terminal if its running, because it will print every even tick. $\endgroup$