Run ModalOperatorTimer automatically on animation playback I have the following script. It updates a MixRGB node's factor input to itself to solve some issues with GLSL viewport in textured mode. Anyways, I was wondering if there is a way to attach this script, so that it runs automatically when I click on the "Play" button or press Alt-A and stop automatically as well when the playback is suspended?
What would be the easiest way to go about doing this? Is creating an addon a feasible solution?

import bpy


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

    def modal(self, context, event):
#        if event.type in {'RIGHTMOUSE', 'ESC'}:
#            self.cancel(context)
#            return {'CANCELLED'}

        if event.type == 'TIMER':
            mat = bpy.data.materials['Material']
            nodes = mat.node_tree.nodes
            mix = nodes["Mix"].inputs[0]
            mix.default_value = mix.default_value
            print('Current Frame:')

        return {'PASS_THROUGH'}

    def execute(self, context):
        wm = context.window_manager
        self._timer = wm.event_timer_add(0.1, 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()


I'm aware of Handlers in Blender. However I tried the below script with no luck:

import bpy

def refreshNow(val):
    mat = bpy.data.materials['Material']
    nodes = mat.node_tree.nodes
    nodes["Mix"].inputs[0].default_value = nodes["Mix"].inputs[0].default_value
    return val

refreshNow(0)

bpy.app.driver_namespace['refresh'] = refreshNow

def my_handler():
    refreshNow(0)

def register():
    bpy.app.handlers.frame_change_post.append(my_handler)
Solution to the above problem is to use Handler rather than a Modal Operator.
Thanks to @Chebhou for his help in this thread and here.

Below is a working solution to execute on playback using handlers:

import bpy

def refreshNow(val):
    mat = bpy.data.materials['Material']
    nodes = mat.node_tree.nodes
    nodes["Mix"].inputs[0].default_value = nodes["Mix"].inputs[0].default_value
    bpy.context.scene.update()
    return val

refreshNow(0)

bpy.app.driver_namespace['refresh'] = refreshNow
def my_handler(scene):
    refreshNow(0)

def register():
    bpy.app.handlers.frame_change_post.append(my_handler)

if __name__ == "__main__":
    register()