1
$\begingroup$

I want to make a python script that allows me to change my workspace with a keypress, and I have a wacom tablet with buttons on it that I can map keys to it. I was thinking about mapping unused function keys to my tablet and having my python script listen for the key and change my workspace accordingly

(for example, the 3rd button on my tablet triggers f5 which a python script would listen for that and change my workspace to shaders)

Is there a specific command for changing the workspace in Python?

(Note: I am a noob at python, I use Lua more)

$\endgroup$
1
  • $\begingroup$ I have the same question. However, from the sparce documention I found online it seems to me the way would be to use bpy.ops.workspace.append_activate Now, if I try this with bpy.ops.workspace.append_activate(idname="Motion Tracking") nothing happens, and all I get in the Console is {'CANCELLED'}. Anyone knows what to do? $\endgroup$ – sebastian_k Jan 8 at 14:28
1
$\begingroup$

Here is a quick solution, which allows you to change the workspace from Python:

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

All the available workspaces are stored in bpy.data.workspaces, so you need to pick one from those.

Now it is just a matter of putting your favourite into a Python script and register it as an operator, so you can map a key to it :)

Good luck!

EDIT:

As promised, a simple addon file: github blender-workspace-swapper

It does not contain keymaps yet (to be fair, I am not sure how those are created for addons, maybe someone else could help?), but calling the operator via the operator search works.

Alternatively, calling it from the python line like this: bpy.ops.workspaceswapper.swap(targetWorkspace="Animation")

If the workspace does not exist, it will append it from the startup.blend in the config folder (make sure that one exists).

$\endgroup$
6
  • $\begingroup$ Ah sorry, I think my answer to your solution should have been posted as a comment. Thanks a lot, this works if the workspace is already present in bpy.data.workspaces. My question though is how to use one that would have to be created yet? So for example, if you start from the Video Editing template and want to switch to Motion Tracking workspace. $\endgroup$ – sebastian_k Jan 8 at 17:07
  • $\begingroup$ In that case, I guess we need to append the existing workspace from somewhere else first (e.g. the Blender default files, a curated preset file or one existing project file). After appending, we can then swap between the existing workspaces. Is there some bigger interest in this? I could create an Addon for that... $\endgroup$ – Mr. Anderson Jan 8 at 17:08
  • 1
    $\begingroup$ Well, that's the thing, in the factory_startup file all those workspaces are already there. So how can I address those? This seems so suggest the way to do it, but how exactly? docs.blender.org/api/current/… $\endgroup$ – sebastian_k Jan 8 at 17:19
  • $\begingroup$ Have managed to get it to work, but it's dinner time now ;) Posting soon! $\endgroup$ – Mr. Anderson Jan 8 at 17:33
  • 1
    $\begingroup$ The addon is amended with your ideas (FYI for everyone not following the Twitter chat :D) $\endgroup$ – Mr. Anderson Jan 9 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.