Calling an Operator from a timer

I have been researching what is possible with timers, but have found some limitations. The main one being you cannot get access to many of the properties in bpy.context, and if you try to call an operator from the timer function it fails saying window is not found in context.

I am curios to know what other devs are using timers for. There are good examples here, but it doesn’t explain any real world use cases.

https://docs.blender.org/api/blender2.8/bpy.app.timers.html

you can override the context of an operator when you call it

Yeah I have tried that, and it works when Blender first starts, but the second time the timer is called i get this and Blender crashes.

Error : EXCEPTION_ACCESS_VIOLATION
Address : 0x00007FF6C0D78150
Module : d:\blender28source\build_windows_Full_x64_vc15_Release\bin\Release\blender.exe
The terminal process terminated with exit code: 11

The operator only has a print statement letting me know that it is executed, so there shouldn’t be an issue there.

I think that’s the question that should be asked/answered, you shouldn’t be getting an access violation simply for calling an operator via a timer. seems like a legitimate bug to me, can you put together a minimum repro example that can be verified?

Perhaps it would help if you post your exact code, or the operator you’re trying to call in your timer. The following example code works just fine, for example:

import bpy

def main(context):
    print("test")

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    def execute(self, context):
        main(context)
        return {'FINISHED'}

def test():    
    bpy.ops.object.simple_operator()
    return 10.0

def register():
    bpy.utils.register_class(SimpleOperator)
    bpy.app.timers.register(test)

def unregister():
    bpy.utils.unregister_class(SimpleOperator)
    bpy.app.timers.unregister(test)

if __name__ == "__main__":
    register()
1 Like

@Andrew_Peel Actually, I’ve just confirmed that this is a bug- the code I posted works fine on a build from a week ago, but with the latest nightly it crashes with an access violation. When I get back to my other workstation I’ll get the build number for the last version it worked correctly and post it here- this should definitely be reported on the bug tracker.

Edit: bug posted here: https://developer.blender.org/T65917

Thank you so much for helping verifying this as a bug and creating a bug report. I have tested this with the latest build and everything is working as expected.