window_manager.progress doesn't work after .invoke + wm.read_homefile #113051

Open
opened 2023-09-29 11:53:13 +02:00 by Andrej · 3 comments
Contributor

Blender 3.6.3

The usecase for this was

  1. User selects file they want to import
  2. We reset context and the entire session by using bpy.ops.wm.read_homefile because imported file is some other separate format, not .blend
  3. After bpy.ops.wm.read_homefile we're starting to import actual file and do stuff in new session.
  4. Since 3rd step is time consuming there was some progress indication implemented with bpy.context.window_manager.progress_begin, bpy.context.window_manager.progress_update, bpy.context.window_manager.progress_end.
    The issue is that progress doesn't seem to work in this case and it doesn't add display progress percent at the user's cursor (below is an example how it usually displays it).
    image

Steps to reproduce the issue.

  1. Run the code below in default blender session.

  2. Go to modifiers and try to run "test with handler" or "test w/o" handler.

  3. Select some other file in the system, it doesn't matter which.

  4. There will be 3 seconds loading during which you won't be able see the progress value on the cursor which is the issue.

  5. In real life example of this code since wm.read_homefile purges the context, I use load_handler the way described here. But this issue is unrelated to handlers as you can see running both buttons on step 2.

  6. Comment out the entire operator's invoke method so it would go straight to .execute() and the issue is gone. So issue seems to be related to operator's invoke or operator running modal.

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

class TestOperator(bpy.types.Operator):
    bl_idname = "object.test_operator"
    bl_label = "Test Operator"
    bl_options = {"REGISTER", "UNDO"}

    use_handler: bpy.props.BoolProperty()

    def do_stuff(self):
        bpy.context.window_manager.progress_begin(0, 100)
        steps = 3
        for i in range(steps):
            bpy.context.window_manager.progress_update(int(i/steps*100))
            sleep(1)
        bpy.context.window_manager.progress_end()

    def execute(self, context):
        @persistent
        def load_handler(*args):
            bpy.app.handlers.load_post.remove(load_handler)
            # do stuff after default file was opened
            self.do_stuff()

        if self.use_handler:
            bpy.app.handlers.load_post.append(load_handler)
            bpy.ops.wm.read_homefile()
        else:
            bpy.ops.wm.read_homefile()
            self.do_stuff()
        return {"FINISHED"}
    
    def invoke(self, context, event):
        context.window_manager.fileselect_add(self)
        return {"RUNNING_MODAL"}


class ExampleModifierPanel(bpy.types.Panel):
    bl_label = "Example Modifier"
    bl_idname = "OBJECT_PT_example_modifier"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "modifier"

    def draw(self, context):
        layout = self.layout
        box = layout.box()
        box.operator("object.test_operator", text="test with handler").use_handler = True
        box.operator("object.test_operator", text="test w/o handler").use_handler = False

def register():
    bpy.utils.register_class(ExampleModifierPanel)
    bpy.utils.register_class(TestOperator)

def unregister():
    bpy.utils.unregister_class(ExampleModifierPanel)
    bpy.utils.unregister_class(TestOperator)


if __name__ == "__main__":
    register()
Blender 3.6.3 The usecase for this was 1) User selects file they want to import 2) We reset context and the entire session by using `bpy.ops.wm.read_homefile` because imported file is some other separate format, not .blend 3) After `bpy.ops.wm.read_homefile` we're starting to import actual file and do stuff in new session. 4) Since 3rd step is time consuming there was some progress indication implemented with `bpy.context.window_manager.progress_begin`, `bpy.context.window_manager.progress_update`, `bpy.context.window_manager.progress_end`. The issue is that progress doesn't seem to work in this case and it doesn't add display progress percent at the user's cursor (below is an example how it usually displays it). ![image](/attachments/443566ea-2f47-4ff5-ab49-41186135eb5f) Steps to reproduce the issue. 1. Run the code below in default blender session. 2. Go to modifiers and try to run "test with handler" or "test w/o" handler. 3. Select some other file in the system, it doesn't matter which. 4. There will be 3 seconds loading during which you won't be able see the progress value on the cursor which is the issue. 5. In real life example of this code since `wm.read_homefile` purges the context, I use `load_handler` the way described [here](https://blender.stackexchange.com/questions/51494/how-to-bring-the-context-back-after-running-read-homefile/). But this issue is unrelated to handlers as you can see running both buttons on step 2. 6. Comment out the entire operator's `invoke` method so it would go straight to `.execute()` and the issue is gone. So issue seems to be related to operator's invoke or operator running modal. ```python import bpy import os from time import sleep from bpy.app.handlers import persistent class TestOperator(bpy.types.Operator): bl_idname = "object.test_operator" bl_label = "Test Operator" bl_options = {"REGISTER", "UNDO"} use_handler: bpy.props.BoolProperty() def do_stuff(self): bpy.context.window_manager.progress_begin(0, 100) steps = 3 for i in range(steps): bpy.context.window_manager.progress_update(int(i/steps*100)) sleep(1) bpy.context.window_manager.progress_end() def execute(self, context): @persistent def load_handler(*args): bpy.app.handlers.load_post.remove(load_handler) # do stuff after default file was opened self.do_stuff() if self.use_handler: bpy.app.handlers.load_post.append(load_handler) bpy.ops.wm.read_homefile() else: bpy.ops.wm.read_homefile() self.do_stuff() return {"FINISHED"} def invoke(self, context, event): context.window_manager.fileselect_add(self) return {"RUNNING_MODAL"} class ExampleModifierPanel(bpy.types.Panel): bl_label = "Example Modifier" bl_idname = "OBJECT_PT_example_modifier" bl_space_type = 'PROPERTIES' bl_region_type = 'WINDOW' bl_context = "modifier" def draw(self, context): layout = self.layout box = layout.box() box.operator("object.test_operator", text="test with handler").use_handler = True box.operator("object.test_operator", text="test w/o handler").use_handler = False def register(): bpy.utils.register_class(ExampleModifierPanel) bpy.utils.register_class(TestOperator) def unregister(): bpy.utils.unregister_class(ExampleModifierPanel) bpy.utils.unregister_class(TestOperator) if __name__ == "__main__": register() ```
Andrej added the
Priority
Normal
Type
Report
Status
Needs Triage
labels 2023-09-29 11:53:13 +02:00
Author
Contributor

And perhaps a bit similar issue - window_manager.progress also doesn't work if you start it from --python-expr. Which would also useful since I've associated some file format with Blender python expression and progress would indiciate the loading progress. I guess other usecase for this is starting render from command line and being able to see it's progress.

It can reproduced with this rather hacky example (usually you would just call operator that does that under the hood).

blender --python-expr "import bpy; from time import sleep; bpy.context.window_manager.progress_begin(0, 100); steps = 10; list(map(lambda i: (bpy.context.window_manager.progress_update(int(i/steps*100)), sleep(1)), range(steps))); bpy.context.window_manager.progress_end();"
And perhaps a bit similar issue - `window_manager.progress` also doesn't work if you start it from `--python-expr`. Which would also useful since I've associated some file format with Blender python expression and `progress` would indiciate the loading progress. I guess other usecase for this is starting render from command line and being able to see it's progress. It can reproduced with this rather hacky example (usually you would just call operator that does that under the hood). ``` blender --python-expr "import bpy; from time import sleep; bpy.context.window_manager.progress_begin(0, 100); steps = 10; list(map(lambda i: (bpy.context.window_manager.progress_update(int(i/steps*100)), sleep(1)), range(steps))); bpy.context.window_manager.progress_end();" ```

The progress cursor display is fragile as it depends on winactive which can become nullptr during an operator when any changes to the windows are made. When winactive is nullptr, the progress cursor is not displayed.

See the comment at:
https://projects.blender.org/blender/blender/blame/src/main/source/blender/makesdna/DNA_windowmanager_types.h#L130-L135

Also see https://projects.blender.org/blender/blender/src/branch/main/source/blender/makesrna/intern/rna_wm_api.cc#L191-L192

Perhaps this could be resolved/improved if we used context.window instead, but this code is old and would need to be carefully reviewed.

The progress cursor display is fragile as it depends on `winactive` which can become `nullptr` during an operator when any changes to the windows are made. When `winactive` is `nullptr`, the progress cursor is not displayed. See the comment at: https://projects.blender.org/blender/blender/blame/src/main/source/blender/makesdna/DNA_windowmanager_types.h#L130-L135 Also see https://projects.blender.org/blender/blender/src/branch/main/source/blender/makesrna/intern/rna_wm_api.cc#L191-L192 Perhaps this could be resolved/improved if we used `context.window` instead, but this code is old and would need to be carefully reviewed.
Germano Cavalcante added
Module
Python API
Status
Confirmed
and removed
Status
Needs Triage
labels 2023-10-02 18:35:40 +02:00
Author
Contributor

The progress cursor display is fragile as it depends on winactive which can become nullptr during an operator when any changes to the windows are made. When winactive is nullptr, the progress cursor is not displayed.

See the comment at:
https://projects.blender.org/blender/blender/blame/src/main/source/blender/makesdna/DNA_windowmanager_types.h#L130-L135

Also see https://projects.blender.org/blender/blender/src/branch/main/source/blender/makesrna/intern/rna_wm_api.cc#L191-L192

Perhaps this could be resolved/improved if we used context.window instead, but this code is old and would need to be carefully reviewed.

@mano-wii Is there some way to update winactive manually from the python code? Thought that perhaps screen update can help (bpy.ops.wm.redraw_timer(type="DRAW_WIN_SWAP", iterations=1)) but it didn't worked

> The progress cursor display is fragile as it depends on `winactive` which can become `nullptr` during an operator when any changes to the windows are made. When `winactive` is `nullptr`, the progress cursor is not displayed. > > See the comment at: > https://projects.blender.org/blender/blender/blame/src/main/source/blender/makesdna/DNA_windowmanager_types.h#L130-L135 > > Also see https://projects.blender.org/blender/blender/src/branch/main/source/blender/makesrna/intern/rna_wm_api.cc#L191-L192 > > Perhaps this could be resolved/improved if we used `context.window` instead, but this code is old and would need to be carefully reviewed. @mano-wii Is there some way to update `winactive` manually from the python code? Thought that perhaps screen update can help (`bpy.ops.wm.redraw_timer(type="DRAW_WIN_SWAP", iterations=1)`) but it didn't worked
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#113051
No description provided.