0
$\begingroup$

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")
$\endgroup$
3
  • $\begingroup$ Does this answer your question? $\endgroup$
    – Jakemoyo
    Jul 7, 2022 at 22:24
  • $\begingroup$ Unfortunately I don't think so, the modal simply has to keep running, as access to it is on the addon interface and also in the addon preferences. But I was surprised to notice that if you start from preferences layout it stops when you close the preferences window. In the link you suggested, we talk about starting a modal without the user's knowledge. This would be doable with some tricks, but it's not my case where I have several buttons calling the same modal with different options. $\endgroup$
    – Noob Cat
    Jul 7, 2022 at 22:45
  • $\begingroup$ I was thinking of creating a "Fake" context and trying to pass it to the operator. But even I have had little success there $\endgroup$
    – Noob Cat
    Jul 7, 2022 at 22:47

0

You must log in to answer this question.

Browse other questions tagged .