context.scene not updated when context.screen.scene changed in an operator called from a panel. #41486

Closed
opened 2014-08-19 02:46:11 +02:00 by hudson barkley · 6 comments

System Information
Windows 7 64bit

Blender Version
Broken: 2.71
Worked: 2.70

Short description of error
In a python script, if a panel calls an operator, and that operator changes the scene by setting 'bpy.context.screen.scene', bpy.context.scene is not updated. This causes operators to run on the previous scene.

Exact steps for others to reproduce the error
The following script is as simple as I could make it, also see http://www.pasteall.org/53521/python
(sorry, couldn't get attached files to work)

import bpy
 
class TestPanel(bpy.types.Panel):
    bl_label = "Test Panel"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"
    def draw(self, context):
        layout = self.layout
        row = layout.row()
        row.operator('test.operator')
 
class TestOperator(bpy.types.Operator):
    bl_label = "Test Operator"
    bl_idname = 'test.operator'
    def execute(self, context):
        scene = bpy.data.scenes.new('test')
        bpy.context.screen.scene = scene
        print(bpy.context.scene)
        return{'FINISHED'}
         
def register():
    bpy.utils.register_module(__name__)
     
if __name__ == "__main__":
    register()
**System Information** Windows 7 64bit **Blender Version** Broken: 2.71 Worked: 2.70 **Short description of error** In a python script, if a panel calls an operator, and that operator changes the scene by setting 'bpy.context.screen.scene', bpy.context.scene is not updated. This causes operators to run on the previous scene. **Exact steps for others to reproduce the error** The following script is as simple as I could make it, also see http://www.pasteall.org/53521/python (sorry, couldn't get attached files to work) import bpy class TestPanel(bpy.types.Panel): bl_label = "Test Panel" bl_space_type = 'PROPERTIES' bl_region_type = 'WINDOW' bl_context = "scene" def draw(self, context): layout = self.layout row = layout.row() row.operator('test.operator') class TestOperator(bpy.types.Operator): bl_label = "Test Operator" bl_idname = 'test.operator' def execute(self, context): scene = bpy.data.scenes.new('test') bpy.context.screen.scene = scene print(bpy.context.scene) return{'FINISHED'} def register(): bpy.utils.register_module(__name__) if __name__ == "__main__": register()
Author

Changed status to: 'Open'

Changed status to: 'Open'
Author

Added subscriber: @snuq

Added subscriber: @snuq

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'
Campbell Barton self-assigned this 2014-08-19 07:03:58 +02:00

This is a quirk of how the API works, switching the scene is handled by a notifier, so the result isn't available immediately. Realize this can be problematic.

We could add some way for Python to handle notifiers while the script runs. (wrap wm_event_do_notifiers, eg context.window_manager.handle_notifiers()) .

But this is more an API limit then a bug.

This is a quirk of how the API works, switching the scene is handled by a notifier, so the result isn't available immediately. Realize this can be problematic. We could add some way for Python to handle notifiers while the script runs. (wrap `wm_event_do_notifiers`, eg `context.window_manager.handle_notifiers()`) . But this is more an API limit then a bug.
Author

If it's an api limitation, why did it work in 2.70? Also if that same operator is called from the console, it behaves as expected, it's only doing this when called from a panel

If it's an api limitation, why did it work in 2.70? Also if that same operator is called from the console, it behaves as expected, it's only doing this when called from a panel

Update, this example can be made to work by adding row.operator_context = 'INVOKE_SCREEN' before row.operator('test.operator')

This works because using INVOKE_SCREEN bypasses the buttons context which is default for panels, The buttons context doesn't get updated when assigning a variable to the screen. But the screen context does.

Update, this example can be made to work by adding `row.operator_context = 'INVOKE_SCREEN'` before `row.operator('test.operator')` This works because using `INVOKE_SCREEN` bypasses the `buttons` context which is default for panels, The buttons context doesn't get updated when assigning a variable to the screen. But the screen context does.
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#41486
No description provided.