Depsgraph not updating properly for frame_change_pre handler #77126

Closed
opened 2020-05-28 15:30:32 +02:00 by Christopher Gearhart · 12 comments

System Information
Operating system: Windows 10
Graphics card:

Blender Version
Broken: 2.83
Worked: Never

Short description of error

Depsgraph access in the frame_update_pre handler differs from viewport animation to render animation. It seems the global depsgraph does not update during render animation. I believe a simple solution would be to provide a depsgraph argument in the frame_update_pre handler (similar to the frame_change_post handler).

Exact steps for others to reproduce the error
Based on the default startup:

  1. Animate the default cube's location over the first 10 frames (and set timeline end frame to 10)
  2. Run the following script:
import bpy
from bpy.app.handlers import persistent

@persistent
def get_mw_translation(scene, depsgraph=None):
    text="post:"
    if depsgraph is None:
        depsgraph = bpy.context.evaluated_depsgraph_get()
        text = "pre:"
        print()
    cube = bpy.data.objects["Cube"].evaluated_get(depsgraph)
    print(text, cube.matrix_world.to_translation())
    
bpy.app.handlers.frame_change_pre.append(get_mw_translation)
bpy.app.handlers.frame_change_post.append(get_mw_translation)
  1. Play the animation. Watch the output in the console, and note that the matrix world data updates appropriately for both 'pre' and 'post' handlers.
  2. Simplify your render settings to render quickly (e.g. Cycles at 5 samples)
  3. Render the animation. Watch the output in the console, and note that the 'pre' handler does not update, while the 'post' handler updates appropriately.
**System Information** Operating system: Windows 10 Graphics card:<irrelevant> **Blender Version** Broken: 2.83 Worked: Never **Short description of error** Depsgraph access in the `frame_update_pre` handler differs from viewport animation to render animation. It seems the global depsgraph does not update during render animation. I believe a simple solution would be to provide a depsgraph argument in the `frame_update_pre` handler (similar to the `frame_change_post` handler). **Exact steps for others to reproduce the error** Based on the default startup: 1. Animate the default cube's location over the first 10 frames (and set timeline end frame to 10) 2. Run the following script: ``` import bpy from bpy.app.handlers import persistent @persistent def get_mw_translation(scene, depsgraph=None): text="post:" if depsgraph is None: depsgraph = bpy.context.evaluated_depsgraph_get() text = "pre:" print() cube = bpy.data.objects["Cube"].evaluated_get(depsgraph) print(text, cube.matrix_world.to_translation()) bpy.app.handlers.frame_change_pre.append(get_mw_translation) bpy.app.handlers.frame_change_post.append(get_mw_translation) ``` 3. Play the animation. Watch the output in the console, and note that the matrix world data updates appropriately for both 'pre' and 'post' handlers. 4. Simplify your render settings to render quickly (e.g. Cycles at 5 samples) 5. Render the animation. Watch the output in the console, and note that the 'pre' handler does not update, while the 'post' handler updates appropriately.

Added subscriber: @bblanimation

Added subscriber: @bblanimation

Added subscriber: @mano-wii

Added subscriber: @mano-wii

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'

Looks well related to #71908 if not the same problem.

Looks well related to #71908 if not the same problem.

Added subscriber: @dr.sybren

Added subscriber: @dr.sybren
Sybren A. Stüvel self-assigned this 2020-06-02 14:35:25 +02:00

This is not the same as #71908.

The difference between viewport & render animation is as follows. In the viewport the depsgraph is "active", and thus the updates by the animation system are written back to the original datablocks. For rendering a new depsgraph is made (because all kinds of things have to be evaluated differently), which as a result is not marked as "active", and thus animation updates are not written back to the original datablocks. The problem here is that bpy.context will give you the viewport depsgraph, and not the render depsgraph. It'll thus always return the values as are currently seen in the viewport.

As can be seen in BKE_scene_graph_update_for_newframe() (in scene.c), updating the scene for a new frame happens as follows:

  • Prior to calling BKE_scene_graph_update_for_newframe(), the scene's current frame is updated to the new frame.
  • The frame_change_pre() callback is called.
  • The depsgraph receives the new scene time and is evaluated.
  • The frame_change_post() callback is called.

If that callback changed any data, the depsgraph is re-evaluated.

As you can see, in the pre-handler the depsgraph hasn't been updated for the new frame yet (hence the 'pre'). This can be used to change things around before the depsgraph is evaluated.

I agree that it is a good idea to explicitly pass the depsgraph here (also see #77086).

This is not the same as #71908. The difference between viewport & render animation is as follows. In the viewport the depsgraph is "active", and thus the updates by the animation system are written back to the original datablocks. For rendering a new depsgraph is made (because all kinds of things have to be evaluated differently), which as a result is not marked as "active", and thus animation updates are not written back to the original datablocks. The problem here is that `bpy.context` will give you the viewport depsgraph, and not the render depsgraph. It'll thus always return the values as are currently seen in the viewport. As can be seen in `BKE_scene_graph_update_for_newframe()` (in `scene.c`), updating the scene for a new frame happens as follows: - Prior to calling `BKE_scene_graph_update_for_newframe()`, the scene's current frame is updated to the new frame. - The `frame_change_pre()` callback is called. - The depsgraph receives the new scene time and is evaluated. - The `frame_change_post()` callback is called. # If that callback changed any data, the depsgraph is re-evaluated. As you can see, in the pre-handler the depsgraph hasn't been updated for the new frame yet (hence the 'pre'). This can be used to change things around before the depsgraph is evaluated. I agree that it is a good idea to explicitly pass the depsgraph here (also see #77086).

Added subscriber: @Sergey

Added subscriber: @Sergey

I agree that it is a good idea to explicitly pass the depsgraph here

Discussed with @Sergey, and this is not possible. The handler is not to be used as a "before the frame changes" handler, but as a "before the depsgraph updates". This means it can be used for adjusting the data & relations for the new frame. The depsgraph itself is intentionally not available, as it's out of date (i.e. it's not in sync with either the previous or the current frame).

I'll see this as a documentation task, as the code is working as intended.

> I agree that it is a good idea to explicitly pass the depsgraph here Discussed with @Sergey, and this is not possible. The handler is not to be used as a "before the frame changes" handler, but as a "before the depsgraph updates". This means it can be used for adjusting the data & relations for the new frame. The depsgraph itself is intentionally not available, as it's out of date (i.e. it's not in sync with either the previous or the current frame). I'll see this as a documentation task, as the code is working as intended.

This issue was referenced by cdf0d95a50

This issue was referenced by cdf0d95a50e6ea5592e65242c3c8ffbdfd4629a0

Changed status from 'Confirmed' to: 'Resolved'

Changed status from 'Confirmed' to: 'Resolved'

Added subscriber: @barakooda

Added subscriber: @barakooda

This comment was removed by @barakooda

*This comment was removed by @barakooda*
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
5 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#77126
No description provided.