Running Operator inside bpy.app.handler causes infinite loop

Good afternoon,
Im running into an issue here where im calling an operator from inside a handler registered at the bpy.app.handlers.depsgraph_update_pre() and bpy.app.handlers.depsgraph_post().

A quick look at the code makes this shine:

The view layer is being updated BEFORE and AFTER the CALL to the Operator in blender’s built-in code. The Before call to the update of the view layer makes further calls to the handlers registered as stated above.

My question is: is there a way to call an operator and bypass the layer update? what’s its reason of being?

I would rather not have to tweak blender’s python Core code yet.

Thanks in advance!

EDIT: Option (2) i have in mind is to remove the handler from inside the handler, calling the operator and then restoring back the handler but dont know if this is safe enough as it has to deal with the intrinsic features of Blender.

            handlers = []
            for _ in range(len(bpy.app.handlers.depsgraph_update_pre)):
                handlers.append(bpy.app.handlers.depsgraph_update_pre.pop())

            <...call to operator HERE...>

            for h in handlers:
                bpy.app.handlers.depsgraph_update_pre.append(h)

EDIT2: The operator i am trying to call within the handler does modify scene datablock contents hence the depsgraph.

In general the advice is, don’t use operators in handlers. Operators are user level tools, not a good programming API for manipulating the scene as part of dependency graph evaluation.

1 Like

Thanks,
What i finally did to overcome the problem is to override the call method of the class in the bpy/ops module in our pipeline, allowing to run special operators without updating the view layer before and after running the operator and without modifying blender’s built in python scripts. This way, if we switch to a newer version and the ops module and class in it suffers changes it shoudlnt affect us.
Im not sure this is the best “generic” solution, but since it s only meant to be used by our specific operator, it shouldnt cause any harm and for us it is a good fit.
Of course, if there is a better clean way to do it i would be more than happy to know, because im not aware of all the possible consequences of not updating the view layer.

Many thanks!