bpy.context.temp_override - TypeError: Area not found in screen #108763

Closed
opened 2023-06-08 15:37:46 +02:00 by Andrej · 4 comments
Contributor

Blender 3.5.1

When I try to run bpy.context.temp_override with area that's not present on current screen I get the error below.
It's useful feature that helps to run operators without switching screen.

Tried the same thing with old context override workflow like bpy.ops.node.clipboard_copy(temp_override) and it works without errors.

# Error: Python: Traceback (most recent call last):
# File "test_nodes_copy.blend\error", line 37, in <module>
# File "test_nodes_copy.blend\error", line 25, in copy_node_graph_to_active_object
# TypeError: Area not found in screen

ways to reproduce

I've attached .blend file with the setup and code below that's causing the error - to reproduce you can open it and just play the "error" script from "Scripting". There is also "working" script that using old context override and it works without errors.

Code that causes error (the code itself is copying node graph from one material to another - haven't found other method to do so besides calling operators on areas from other screen):

import bpy

def get_shader_editor_context():
    for screen in bpy.data.screens:
        for area in screen.areas:
            if area.type == "NODE_EDITOR":
                for space in area.spaces:
                    if space.tree_type == "ShaderNodeTree":
                        context_override = {"area": area, "space": space, "screen": screen}
                        return context_override

def copy_node_graph_to_active_object(material):
    temp_override = get_shader_editor_context()
    old_material = bpy.context.object.active_material
    new_material = material
    
    # remove all nodes from the current material
    for n in old_material.node_tree.nodes[:]:
        old_material.node_tree.nodes.remove(n)
    
    # change current material and make other window's shader editor is updated
    bpy.context.object.active_material = new_material
    temp_override['space'].node_tree = new_material.node_tree
    
    with bpy.context.temp_override(**temp_override):
        # select all nodes and copy them to clipboard
        for node in new_material.node_tree.nodes:
            node.select = True
        bpy.ops.node.clipboard_copy()

        # back to original material
        bpy.context.object.active_material = old_material
        temp_override['space'].node_tree = old_material.node_tree
        bpy.ops.node.clipboard_paste(offset=(0,0))


copy_node_graph_to_active_object(bpy.data.materials['test'])

Deprecated temp context override method that's working:

import bpy

def get_shader_editor_context():
    for screen in bpy.data.screens:
        for area in screen.areas:
            if area.type == "NODE_EDITOR":
                for space in area.spaces:
                    if space.tree_type == "ShaderNodeTree":
                        context_override = {"area": area, "space": space, "screen": screen}
                        return context_override

def copy_node_graph_to_active_object(material):
    temp_override = get_shader_editor_context()
    old_material = bpy.context.object.active_material
    new_material = material
    
    # remove all nodes from the current material
    for n in old_material.node_tree.nodes[:]:
        old_material.node_tree.nodes.remove(n)
    
    # change current material and make other window's shader editor is updated
    bpy.context.object.active_material = new_material
    temp_override['space'].node_tree = new_material.node_tree
    
    # select all nodes and copy them to clipboard
    for node in new_material.node_tree.nodes:
        node.select = True
    bpy.ops.node.clipboard_copy(temp_override)

    # back to original material
    bpy.context.object.active_material = old_material
    temp_override['space'].node_tree = old_material.node_tree
    bpy.ops.node.clipboard_paste(temp_override, offset=(0,0))


copy_node_graph_to_active_object(bpy.data.materials['test'])
Blender 3.5.1 When I try to run `bpy.context.temp_override` with area that's not present on current screen I get the error below. It's useful feature that helps to run operators without switching screen. Tried the same thing with old context override workflow like `bpy.ops.node.clipboard_copy(temp_override)` and it works without errors. ``` # Error: Python: Traceback (most recent call last): # File "test_nodes_copy.blend\error", line 37, in <module> # File "test_nodes_copy.blend\error", line 25, in copy_node_graph_to_active_object # TypeError: Area not found in screen ``` ## ways to reproduce I've attached .blend file with the setup and code below that's causing the error - to reproduce you can open it and just play the "error" script from "Scripting". There is also "working" script that using old context override and it works without errors. Code that causes error (the code itself is copying node graph from one material to another - haven't found other method to do so besides calling operators on areas from other screen): ```python import bpy def get_shader_editor_context(): for screen in bpy.data.screens: for area in screen.areas: if area.type == "NODE_EDITOR": for space in area.spaces: if space.tree_type == "ShaderNodeTree": context_override = {"area": area, "space": space, "screen": screen} return context_override def copy_node_graph_to_active_object(material): temp_override = get_shader_editor_context() old_material = bpy.context.object.active_material new_material = material # remove all nodes from the current material for n in old_material.node_tree.nodes[:]: old_material.node_tree.nodes.remove(n) # change current material and make other window's shader editor is updated bpy.context.object.active_material = new_material temp_override['space'].node_tree = new_material.node_tree with bpy.context.temp_override(**temp_override): # select all nodes and copy them to clipboard for node in new_material.node_tree.nodes: node.select = True bpy.ops.node.clipboard_copy() # back to original material bpy.context.object.active_material = old_material temp_override['space'].node_tree = old_material.node_tree bpy.ops.node.clipboard_paste(offset=(0,0)) copy_node_graph_to_active_object(bpy.data.materials['test']) ``` Deprecated temp context override method that's working: ```python import bpy def get_shader_editor_context(): for screen in bpy.data.screens: for area in screen.areas: if area.type == "NODE_EDITOR": for space in area.spaces: if space.tree_type == "ShaderNodeTree": context_override = {"area": area, "space": space, "screen": screen} return context_override def copy_node_graph_to_active_object(material): temp_override = get_shader_editor_context() old_material = bpy.context.object.active_material new_material = material # remove all nodes from the current material for n in old_material.node_tree.nodes[:]: old_material.node_tree.nodes.remove(n) # change current material and make other window's shader editor is updated bpy.context.object.active_material = new_material temp_override['space'].node_tree = new_material.node_tree # select all nodes and copy them to clipboard for node in new_material.node_tree.nodes: node.select = True bpy.ops.node.clipboard_copy(temp_override) # back to original material bpy.context.object.active_material = old_material temp_override['space'].node_tree = old_material.node_tree bpy.ops.node.clipboard_paste(temp_override, offset=(0,0)) copy_node_graph_to_active_object(bpy.data.materials['test']) ```
Andrej added the
Priority
Normal
Type
Report
Status
Needs Triage
labels 2023-06-08 15:37:47 +02:00
Andrej changed title from bpy.context.temp_override to bpy.context.temp_override - TypeError: Area not found in screen 2023-06-08 16:54:03 +02:00
Iliya Katushenock added the
Interest
Python API
label 2023-06-08 18:23:53 +02:00
Member

Thanks for the report. I can confirm

Thanks for the report. I can confirm
Pratik Borhade added
Module
Python API
Status
Confirmed
and removed
Status
Needs Triage
labels 2023-06-09 05:32:51 +02:00
Author
Contributor

Any progress on the issue?

It will be critical on Blender 4.0 release since there is no other way to copy shader node graph from one shader to another besides using bpy.ops.node.clipboard_copy(temp_override) and bpy.ops.node.clipboard_paste(temp_override) and it will stop working since bpy.context.temp_override is unable to replace it.

Also met the same error with bpy.ops.console.scrollback_append

import bpy

def get_console_context():
    screen = bpy.data.screens['Scripting']
    area = next(area for area in screen.areas if area.type == "CONSOLE")
    context_override = {"area": area}
    return context_override

context = get_console_context()
bpy.ops.console.scrollback_append(context, text='test_deprecated', type='ERROR')    

with bpy.context.temp_override(**context):
    bpy.ops.console.scrollback_append(text='test_temp_override', type='ERROR')    
Any progress on the issue? It will be critical on Blender 4.0 release since there is no other way to copy shader node graph from one shader to another besides using `bpy.ops.node.clipboard_copy(temp_override)` and `bpy.ops.node.clipboard_paste(temp_override)` and it will stop working since `bpy.context.temp_override` is unable to replace it. Also met the same error with `bpy.ops.console.scrollback_append` ```python import bpy def get_console_context(): screen = bpy.data.screens['Scripting'] area = next(area for area in screen.areas if area.type == "CONSOLE") context_override = {"area": area} return context_override context = get_console_context() bpy.ops.console.scrollback_append(context, text='test_deprecated', type='ERROR') with bpy.context.temp_override(**context): bpy.ops.console.scrollback_append(text='test_temp_override', type='ERROR') ```
Author
Contributor

Tried as a workaround to change workspace before the temp_override but it didn't worked and crashed Blender with EXCEPTION_ACCESS_VIOLATION (probably because workspace doesn't update until script is finished).

To reproduce you can run the code below in default blender scene.

import bpy

def get_workspace_from_screen(screen):
    for workspace in bpy.data.workspaces:
        if screen in workspace.screens[:]:
            return workspace

def get_shader_editor_context():
    for screen in bpy.data.screens:
        for area in screen.areas:
            if area.type == "NODE_EDITOR":
                for space in area.spaces:
                    if space.tree_type == "ShaderNodeTree":
                        context_override = {
                            "area": area, 
                            "space": space, 
                            "screen": screen, 
                        }
                        return context_override

temp_override = get_shader_editor_context()
bpy.context.window.workspace = get_workspace_from_screen(temp_override["screen"])
print(bpy.context.window.workspace) # doesn't get updated until script is finished
with bpy.context.temp_override(**temp_override):
    pass
Tried as a workaround to change workspace before the `temp_override` but it didn't worked and crashed Blender with `EXCEPTION_ACCESS_VIOLATION` (probably because workspace doesn't update until script is finished). To reproduce you can run the code below in default blender scene. ```python import bpy def get_workspace_from_screen(screen): for workspace in bpy.data.workspaces: if screen in workspace.screens[:]: return workspace def get_shader_editor_context(): for screen in bpy.data.screens: for area in screen.areas: if area.type == "NODE_EDITOR": for space in area.spaces: if space.tree_type == "ShaderNodeTree": context_override = { "area": area, "space": space, "screen": screen, } return context_override temp_override = get_shader_editor_context() bpy.context.window.workspace = get_workspace_from_screen(temp_override["screen"]) print(bpy.context.window.workspace) # doesn't get updated until script is finished with bpy.context.temp_override(**temp_override): pass ```

6af92e1360 resolves this, closing.

6af92e13607bde4cdaccba0f280e7a5219725cbf resolves this, closing.
Blender Bot added
Status
Archived
and removed
Status
Confirmed
labels 2023-12-06 11:20:04 +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
3 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#108763
No description provided.