Maximize view after workspace change

How to change workspace and maximize view?

Running this code will change workspace to scripting, but print will still return old workspace, which I suspect is problem:

bpy.context.window.workspace = bpy.data.workspaces['Scripting']

print(bpy.context.window.workspace)
 # <bpy_struct, WorkSpace("Layout") at 0x0000008F3C2160C8>

This will maximize view, but only if I have manually opened workspace before:

bpy.context.window.workspace = bpy.data.workspaces['Scripting']
for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        override = {'area': area}
        bpy.ops.screen.screen_full_area(override)

This can force maximize view, but Blender crashes once I switch to Scripting view:

for area in bpy.data.workspaces['Scripting'].screens[0].areas:
    if area.type == 'TEXT_EDITOR':
        override = {'area': area}
        bpy.ops.screen.screen_full_area(override)
1 Like

I found some kind of solution with timers:

import bpy
import time
import functools

def maximize_area(start_time):
    # Wait for workspace change
    if time.time() - start_time < 5: # Wait max 5 seconds
        if bpy.context.window.workspace != bpy.data.workspaces['Scripting']:
            print('Retry')
            return 0.01 # retry in 0.01 s
        
    # Maximize text editor in new workspace
    for area in bpy.context.screen.areas:
        if area.type == 'TEXT_EDITOR':
            bpy.ops.screen.screen_full_area({'area': area})
            break
            
# Change workspace                 
bpy.context.window.workspace = bpy.data.workspaces['Scripting'] 

# Start timer to run maximize_area(time.time())
func = functools.partial(maximize_area, time.time())
bpy.app.timers.register(func, first_interval=0.01) # Run after 0.01 s

did you find any non timer solution?

I gave an example of how do to do this in your other thread: