Maniphest T75352

'bpy.context.scene' not updated correctly when changed from the Properties panel context
Closed, Archived

Assigned To
Campbell Barton (campbellbarton)
Authored By
Christopher Gearhart (bblanimation)
Apr 3 2020, 6:09 PM
Tags
  • Python API
  • BF Blender (2.90)
Subscribers
Campbell Barton (campbellbarton)
Christopher Gearhart (bblanimation)

Description

System Information
Operating system: Windows 10
Graphics card: <irrelevant>

Blender Version
Broken: 2.82.7 (also broken in 2.79)
Worked: Never

Short description of error

When the active scene is changed by a script from the 'Properties' panel context, the bpy.context.scene property is not updated correctly. It seems to work fine from other contexts (e.g. the 'Text Editor' context, the '3D View' context, etc.)

Exact steps for others to reproduce the error

  • Open the default startup file
  • Run the following script:
import bpy

class TEST_OT_make_scene(bpy.types.Operator):
    bl_idname = "test.make_scene"
    bl_label = "Make Scene"
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        scn = bpy.data.scenes.new("My Scene")
        scn.world = bpy.data.worlds.new("My World")
        bpy.context.window.scene = scn
        print(bpy.context.window.scene.name, bpy.context.scene.name)
        self.report({"INFO"}, bpy.context.window.scene.name + " == " + bpy.context.scene.name)
        return {"FINISHED"}
        

class RENDER_PT_test_case(bpy.types.Panel):
    bl_space_type  = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_label       = "TEST CASE"
    bl_idname      = "RENDER_PT_test_case"
    bl_context     = "render"

    @classmethod
    def poll(self, context):
        return True

    def draw(self, context):
        self.layout.operator("test.make_scene")

        
bpy.utils.register_class(TEST_OT_make_scene)
bpy.utils.register_class(RENDER_PT_test_case)
  • Navigate to the 'Render' properties panel, and scroll down to the new tab titled "TEST CASE". Click on the operator. If it worked correctly, the names reported to the Blender UI should match.

Event Timeline

Christopher Gearhart (bblanimation) created this task.Apr 3 2020, 6:09 PM
Christopher Gearhart (bblanimation) added a project: Add-ons (BF-Blender).
Germano Cavalcante (mano-wii) edited projects, added BF Blender; removed Add-ons (Community).Apr 3 2020, 7:26 PM
Richard Antalik (ISS) changed the task status from Needs Triage to Confirmed.Apr 9 2020, 3:14 PM
Richard Antalik (ISS) updated the task description.
Christopher Gearhart (bblanimation) updated the task description.Apr 16 2020, 2:49 PM
Bastien Montagne (mont29) added a project: Python API.May 22 2020, 9:59 AM
Campbell Barton (campbellbarton) moved this task from Backlog to Responsibility of Other Project/Module on the Python API board.Jun 18 2020, 3:33 PM
Campbell Barton (campbellbarton) edited projects, added BF Blender (2.90); removed BF Blender.
Campbell Barton (campbellbarton) moved this task from Responsibility of Other Project/Module to Backlog on the Python API board.
Campbell Barton (campbellbarton) moved this task from Backlog to Bugs (API) on the Python API board.
Campbell Barton (campbellbarton) removed a project: Add-ons (BF-Blender).Jun 19 2020, 9:26 AM
Campbell Barton (campbellbarton) closed this task as Archived.Jun 19 2020, 10:39 AM
Campbell Barton (campbellbarton) claimed this task.
Campbell Barton (campbellbarton) added a subscriber: Campbell Barton (campbellbarton).

This is caused by the scene from context.scene being taken from the buttons window, which is only updated when it's refreshed.

Internally we would need to run buttons_context_compute on all space-types immediately, once the scene is set.

You can resolve this by setting the operator context not to use the properties area.

def draw(self, context):
    self.layout.operator_context = 'INVOKE_SCREEN'
    self.layout.operator("test.make_scene")

Closing since this is working as intended.