Page MenuHome

2.8: Python API: window's workspace can't be set in Application timer
Open, Confirmed, MediumPublic

Description

System Information
Operating system: Darwin-18.7.0-x86_64-i386-64bit 64 Bits
Graphics card: Intel HD Graphics 5000 OpenGL Engine Intel Inc. 4.1 INTEL-12.10.12

Blender Version
Broken: version: 2.82 (sub 1), branch: master (modified), commit date: 2019-10-31 23:45, hash: rBc11d3c1b9208
Worked: (optional)

Short description of error
In an app-level timer, trying to set the workspace on a window doesn't work. It looks like the call to WM_event_add_notifier in rna_Window_workspace_update always dispatches events to the context's current window rather than the Window object whose workspace attribute is being changed.

Exact steps for others to reproduce the error
Run this script in the Scripting workspace:

import bpy
def tick():
    bpy.context.window_manager.windows[0].workspace = bpy.data.workspaces['Shading']
    return 0
bpy.app.timers.register(tick)

Running the script does not switch workspaces to the Shading workspace. Calling the tick() function directly switches workspaces as expected.

Details

Type
Bug

Event Timeline

Philipp Oeser (lichtwerk) lowered the priority of this task from Needs Triage by Developer to Confirmed, Medium.Thu, Nov 7, 1:09 PM

Can confirm.

However you can use the workaround from T62074: 2.8: Python API: crash due to missing window context in Application timer which is using an operator with custom context in the timer. This seems to work nicely:

import bpy

class TestOperator(bpy.types.Operator):
    bl_idname = "scene.test_operator"
    bl_label = "Test Operator"

    def execute(self, context):
        context.window.workspace = bpy.data.workspaces['Shading']
        return {'FINISHED'}

bpy.utils.register_class(TestOperator)

def tick():
    window = bpy.context.window_manager.windows[0]
    ctx = {'window': window, 'screen': window.screen}  
    bpy.ops.scene.test_operator(ctx)
    
    return 5.0

bpy.app.timers.register(tick)
Philipp Oeser (lichtwerk) renamed this task from A window's workspace can't be set during app-level timer callbacks to 2.8: Python API: window's workspace can't be set in Application timer.Thu, Nov 7, 1:11 PM