depsgraph_update_pre is not triggered on manual depsgraph update #113930

Open
opened 2023-10-19 12:21:43 +02:00 by Andrej · 4 comments
Contributor

Blender 3.6.4

Typically bpy.context.view_layer.update() is used to update depsgraph manually but it doesn't seem to trigger depsgraph_update_pre handler. I also tried multiple other ways to update depsgraph and none of them trigger the handler too.

The workaround is to call some other operator, it will trigger the handler. But need to make sure to change hide_render, hide_select, hide_viewport of some object, otherwise it wouldn't work.

What expected - manual depsgraph update to trigger the callbacks added to depsgraph_update_pre handler

The use case was trying to make automated tests for blender addon and was trying to make a way to trigger depsgraph_update_pre when Blender is running in background mode.

  1. Empty blender scene, select cube.
  2. Run the code below.
  3. Run operator "Depsgraph Update Manual" from F3 menu. despgraph_pre_handler will triggered after empty_operator call.
  4. change if True to if False - despgraph_pre_handler won't be triggered even after empty_operator call.

Code output:

operator started
----------------> view_layer update
----------------> evaluated_depsgraph_get update
----------------> hide_render change
----------------> hide_select  change
----------------> hide_viewport change
calling another operator
despgraph_pre_handler_triggered
operator has finished

import bpy
from bpy.app.handlers import persistent

class UpdateDepsgraphManual(bpy.types.Operator):
    bl_idname = "object.update_depsgraph_manual"
    bl_label = "Depsgraph Update Manual"
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        print('operator started')
        print('----------------> view_layer update')
        bpy.context.view_layer.update()
        print('----------------> evaluated_depsgraph_get update')
        bpy.context.evaluated_depsgraph_get().update()

        # NOTE: if changed to False then `bpy.ops.object.empty_operator` wouldnt trigger
        # depsgraph update either
        if True:
            obj = context.object
            print('----------------> hide_render change')
            obj.hide_render = obj.hide_render
            print('----------------> hide_select  change')
            obj.hide_select  = obj.hide_select 
            print('----------------> hide_viewport change')
            obj.hide_viewport = obj.hide_viewport

        print('calling another operator')
        bpy.ops.object.empty_operator()

        print('operator has finished')
        return {'FINISHED'}
    
class EmptyOperator(bpy.types.Operator):
    bl_idname = "object.empty_operator"
    bl_label = "Empty Operator"
    bl_options = {"REGISTER", "UNDO"}

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

@persistent
def depsgraph_update_pre_handler(scene):
    print('despgraph_pre_handler_triggered')

def register():
    bpy.app.handlers.depsgraph_update_pre.append(depsgraph_update_pre_handler)
    bpy.utils.register_class(UpdateDepsgraphManual)
    bpy.utils.register_class(EmptyOperator)

if __name__ == "__main__":
    register()
Blender 3.6.4 Typically `bpy.context.view_layer.update()` is used to update depsgraph manually but it doesn't seem to trigger `depsgraph_update_pre` handler. I also tried multiple other ways to update depsgraph and none of them trigger the handler too. The workaround is to call some other operator, it will trigger the handler. But need to make sure to change `hide_render`, `hide_select`, `hide_viewport` of some object, otherwise it wouldn't work. What expected - manual depsgraph update to trigger the callbacks added to `depsgraph_update_pre` handler The use case was trying to make automated tests for blender addon and was trying to make a way to trigger `depsgraph_update_pre` when Blender is running in background mode. 1. Empty blender scene, select cube. 2. Run the code below. 3. Run operator "Depsgraph Update Manual" from F3 menu. `despgraph_pre_handler` will triggered after `empty_operator` call. 4. change `if True` to `if False` - `despgraph_pre_handler` won't be triggered even after `empty_operator` call. Code output: ``` operator started ----------------> view_layer update ----------------> evaluated_depsgraph_get update ----------------> hide_render change ----------------> hide_select change ----------------> hide_viewport change calling another operator despgraph_pre_handler_triggered operator has finished ``` ```python import bpy from bpy.app.handlers import persistent class UpdateDepsgraphManual(bpy.types.Operator): bl_idname = "object.update_depsgraph_manual" bl_label = "Depsgraph Update Manual" bl_options = {"REGISTER", "UNDO"} def execute(self, context): print('operator started') print('----------------> view_layer update') bpy.context.view_layer.update() print('----------------> evaluated_depsgraph_get update') bpy.context.evaluated_depsgraph_get().update() # NOTE: if changed to False then `bpy.ops.object.empty_operator` wouldnt trigger # depsgraph update either if True: obj = context.object print('----------------> hide_render change') obj.hide_render = obj.hide_render print('----------------> hide_select change') obj.hide_select = obj.hide_select print('----------------> hide_viewport change') obj.hide_viewport = obj.hide_viewport print('calling another operator') bpy.ops.object.empty_operator() print('operator has finished') return {'FINISHED'} class EmptyOperator(bpy.types.Operator): bl_idname = "object.empty_operator" bl_label = "Empty Operator" bl_options = {"REGISTER", "UNDO"} def execute(self, context): return {'FINISHED'} @persistent def depsgraph_update_pre_handler(scene): print('despgraph_pre_handler_triggered') def register(): bpy.app.handlers.depsgraph_update_pre.append(depsgraph_update_pre_handler) bpy.utils.register_class(UpdateDepsgraphManual) bpy.utils.register_class(EmptyOperator) if __name__ == "__main__": register() ```
Andrej added the
Status
Needs Triage
Type
Report
Priority
Normal
labels 2023-10-19 12:21:44 +02:00
Iliya Katushenock added the
Interest
Dependency Graph
label 2023-10-19 13:23:35 +02:00
Member

Code from 4b06d0bf51 only triggers the callbacks if any ID actually changed, see https://projects.blender.org/blender/blender/src/branch/main/source/blender/blenkernel/intern/scene.cc#L2550

See also discussion in https://archive.blender.org/developer/D3977

So this looks like it is actually working as expected.

The use case was trying to make automated tests for blender addon and was trying to make a way to trigger depsgraph_update_pre when Blender is running in background mode.

Maybe you could elaborate why having the callbacks only fire if an ID changed is problematic for your usecase?

Code from 4b06d0bf51c3 only triggers the callbacks if any ID actually changed, see https://projects.blender.org/blender/blender/src/branch/main/source/blender/blenkernel/intern/scene.cc#L2550 See also discussion in https://archive.blender.org/developer/D3977 So this looks like it is actually working as expected. > The use case was trying to make automated tests for blender addon and was trying to make a way to trigger depsgraph_update_pre when Blender is running in background mode. Maybe you could elaborate why having the callbacks only fire if an ID changed is problematic for your usecase?
Philipp Oeser added
Status
Needs Information from User
and removed
Status
Needs Triage
labels 2023-11-02 14:47:14 +01:00
Author
Contributor

Maybe you could elaborate why having the callbacks only fire if an ID changed is problematic for your usecase?

Didn't realised it's any ID change - have seen somewhere mention that it's somehow tied only to objects being hidden/displayed. Any ID change will totally work for me.
And I think I've focused on second operator call and overlooked the view_layer.update() and context.evaluated_depsgraph_get().update() triggering depsgraph_update_pre too if some ID was changed. So I'll probably stick to something like scene.show_subframe = scene.show_subframe; context.view_layer.update() to trigger depsgraph callback from background mode. Thank you.

If someone will be looking for a working example:

----------------> scene property change
----------------> view_layer update
despgraph_pre_handler_triggered
----------------> scene property change
----------------> evaluated_depsgraph_get update
despgraph_pre_handler_triggered
----------------> scene property change
calling another operator
despgraph_pre_handler_triggered
operator has finished

import bpy
from bpy.app.handlers import persistent
from time import sleep

class UpdateDepsgraphManual(bpy.types.Operator):
    bl_idname = "object.update_depsgraph_manual"
    bl_label = "Depsgraph Update Manual"
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        print('operator started')
        scene = context.scene
        print('----------------> scene property change')
        scene.show_subframe = scene.show_subframe
        print('----------------> view_layer update')
        bpy.context.view_layer.update()
        print('----------------> scene property change')
        scene.show_subframe = scene.show_subframe
        print('----------------> evaluated_depsgraph_get update')
        bpy.context.evaluated_depsgraph_get().update()
        print('----------------> scene property change')
        scene.show_subframe = scene.show_subframe
        print('calling another operator')
        bpy.ops.object.empty_operator()

        print('operator has finished')
        return {'FINISHED'}
    
class EmptyOperator(bpy.types.Operator):
    bl_idname = "object.empty_operator"
    bl_label = "Empty Operator"
    bl_options = {"REGISTER", "UNDO"}

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

@persistent
def depsgraph_update_pre_handler(scene):
    print('despgraph_pre_handler_triggered')

def register():
    bpy.app.handlers.depsgraph_update_pre.append(depsgraph_update_pre_handler)
    bpy.utils.register_class(UpdateDepsgraphManual)
    bpy.utils.register_class(EmptyOperator)

if __name__ == "__main__":
    register()

> Maybe you could elaborate why having the callbacks only fire if an ID changed is problematic for your usecase? Didn't realised it's any ID change - have seen somewhere mention that it's somehow tied only to objects being hidden/displayed. Any ID change will totally work for me. And I think I've focused on second operator call and overlooked the `view_layer.update()` and `context.evaluated_depsgraph_get().update()` triggering `depsgraph_update_pre` too if some ID was changed. So I'll probably stick to something like `scene.show_subframe = scene.show_subframe; context.view_layer.update()` to trigger depsgraph callback from background mode. Thank you. If someone will be looking for a working example: ``` ----------------> scene property change ----------------> view_layer update despgraph_pre_handler_triggered ----------------> scene property change ----------------> evaluated_depsgraph_get update despgraph_pre_handler_triggered ----------------> scene property change calling another operator despgraph_pre_handler_triggered operator has finished ``` ```python import bpy from bpy.app.handlers import persistent from time import sleep class UpdateDepsgraphManual(bpy.types.Operator): bl_idname = "object.update_depsgraph_manual" bl_label = "Depsgraph Update Manual" bl_options = {"REGISTER", "UNDO"} def execute(self, context): print('operator started') scene = context.scene print('----------------> scene property change') scene.show_subframe = scene.show_subframe print('----------------> view_layer update') bpy.context.view_layer.update() print('----------------> scene property change') scene.show_subframe = scene.show_subframe print('----------------> evaluated_depsgraph_get update') bpy.context.evaluated_depsgraph_get().update() print('----------------> scene property change') scene.show_subframe = scene.show_subframe print('calling another operator') bpy.ops.object.empty_operator() print('operator has finished') return {'FINISHED'} class EmptyOperator(bpy.types.Operator): bl_idname = "object.empty_operator" bl_label = "Empty Operator" bl_options = {"REGISTER", "UNDO"} def execute(self, context): return {'FINISHED'} @persistent def depsgraph_update_pre_handler(scene): print('despgraph_pre_handler_triggered') def register(): bpy.app.handlers.depsgraph_update_pre.append(depsgraph_update_pre_handler) bpy.utils.register_class(UpdateDepsgraphManual) bpy.utils.register_class(EmptyOperator) if __name__ == "__main__": register() ```
Blender Bot added
Status
Archived
and removed
Status
Needs Information from User
labels 2023-11-02 15:20:00 +01:00
Author
Contributor

@lichtwerk
Oh, actually, there is a bit more to it. The initial problem I've run into is that despgraph update wasn't triggered when I changed property from some ID's propertygroup. Doesn't it considered to be ID change too?
Changing this property from UI is triggerring depsgraph update but not when it's changed from code/background mode.

Example code:

operator started
----------------> camera property change
----------------> view_layer update
----------------> camera property change
----------------> evaluated_depsgraph_get update
----------------> camera property change
calling another operator
operator has finished

import bpy
from bpy.app.handlers import persistent
from time import sleep

class UpdateDepsgraphManual(bpy.types.Operator):
    bl_idname = "object.update_depsgraph_manual"
    bl_label = "Depsgraph Update Manual"
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        print('operator started')
        scene = context.scene
        print('----------------> camera property change')
        scene.test_prop_group.my_value = 25
        print('----------------> view_layer update')
        bpy.context.view_layer.update()
        print('----------------> camera property change')
        scene.test_prop_group.my_value = 25
        print('----------------> evaluated_depsgraph_get update')
        bpy.context.evaluated_depsgraph_get().update()
        print('----------------> camera property change')
        scene.test_prop_group.my_value = 25
        print('calling another operator')
        bpy.ops.object.empty_operator()

        print('operator has finished')
        return {'FINISHED'}
    

class NewTestPropGroup(bpy.types.PropertyGroup):
    my_value: bpy.props.IntProperty(name="test", default=0)

class EmptyOperator(bpy.types.Operator):
    bl_idname = "object.empty_operator"
    bl_label = "Empty Operator"
    bl_options = {"REGISTER", "UNDO"}

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

@persistent
def depsgraph_update_pre_handler(scene):
    print('despgraph_pre_handler_triggered')

def register():
    bpy.app.handlers.depsgraph_update_pre.append(depsgraph_update_pre_handler)
    bpy.utils.register_class(UpdateDepsgraphManual)
    bpy.utils.register_class(EmptyOperator)
    bpy.utils.register_class(NewTestPropGroup)
    bpy.types.Scene.test_prop_group = bpy.props.PointerProperty(type=NewTestPropGroup)

if __name__ == "__main__":
    register()

@lichtwerk Oh, actually, there is a bit more to it. The initial problem I've run into is that despgraph update wasn't triggered when I changed property from some ID's propertygroup. Doesn't it considered to be ID change too? Changing this property from UI is triggerring depsgraph update but not when it's changed from code/background mode. Example code: ``` operator started ----------------> camera property change ----------------> view_layer update ----------------> camera property change ----------------> evaluated_depsgraph_get update ----------------> camera property change calling another operator operator has finished ``` ```python import bpy from bpy.app.handlers import persistent from time import sleep class UpdateDepsgraphManual(bpy.types.Operator): bl_idname = "object.update_depsgraph_manual" bl_label = "Depsgraph Update Manual" bl_options = {"REGISTER", "UNDO"} def execute(self, context): print('operator started') scene = context.scene print('----------------> camera property change') scene.test_prop_group.my_value = 25 print('----------------> view_layer update') bpy.context.view_layer.update() print('----------------> camera property change') scene.test_prop_group.my_value = 25 print('----------------> evaluated_depsgraph_get update') bpy.context.evaluated_depsgraph_get().update() print('----------------> camera property change') scene.test_prop_group.my_value = 25 print('calling another operator') bpy.ops.object.empty_operator() print('operator has finished') return {'FINISHED'} class NewTestPropGroup(bpy.types.PropertyGroup): my_value: bpy.props.IntProperty(name="test", default=0) class EmptyOperator(bpy.types.Operator): bl_idname = "object.empty_operator" bl_label = "Empty Operator" bl_options = {"REGISTER", "UNDO"} def execute(self, context): return {'FINISHED'} @persistent def depsgraph_update_pre_handler(scene): print('despgraph_pre_handler_triggered') def register(): bpy.app.handlers.depsgraph_update_pre.append(depsgraph_update_pre_handler) bpy.utils.register_class(UpdateDepsgraphManual) bpy.utils.register_class(EmptyOperator) bpy.utils.register_class(NewTestPropGroup) bpy.types.Scene.test_prop_group = bpy.props.PointerProperty(type=NewTestPropGroup) if __name__ == "__main__": register() ```
Member

Need to check again

Need to check again
Blender Bot added
Status
Needs Triage
and removed
Status
Archived
labels 2023-11-02 18:30:55 +01:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#113930
No description provided.