New timers, have no context.object. Why is that so. Cant override it

I use simple:

bpy.app.timers.register(run_import_periodically, persistent=True)

def run_import_periodically():
bpy.ops.scene.gob_import()
return time_interval

But gob_import operator doesn’t ‘see’ context.object (is none), no context.window etc. and so I can’t make this operator work.
I tried to override context, but then i get bad poll inside gob_import(override) execute method, on bpy.ops.object.mode_set(mode=‘OBJECT’).
Override is:

oContextOverride = {}
    obj = bpy.context.view_layer.objects.active
    for oWindow in bpy.context.window_manager.windows:  # IMPROVE: Find way to avoid doing four levels of traversals at every request!!
        oScreen = oWindow.screen
        for oArea in oScreen.areas:
            if oArea.type == 'VIEW_3D':  #  window/screen/area 
                for oRegion in oArea.regions:
                    if oRegion.type == 'WINDOW':  
                        oContextOverride = {'window': oWindow, 'screen': oScreen, 'area': oArea, 'region': oRegion, 'scene': bpy.context.scene, 'selected_editable_objects': obj,
                                            'active_object': obj, 'selected_objects': obj} 

bpy.ops.scene.gob_import(oContextOverride)

The context should be ok now, but I get the poll error on mode_set(mode = ‘OBJECT’).
I also tried mode_set(context.copy(0), mode = ‘OBJECT’) but then blender just crashes.

Why operator called from new timers, dosen’t see the bpy.context like every other operator can? How to fix it.

Ok if figured it out. The missing part was
‘workspace’: window.workspace[0]
Now mode_set works ok.

These timers are global, they are not bound to any context. E.g. they don’t belong to any window/scene/…

If possible, you should avoid calling operators from timers.
If you have to use one, you have to override the context as much as you can… Yes, this can be a bit annoying. I do not have a better solution for it yet.

1 Like

I followed you suggestion. Now Goz addon import timer is executing the operator, only if zbrush file timestamp is updated. This avoid to many operator calls. Reading file timestamp every 2 seconds should be less resource hungry than running the operator every 2sec, I think…

1 Like