Slower frame-change with many objects in the scenes collection #104798

Closed
opened 2023-02-16 01:25:43 +01:00 by Campbell Barton · 0 comments

Update: since 4.0 there is significantly less change in performance described in this report no matter the value of USE_SCENE_COLLECTION when the script runs. However the PR to resolve this (!104801) speeds up both cases.


When objects are in the scenes collection (instead of a sub-collection), changing frames using left/right keys is slower.

There is also a slowdown when animation playback restarts.

This occurs in 3.5x ab63fe9eab8fd01d4a763199442987191d105c55 and 3.4x release.

To reproduce the problem:

  • With a new file paste the script inlined at the bottom of the report into a text editor and run it.

  • Press Spacebar in the 3D viewport to play back animation.

  • Note the FPS (~13.1 FPS here).

  • Start again, doing the previous steps with USE_SCENE_COLLECTION = True in the script.

  • Note the FPS is much lower (~3.46 FPS here).


Explanation

  • This is an issue I ran into when investigating why objects in collections were being re-populated during animation playback in some files, see !104553.

  • The cause of the problem is the scene is tagged by the depsgraph (see: ANIMPLAY_FLAG_JUMPED). (note: the jumped flag is set when changing frame via arrow keys & when the animation restarts).

  • When ANIMPLAY_FLAG_JUMPED is set scene is tagged with ID_RECALC_FRAME_CHANGE.

  • This causes the scene to be copied (see blender::deg::deg_update_copy_on_write_datablock).

  • The scene->master_collection is copied (non recursively).

    • When scene->master_collection contains many objects, copying has a noticeable performance penalty.
    • When it only contains some collections, the problem isn't noticeable.

Other Notes

  • This is mainly noticeable when loading files pre 2.8x which load objects directly into scene->master_collection.
  • This report mentions animation playback speed because it's easily measurable, however many other operations trigger this (left/right arrow keys to change frames, every update when dragging scene number buttons "Sampling" (Render or Viewport) for e.g.).
  • In the test script the start and end frames are the same to so the playback is continually restarting to accentuate the problem and match the slowdown from changing frames via arrow keys.

This script adds many objects to the factory settings file, hides them, adds some keyframes to the Light and sets the 3D viewport full-screen, ready for animation playback.

import bpy

USE_SCENE_COLLECTION = False
OBJECT_NUMBER = 20_000

def objects_create(object_num):
    objects = []
    new = bpy.data.objects.new
    for i in range(object_num):
        objects.append(new(hex(i), None))
    return objects


def objects_collection_link(collection, objects):
    link = collection.objects.link
    for ob in objects:
        link(ob)


def main():
    from bpy import context

    bpy.ops.wm.read_homefile(use_factory_startup=True)

    if USE_SCENE_COLLECTION:
        collection = context.scene.collection
    else:
        collection = context.collection

    # Keyframe the lamps location, this is important as without this
    # the scene is not considered "animated".
    ob_lamp = bpy.data.objects["Light"]
    ob_lamp.keyframe_insert("location", frame=1)

    # The short frame range shows the problem most,
    # as this tags the scene to be tagged to update on frame-change.
    context.scene.frame_start = 1
    context.scene.frame_end = 1
    
    # Attempt max FPS.
    context.scene.render.fps = 1000

    objects = objects_create(OBJECT_NUMBER)
    objects_collection_link(collection, objects)
    
    # Hide everything, except the lamp.
    bpy.ops.object.select_all(action='SELECT')
    window = bpy.data.window_managers[0].windows[0]
    area = next(area for area in window.screen.areas if area.type == 'VIEW_3D')
    
    # Hide needs an 3D viewport (but select doesn't, hrm..).
    with context.temp_override(window=window, area=area):
        bpy.ops.object.hide_view_set(unselected=False)
    ob_lamp.hide_set(False)
    
    # Fullscreen 3D view (rule out slowdown from drawing other areas).
    with context.temp_override(window=window, area=area):
        bpy.ops.screen.screen_full_area()
    

if __name__ == "__main__":
    main()
Update: since 4.0 there is significantly less change in performance described in this report no matter the value of `USE_SCENE_COLLECTION` when the script runs. However the PR to resolve this (!104801) speeds up both cases. ---- When objects are in the scenes collection (instead of a sub-collection), changing frames using left/right keys is slower. There is also a slowdown when animation playback restarts. This occurs in 3.5x `ab63fe9eab8fd01d4a763199442987191d105c55` and 3.4x release. To reproduce the problem: - With a new file paste the script inlined at the bottom of the report into a text editor and run it. - Press Spacebar in the 3D viewport to play back animation. - Note the FPS (~13.1 FPS here). - Start again, doing the previous steps with `USE_SCENE_COLLECTION = True` in the script. - Note the FPS is much lower (~3.46 FPS here). --- #### Explanation - This is an issue I ran into when investigating why objects in collections were being re-populated during animation playback in some files, see !104553. - The cause of the problem is the scene is tagged by the depsgraph (see: `ANIMPLAY_FLAG_JUMPED`). _(note: the jumped flag is set when changing frame via arrow keys & when the animation restarts)._ - When `ANIMPLAY_FLAG_JUMPED` is set `scene` is tagged with `ID_RECALC_FRAME_CHANGE`. - This causes the scene to be copied (see `blender::deg::deg_update_copy_on_write_datablock`). - The `scene->master_collection` is copied (non recursively). - When `scene->master_collection` contains many objects, copying has a noticeable performance penalty. - When it only contains some collections, the problem isn't noticeable. #### Other Notes - This is mainly noticeable when loading files pre 2.8x which load objects directly into `scene->master_collection`. - This report mentions animation playback speed because it's easily measurable, however many other operations trigger this (left/right arrow keys to change frames, every update when dragging scene number buttons "Sampling" (Render or Viewport) for e.g.). - In the test script the start and end frames are the same to so the playback is continually restarting to accentuate the problem and match the slowdown from changing frames via arrow keys. --- This script adds many objects to the factory settings file, hides them, adds some keyframes to the Light and sets the 3D viewport full-screen, ready for animation playback. ```python import bpy USE_SCENE_COLLECTION = False OBJECT_NUMBER = 20_000 def objects_create(object_num): objects = [] new = bpy.data.objects.new for i in range(object_num): objects.append(new(hex(i), None)) return objects def objects_collection_link(collection, objects): link = collection.objects.link for ob in objects: link(ob) def main(): from bpy import context bpy.ops.wm.read_homefile(use_factory_startup=True) if USE_SCENE_COLLECTION: collection = context.scene.collection else: collection = context.collection # Keyframe the lamps location, this is important as without this # the scene is not considered "animated". ob_lamp = bpy.data.objects["Light"] ob_lamp.keyframe_insert("location", frame=1) # The short frame range shows the problem most, # as this tags the scene to be tagged to update on frame-change. context.scene.frame_start = 1 context.scene.frame_end = 1 # Attempt max FPS. context.scene.render.fps = 1000 objects = objects_create(OBJECT_NUMBER) objects_collection_link(collection, objects) # Hide everything, except the lamp. bpy.ops.object.select_all(action='SELECT') window = bpy.data.window_managers[0].windows[0] area = next(area for area in window.screen.areas if area.type == 'VIEW_3D') # Hide needs an 3D viewport (but select doesn't, hrm..). with context.temp_override(window=window, area=area): bpy.ops.object.hide_view_set(unselected=False) ob_lamp.hide_set(False) # Fullscreen 3D view (rule out slowdown from drawing other areas). with context.temp_override(window=window, area=area): bpy.ops.screen.screen_full_area() if __name__ == "__main__": main() ```
Campbell Barton added the
Priority
Normal
Status
Needs Triage
Type
Report
labels 2023-02-16 01:25:43 +01:00
Campbell Barton changed title from Slower frame-change with objects are in the scenes collection to Slower frame-change with many objects in the scenes collection 2023-02-16 01:28:58 +01:00
Richard Antalik added
Status
Confirmed
and removed
Status
Needs Triage
labels 2023-02-16 03:51:03 +01:00
Richard Antalik added the
Module
Core
Interest
Core
labels 2023-02-16 03:52:04 +01:00
Bastien Montagne added
Type
Bug
and removed
Type
Report
labels 2023-02-17 10:03:51 +01:00
Bastien Montagne removed the
Interest
Core
label 2023-02-17 18:18:50 +01:00
Blender Bot added
Status
Resolved
and removed
Status
Confirmed
labels 2024-04-18 05:51:51 +02: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
1 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#104798
No description provided.