7

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

Crantisz
  • 35,244
  • 2
  • 37
  • 89
  • Related https://blender.stackexchange.com/questions/19668/execute-a-python-function-whenever-the-user-interacts-with-the-program where depsgraph update is akin to old scene update. – batFINGER Apr 02 '19 at 16:41
  • @batFINGER unfortunately, changing workspace doesn't cause depsgraph update event... – Crantisz Apr 03 '19 at 07:49
  • 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. https://blender.stackexchange.com/questions/126458/internal-get-set-function-of-property – batFINGER Apr 03 '19 at 10:34
  • 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)... – batFINGER Apr 03 '19 at 10:59
  • Of course, I can make a timer to check this every 0.1sec, but I think there is more performance method – Crantisz Apr 03 '19 at 11:01
  • Just remembed this too https://blender.stackexchange.com/a/110689/15543 – batFINGER Apr 03 '19 at 11:03
  • @batFINGER it's a bit easily in 2.80 https://docs.blender.org/api/blender2.8/bpy.app.timers.html – Crantisz Apr 03 '19 at 11:08
  • See https://blender.stackexchange.com/questions/135970/context-is-incorrect-when-calling-from-a-timer/135995#135995 Be interested to see how you go, quite likely in the timer thread context.workspace is None .. and overriding the context will be no use in this case. If not and it is like the window manager it will work. – batFINGER Apr 03 '19 at 11:13
  • Still intrigued Vote to close as unclear why you are asking Lol.. – batFINGER Apr 03 '19 at 11:29
  • @batFINGER i want to transfer some area's properties from one workspace to others – Crantisz Apr 03 '19 at 11:37
  • 2
    @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). – Crantisz Jan 24 '22 at 15:05

1 Answers1

12

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?

pyCod3R
  • 1,926
  • 4
  • 15
  • 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 – Crantisz Jan 23 '22 at 11:23
  • 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' – gogesite Apr 09 '23 at 05:14
  • 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? – seniva Apr 11 '23 at 10:57