7
$\begingroup$

I want to write a function, which will be called after changing the workspace.

I thought, that i can find workspace change event in bpy.app.handlers, but it looks like where is no such handler here… What can I do?

enter image description here

$\endgroup$
12
  • $\begingroup$ Related blender.stackexchange.com/questions/19668/… where depsgraph update is akin to old scene update. $\endgroup$
    – batFINGER
    Apr 2, 2019 at 16:41
  • $\begingroup$ @batFINGER unfortunately, changing workspace doesn't cause depsgraph update event... $\endgroup$
    – Crantisz
    Apr 3, 2019 at 7:49
  • $\begingroup$ Correct, the idea is it runs frequently enough to flag it. Similarly for A property with a getter fired on a UI draw or a modal timer. The property getter would be ideal way to fire when saved value has changed. blender.stackexchange.com/questions/126458/… $\endgroup$
    – batFINGER
    Apr 3, 2019 at 10:34
  • $\begingroup$ Out of interest... What do you have in mind here?, are you going to keep a history of visited workspaces? Could go whole hog on this question and fudge an on context change event comparing current, since context changes accordingly without which member being flagged, the context is what it is., against a repr value copy of all (or selected props to watch)... $\endgroup$
    – batFINGER
    Apr 3, 2019 at 10:59
  • 2
    $\begingroup$ @MartyFouts my question is more general, and it was asked earlier, and accepted answer in that question seems to be incorrect for me (because you can do, see the answer below). $\endgroup$
    – Crantisz
    Jan 24, 2022 at 15:05

1 Answer 1

11
+250
$\begingroup$

Use msgbus and subscribe to the workspace property of the window class: https://docs.blender.org/api/current/bpy.types.Window.html#bpy.types.Window.workspace

enter image description here

import bpy

handle = object()

# Triggers when window's workspace is changed
subscribe_to = bpy.types.Window, "workspace"

def notify_test(context):
    print(context.workspace.name)
    context.workspace.status_text_set("Hello: {}".format(context.workspace.name))

bpy.msgbus.subscribe_rna(
    key=subscribe_to,
    owner=handle,
    args=(bpy.context,),
    notify=notify_test,
)

bpy.msgbus.publish_rna(key=subscribe_to)

Output:

Layout
Scripting
Texture Paint
...

https://docs.blender.org/api/current/bpy.msgbus.html

How to get an event when an object is selected?

Msgbus - How to subscribe to bpy.context.scene?

$\endgroup$
3
  • $\begingroup$ After all these years, finally get the solution! Many thanks, I'll give you some more points, just need to wait 24 hours, because of site rules $\endgroup$
    – Crantisz
    Jan 23, 2022 at 11:23
  • $\begingroup$ Is it possible to have this automatically run at startup? This error occurs if trying to load it it at module registration time: AttributeError: '_RestrictContext' object has no attribute 'workspace' $\endgroup$
    – gogesite
    Apr 9 at 5:14
  • $\begingroup$ Using msgbus only seems to work when the user explicitly clicks a workspace name with the mouse. It does not work when using the keyboard shortcuts (It is called "Cycle Workspace" aka "screen.workspace_cycle" operation. Can the solution be extended to cover this? $\endgroup$
    – seniva
    Apr 11 at 10:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .