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:

I found better solution with msgbus callbacks.

This will maximize view after workspace gets changed next time:

def maximize_next_workspace():
    owner = object()
    subscribe_to = bpy.types.Window, "workspace"

    def msgbus_callback(*args):
        for area in bpy.context.screen.areas:
            if area.type == 'VIEW_3D':
                with bpy.context.temp_override(area=area): # Blender 3.2 and above
                    bpy.ops.screen.screen_full_area()
        # Clear subscription
        bpy.msgbus.clear_by_owner(owner)

    # Subscribe to workspace changes
    bpy.msgbus.subscribe_rna(
        key=subscribe_to,
        owner=owner,
        args=(1, 2, 3),
        notify=msgbus_callback,
    )

For example:

# Maximize after workspace is changed
maximize_next_workspace()

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

Message bus: https://docs.blender.org/api/current/bpy.msgbus.html
Blender 3.2 temp_override: https://wiki.blender.org/wiki/Reference/Release_Notes/3.2/Python_API