with context.temp_override(area=context.area) doesn't restore original area or region after with scope. #110632

Closed
opened 2023-07-30 16:58:39 +02:00 by chaos · 3 comments
Member

System Information
Operating system: Linux-5.4.0-149-generic-x86_64-with-glibc2.31 64 Bits, X11 UI
Graphics card: NVIDIA GeForce RTX 3080 Ti/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 525.116.04

Blender Version
Broken: version: 3.6.0, branch: blender-v3.6-release, commit date: 2023-06-27 08:08, hash: c7fc78b81ecb

Short description of error
The context-manager from context.temp_override doesn't restore the original area or region objects when those are the current ones in context. As result, the original context is invalidated. This doesn't happen for all kinds of data matching the original context, for example, the current window is correctly restored, and other data such as screen, space_data or active_object seems to be unaffected by this.

Exact steps for others to reproduce the error
Here's a code snippet to try this out.
Run this in the Text editor and then call the operator from the 3D View with F3 > test.test

import bpy

class TEST_OT_test(bpy.types.Operator):
    bl_idname = 'test.test'
    bl_label = 'Test'

    def invoke(self, context, event):
        print("Area before:", context.area)
        with context.temp_override(area=context.area):
            pass
        print("Area after:", context.area)
        return {'FINISHED'}

bpy.utils.register_class(TEST_OT_test)

When calling the operator, we get

Area before: <bpy_struct, Area at 0x7ff4def1b488>
Area after: None

if the script was written in this way:

        print("Area before:", context.area)
        context.temp_override(area=context.area)
        print("Area after:", context.area)

Then the output is expected:

Area before: <bpy_struct, Area at 0x7f0186d24448>
Area after: <bpy_struct, Area at 0x7f0186d24448>

The area gets invalidated, so from now on all script attempts to use context.area in the same invoke scope will fail.
As you can see, we are overriding the original context with its own data, which is the only exception to use temp_override. If we pass data which is different than the current context, everything's restored properly.

temp_override should restore or just plain ignore the overridden element when it matches the current one in context. This allows to programatically pass overrides without having to worry or check if the overridden data is part of the current context. This way, things like if area_override == context.area: # avoid calling temp_override can be safely ignored.

context.window works ok, while context.area and context.region don't.

**System Information** Operating system: Linux-5.4.0-149-generic-x86_64-with-glibc2.31 64 Bits, X11 UI Graphics card: NVIDIA GeForce RTX 3080 Ti/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 525.116.04 **Blender Version** Broken: version: 3.6.0, branch: blender-v3.6-release, commit date: 2023-06-27 08:08, hash: `c7fc78b81ecb` **Short description of error** The context-manager from `context.temp_override` doesn't restore the original `area` or `region` objects when those are the current ones in context. As result, the original context is invalidated. This doesn't happen for all kinds of data matching the original context, for example, the current `window` is correctly restored, and other data such as `screen`, `space_data` or `active_object` seems to be unaffected by this. **Exact steps for others to reproduce the error** Here's a code snippet to try this out. Run this in the Text editor and then call the operator from the 3D View with F3 > test.test ``` import bpy class TEST_OT_test(bpy.types.Operator): bl_idname = 'test.test' bl_label = 'Test' def invoke(self, context, event): print("Area before:", context.area) with context.temp_override(area=context.area): pass print("Area after:", context.area) return {'FINISHED'} bpy.utils.register_class(TEST_OT_test) ``` When calling the operator, we get ``` Area before: <bpy_struct, Area at 0x7ff4def1b488> Area after: None ``` if the script was written in this way: ``` print("Area before:", context.area) context.temp_override(area=context.area) print("Area after:", context.area) ``` Then the output is expected: ``` Area before: <bpy_struct, Area at 0x7f0186d24448> Area after: <bpy_struct, Area at 0x7f0186d24448> ``` The area gets invalidated, so from now on all script attempts to use `context.area` in the same invoke scope will fail. As you can see, we are overriding the original context with its own data, which is the only exception to use temp_override. If we pass data which is different than the current context, everything's restored properly. `temp_override` should restore or just plain ignore the overridden element when it matches the current one in context. This allows to programatically pass overrides without having to worry or check if the overridden data is part of the current context. This way, things like `if area_override == context.area: # avoid calling temp_override` can be safely ignored. `context.window` works ok, while `context.area` and `context.region` don't.
chaos added the
Type
Report
Priority
Normal
Status
Needs Triage
labels 2023-07-30 16:58:40 +02:00
YimingWu changed title from context.temp_override doesn't restore 'area' or 'region' when provided with its own context to `with context.temp_override(area=context.area) doesn't restore 'area' or 'region' after `with` scope. 2023-07-31 04:59:17 +02:00
YimingWu changed title from `with context.temp_override(area=context.area) doesn't restore 'area' or 'region' after `with` scope. to `with context.temp_override(area=context.area)` doesn't restore original `area` or `region` after `with` scope. 2023-07-31 04:59:37 +02:00
Member

I don't know python that well to see if the with keyword is behaving as expected, but it feels like a bug to me.

I don't know python that well to see if the `with` keyword is behaving as expected, but it feels like a bug to me.
YimingWu added
Module
Python API
Status
Confirmed
and removed
Status
Needs Triage
labels 2023-07-31 05:02:58 +02:00
Author
Member

@ChengduLittleA Sorry if I sound rude, but in my opinion, if you weren't sure about what the with statement does, you shouldn't have edited the report that way.

print("Area before:", context.area)
context.temp_override(area=context.area)
print("Area after:", context.area)

This part of the code doesn't make sense, since this way you are not really making use of the context-manager.
temp_override returns the context-manager object containing the __enter__ and __exit__ methods which are automatically called by the with statement to open and close a certain context throughout the duration of the with scope.

This is certainly not a problem about the with statement, which is being used canonically here.
The problem relies on what the context-manager object does on __exit__
Here's an example showing the same symptom without having to use the with statement. We get the same problem as before.

print("Area before:", context.area)
context_manager = context.temp_override(area=context.area)
context_manager.__enter__()
# do something inside the temporary context 
context_manager.__exit__()
print("Area after:", context.area)
Area before: <bpy_struct, Area at 0x7f5ba786ec48>
Area after: None
@ChengduLittleA Sorry if I sound rude, but in my opinion, if you weren't sure about what the `with` statement does, you shouldn't have edited the report that way. ``` print("Area before:", context.area) context.temp_override(area=context.area) print("Area after:", context.area) ``` This part of the code doesn't make sense, since this way you are not really making use of the context-manager. `temp_override` returns the context-manager object containing the `__enter__` and `__exit__` methods which are automatically called by the `with` statement to open and close a certain context throughout the duration of the `with` scope. This is certainly not a problem about the `with` statement, which is being used canonically here. The problem relies on what the context-manager object does on `__exit__` Here's an example showing the same symptom without having to use the `with` statement. We get the same problem as before. ``` print("Area before:", context.area) context_manager = context.temp_override(area=context.area) context_manager.__enter__() # do something inside the temporary context context_manager.__exit__() print("Area after:", context.area) ``` ``` Area before: <bpy_struct, Area at 0x7f5ba786ec48> Area after: None ```
Member

temp_override returns the context-manager object containing the enter and exit methods which are automatically called by the with statement to open and close a certain context throughout the duration of the with scope.

I see... Then the problem is now apparent, that __exit__() somehow did not run or failed to do the job. Thanks for clarifying!

> temp_override returns the context-manager object containing the __enter__ and __exit__ methods which are automatically called by the with statement to open and close a certain context throughout the duration of the with scope. I see... Then the problem is now apparent, that `__exit__()` somehow did not run or failed to do the job. Thanks for clarifying!
Blender Bot added
Status
Resolved
and removed
Status
Confirmed
labels 2023-08-09 08:53:28 +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
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#110632
No description provided.