I wrote a modal operator, but I have a problem and it is the following:
If I run this modal from the preferences by drawing a button in the addon layout, once I close the preferences, the modal stops, I think because of the missing context (I got this idea but I don't know if it's right)
Anyone have any idea how I can call and run the modal, so that once the preferences window is closed, it doesn't stop?
This example script is modal with the button into the addon['my_addon'].references panel:
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):
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)
class MyAddonPreferences(bpy.types.AddonPreferences):
bl_idname = "my_addon"
def draw(self, context):
layout=self.layout
layout.operator("wm.modal_timer_operator", text="Run Timer operator")