Page MenuHome

Allow context override for workspace operators
ClosedPublic

Authored by Kai Jægersen (kaio) on Feb 17 2020, 2:29 AM.

Details

Summary

This is a patch in response to the thread on devtalk which talks about not being able to override the context for workspace operators.

Currently workspace operators don't look at the python context for a workspace. Instead, the fallback is always the window's active workspace, which means a context dictionary passed to bpy.ops.workspace operators is never used.

The patch changes the fallback from WM_window_get_active_workspace to CTX_wm_workspace which reads the workspace member.

Workspace lookup before:

  1. Find workspace by highlighted tab
  2. Find workspace by window

After:

  1. Find workspace by highlighted tab
  2. Find workspace by python context (new)

For operators using 1. this means no change, i.e the workspace tab under the cursor precedes any override. But when firing the operator from a script, an optional context dictionary will be read.

Tested override and and works:

  • bpy.ops.workspace.delete()
  • bpy.ops.workspace.duplicate()
  • bpy.ops.workspace.reorder_to_back()
  • bpy.ops.workspace.reorder_to_front()

Note: When an empty dict is passed to the operator, or the workspace field is None, the fallback seems to always be the workspace hook tied to the active window (same as before). Not sure if this is a benign side effect, didn't dig too much.

Included a simple blend to demonstrate the context override

which is essentially:

# We want to delete all workspaces except the active one.
import bpy

def remove_workspaces_except_active():
    this_ws = bpy.context.workspace

    for ws in bpy.data.workspaces:
        if ws != this_ws:
            bpy.ops.workspace.delete({'workspace': ws})

if __name__ == "__main__":
    remove_workspaces_except_active()

Diff Detail

Repository
rB Blender