node.group_ungroup() operator fails to run with correct override context in headless Blender process #115367

Open
opened 2023-11-24 17:46:06 +01:00 by Ludvik Koutny · 12 comments
Contributor

System Information
Operating system: Windows-10-10.0.22621-SP0 64 Bits
Graphics card: NVIDIA GeForce RTX 2080 Ti/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 545.84

Blender Version
Broken: version: 4.0.0, branch: blender-v4.0-release, commit date: 2023-11-13 17:26, hash: 878f71061b8e
Worked: (newest version of Blender that worked as expected)

Short description of error
When running operators in headless blender mode like node.group_ungroup() with context override, the operator poll will fail despite the override context being correct.

Steps to reproduce the error

  1. Download and unzip MokeyBatch.zip
  2. Ensure that MonkeyBatch.blend, run_headless.py and batch.py are in the same folder
  3. Open the MonkeyBatch.blend and run run_headless.py from the text editor
  4. Observe the console output, it should print "RuntimeError: Operator bpy.ops.node.group_ungroup.poll() failed, context is incorrect"
  5. Switch text editor to Ungroup text datablock, which contains identical code to batch.py
  6. Run the script and notice the node.group_ungroup() operator works
    Result: The operator won't work in headless Blender process despite supplied context override being correct
    Expected: The operator runs correctly when the supplied context override is correct
**System Information** Operating system: Windows-10-10.0.22621-SP0 64 Bits Graphics card: NVIDIA GeForce RTX 2080 Ti/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 545.84 **Blender Version** Broken: version: 4.0.0, branch: blender-v4.0-release, commit date: 2023-11-13 17:26, hash: `878f71061b8e` Worked: (newest version of Blender that worked as expected) **Short description of error** When running operators in headless blender mode like node.group_ungroup() with context override, the operator poll will fail despite the override context being correct. **Steps to reproduce the error** 1. Download and unzip MokeyBatch.zip 2. Ensure that MonkeyBatch.blend, run_headless.py and batch.py are in the same folder 3. Open the MonkeyBatch.blend and run run_headless.py from the text editor 4. Observe the console output, it should print "RuntimeError: Operator bpy.ops.node.group_ungroup.poll() failed, context is incorrect" 5. Switch text editor to Ungroup text datablock, which contains identical code to batch.py 6. Run the script and notice the node.group_ungroup() operator works Result: The operator won't work in headless Blender process despite supplied context override being correct Expected: The operator runs correctly when the supplied context override is correct
Ludvik Koutny added the
Priority
Normal
Type
Report
Status
Needs Triage
labels 2023-11-24 17:46:07 +01:00
Iliya Katushenock added the
Interest
Nodes & Physics
Interest
Overrides
labels 2023-11-24 17:55:47 +01:00
Member

The script only fails on the last line for me where the context isn't overridden, which I would have expected since the current context will be the Text Editor. The operator call within the context override on line 26 works fine.

The script only fails on the last line for me where the context isn't overridden, which I would have expected since the current context will be the Text Editor. The operator call within the context override on line 26 works fine.
Author
Contributor

The script only fails on the last line for me where the context isn't overridden, which I would have expected since the current context will be the Text Editor. The operator call within the context override on line 26 works fine.

Huh, you are totally right... I hastily put this file together because bug reports require simplified examples, so I missed this. I can make it work in the simplified file, but I still can't make it work in my addon which runs headless Blender :(

I know this is probably too much to ask but could you by any chance figure out what I am doing wrong in my addon? The addon is relatively small (about 400 lines out of which only about 50 are relevant to this). I am even willing to pay you for your time. I am doing literally the same thing as in this small repro file, but in my addon it doesn't work.

I am attaching the addon file, and if you install the addon (feel free to check the source code beforehand), then you can just open the .blend file in Monkeys.zip, set the bake output path on the sidebar panel to a valid folder, and click the "Bake Collection PBR" button in the LK Bake Toolkit sidebar. It will call very short and simple LK_BAKE_TOOLKIT_OT_submit_bake operator in init.py which launches blender instance which runs bake.py where the culprit code is on lines 19-45.

Thanks in advance.

> The script only fails on the last line for me where the context isn't overridden, which I would have expected since the current context will be the Text Editor. The operator call within the context override on line 26 works fine. Huh, you are totally right... I hastily put this file together because bug reports require simplified examples, so I missed this. I can make it work in the simplified file, but I still can't make it work in my addon which runs headless Blender :( I know this is probably too much to ask but could you by any chance figure out what I am doing wrong in my addon? The addon is relatively small (about 400 lines out of which only about 50 are relevant to this). I am even willing to pay you for your time. I am doing literally the same thing as in this small repro file, but in my addon it doesn't work. I am attaching the addon file, and if you install the addon (feel free to check the source code beforehand), then you can just open the .blend file in Monkeys.zip, set the bake output path on the sidebar panel to a valid folder, and click the "Bake Collection PBR" button in the LK Bake Toolkit sidebar. It will call very short and simple LK_BAKE_TOOLKIT_OT_submit_bake operator in __init__.py which launches blender instance which runs bake.py where the culprit code is on lines 19-45. Thanks in advance.
Author
Contributor

I am getting even more confused here.

When launching Blender in headless mode, the exact same script will fail the first time and then run fine the second time. I am slowly going crazy.

Just running the identical script second time works... O_o

EDIT: It appears to work when the supplied NODE_EDITOR area already exists in the UI, but it will fail if other area is converted to NODE_EDITOR area during the same operation 🤔

I am getting even more confused here. When launching Blender in headless mode, the exact same script will fail the first time and then run fine the second time. I am slowly going crazy. <video src="/attachments/9ffdccfa-a317-4606-9db2-f4ff87210611" title="2023-11-24_21-20-42.mp4" controls></video> Just running the identical script second time works... O_o EDIT: It appears to work when the supplied NODE_EDITOR area already exists in the UI, but it will fail if other area is converted to NODE_EDITOR area during the same operation 🤔
Ludvik Koutny changed title from node.group_ungroup() operator doesn't work with context override to context override fails on some operators if executed too soon 2023-11-24 22:04:42 +01:00

At first glance, it doesn't look like a bug. You are changing the area type in:
area.type = 'NODE_EDITOR'

It's important to request a UI update to ensure that this change takes effect.

Does this make sense to you?

At first glance, it doesn't look like a bug. You are changing the area type in: `area.type = 'NODE_EDITOR'` It's important to request a UI update to ensure that this change takes effect. Does this make sense to you?
Germano Cavalcante added
Status
Needs Information from User
and removed
Status
Needs Triage
labels 2023-11-25 14:20:44 +01:00
Author
Contributor

At first glance, it doesn't look like a bug. You are changing the area type in:
area.type = 'NODE_EDITOR'

It's important to request a UI update to ensure that this change takes effect.

Does this make sense to you?

Not sure, I would like to try it and see if it works? What's the proper way to request UI update? There doesn't seem to be any documentation on it. I can make it work if I spawn blender process with UI, but once I pass the '-b' argument, to run Blender without UI, even that bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1) workaround stops working.

> At first glance, it doesn't look like a bug. You are changing the area type in: > `area.type = 'NODE_EDITOR'` > > It's important to request a UI update to ensure that this change takes effect. > > Does this make sense to you? Not sure, I would like to try it and see if it works? What's the proper way to request UI update? There doesn't seem to be any documentation on it. I can make it work if I spawn blender process with UI, but once I pass the '-b' argument, to run Blender without UI, even that bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1) workaround stops working.

What's the proper way to request UI update?

I don't remember off the top of my head. scene.update or something like that...
But generally having to change the interface to execute a command is not conventinal (this is more common to test something).

For assistance I suggest to ask on one of the community websites: https://www.blender.org/community/

> What's the proper way to request UI update? I don't remember off the top of my head. scene.update or something like that... But generally having to change the interface to execute a command is not conventinal (this is more common to test something). For assistance I suggest to ask on one of the community websites: https://www.blender.org/community/
Author
Contributor

What's the proper way to request UI update?

I don't remember off the top of my head. scene.update or something like that...
But generally having to change the interface to execute a command is not conventinal (this is more common to test something).

For assistance I suggest to ask on one of the community websites: https://www.blender.org/community/

I would like to close this too, if this is not a bug, but I still want to ensure it's not one, because, I really spent a lot of time trying various things and context override just won't work in headless Blender process, while the exact same code does work in regular Blender and even in spawned subprocess with UI enabled.

Just to be sure... if it wasn't possible to run bpy.ops.node.group_ungroup() operator in headless Blender (using the '-b' argument) under any circumstances, would that be considered a bug?

> > What's the proper way to request UI update? > > I don't remember off the top of my head. scene.update or something like that... > But generally having to change the interface to execute a command is not conventinal (this is more common to test something). > > For assistance I suggest to ask on one of the community websites: https://www.blender.org/community/ I would like to close this too, if this is not a bug, but I still want to ensure it's not one, because, I really spent a lot of time trying various things and context override just won't work in headless Blender process, while the exact same code does work in regular Blender and even in spawned subprocess with UI enabled. Just to be sure... if it wasn't possible to run bpy.ops.node.group_ungroup() operator in headless Blender (using the '-b' argument) under any circumstances, would that be considered a bug?
Ludvik Koutny changed title from context override fails on some operators if executed too soon to node.group_ungroup() operator fails to run with correct override context in headless Blender process 2023-11-25 15:56:06 +01:00
Author
Contributor

@mano-wii I have spent lots of time making really good repro file. I have updated the report description to be concise and I've uploaded a new package which should make it really easy to reproduce it.

I don't remember off the top of my head. scene.update or something like that...
But generally having to change the interface to execute a command is not conventinal (this is more common to test something).

Regarding this, do you have any better idea how to run node.group_ungroup() which requires node editor area to be run? I can't make assumption that there's a shader editor already existing somewhere in the UI as users can customize their startup .blend file.

@mano-wii I have spent lots of time making really good repro file. I have updated the report description to be concise and I've uploaded a new package which should make it really easy to reproduce it. > I don't remember off the top of my head. scene.update or something like that... > But generally having to change the interface to execute a command is not conventinal (this is more common to test something). Regarding this, do you have any better idea how to run node.group_ungroup() which requires node editor area to be run? I can't make assumption that there's a shader editor already existing somewhere in the UI as users can customize their startup .blend file.
Author
Contributor

I tried another approach, where I created a blend file that has only one workspace with one area, and that area is NODE_EDITOR. I then load this blender file during the startup of headless blender. The print(context.screen.areas[0].type) returns NODE_EDITOR so this area already exists, and is not being changed to from another area. I then do following:

        print(context.screen.areas[0].type)

        node_groups = [node for node in node_tree.nodes if node.type == 'GROUP']

        context_override = context.copy()

        context_override['area'] = context.screen.areas[0]
        context_override['region'] = context.screen.areas[0].regions[-1]
        context_override['space_data'] = context.screen.areas[0].spaces.active

        context_override['active_node'] = node_groups[0]
        context_override['material'] = material
        context_override['selected_nodes'] = node_groups

        with context.temp_override(**context_override):
            bpy.ops.node.group_ungroup()

And the result is same:
RuntimeError: Operator bpy.ops.node.group_ungroup.poll() failed, context is incorrect

This must be bugged... Even when I ensure the NODE_EDITOR area already exists, the operator still considers the context incorrect.

I tried another approach, where I created a blend file that has only one workspace with one area, and that area is NODE_EDITOR. I then load this blender file during the startup of headless blender. The print(context.screen.areas[0].type) returns NODE_EDITOR so this area already exists, and is not being changed to from another area. I then do following: ``` print(context.screen.areas[0].type) node_groups = [node for node in node_tree.nodes if node.type == 'GROUP'] context_override = context.copy() context_override['area'] = context.screen.areas[0] context_override['region'] = context.screen.areas[0].regions[-1] context_override['space_data'] = context.screen.areas[0].spaces.active context_override['active_node'] = node_groups[0] context_override['material'] = material context_override['selected_nodes'] = node_groups with context.temp_override(**context_override): bpy.ops.node.group_ungroup() ``` And the result is same: RuntimeError: Operator bpy.ops.node.group_ungroup.poll() failed, context is incorrect This must be bugged... Even when I ensure the NODE_EDITOR area already exists, the operator still considers the context incorrect.
Germano Cavalcante added
Status
Needs Triage
and removed
Status
Needs Information from User
labels 2023-11-25 17:09:04 +01:00

I have facing similar problem with group_ungroup! The issue is to make sure b61496c508/source/blender/editors/space_node/node_group.cc (L88) evaluted to be true

I have facing similar problem with group_ungroup! The issue is to make sure https://github.com/blender/blender/blob/b61496c5089260a5a35ed4863685ef072e7c5e79/source/blender/editors/space_node/node_group.cc#L88 evaluted to be true
Member

HI, thanks for the report. Can confirm. Instead of with context.temp_override(**context_override): following might work but not sure how correct this is:

for window in context.window_manager.windows:
    for area in window.screen.areas:
        if area.type == 'NODE_EDITOR':
            with context.temp_override(window=window, area=area):
                bpy.ops.node.group_ungroup()

@ideasman42 , can you check if the script mentioned in the report is valid/supported case?

HI, thanks for the report. Can confirm. Instead of `with context.temp_override(**context_override):` following might work but not sure how correct this is: ``` for window in context.window_manager.windows: for area in window.screen.areas: if area.type == 'NODE_EDITOR': with context.temp_override(window=window, area=area): bpy.ops.node.group_ungroup() ``` @ideasman42 , can you check if the script mentioned in the report is valid/supported case?
Member

Ah, my change won't work since the other instance is launched in background mode (no window) :)

It didn't throw the context error so I assumed it's working

Ah, my change won't work since the other instance is launched in background mode (no window) :) It didn't throw the context error so I assumed it's working
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#115367
No description provided.