How execute script with global shortcut?

Hi there,

can anyone help me out turning the following script into a piece of code so that the script can be executed by a globally working and defined keyboard shortcut?
(e.g. SPACE+CTRL+SHIFT)

So far those lines can be successfully be run in text editor, but I better would like to have it installed somehow as an addon.

Thanks in advance.

import bpy

if bpy.context.window.workspace == bpy.data.workspaces['Layout']:
    bpy.context.window.scene = bpy.data.scenes['Scene.001']

if bpy.context.window.workspace == bpy.data.workspaces['Sculpting']:
    bpy.context.window.scene = bpy.data.scenes['Scene.002']

I think you’ll first need to convert your script to an operator, before you can assign it a keyboard shortcut.

There’s some useful information in the “official” add-on tutorial:
https://docs.blender.org/manual/en/latest/advanced/scripting/addon_tutorial.html

There’s also some useful information in this thread on devtalk:
https://devtalk.blender.org/t/official-keymap-example-does-not-work/9032/6

You might also want to check whether your preferred keyboard shortcut is already in use. There’s an add-on “is key free” (included in blender) that allows you to search for a specific shortcut to see what it does already

Thank you very much for your contribution. Looks like I would have to dive deep into python programming, for what I’m not having resources at the moment, just to get back a useful blender feature which I could easily use in 2.79 but not in 2.80 anymore by default:

  automatic "workspace(f.k.a screen) dependent scene loading"

So, upgrading my 2D cutout animation tool and template suite named “Flattiefolks Animation Kit” (being available for free for everyone) from 2.79 to 2.80 & future blender versions won’t happen any soon or at all. :cry:

Any further help welcome.

maybe there is another way that I don’t know about. How did you achieve this in 2.79?

EDIT: Sorry, ignore that. I forgot how 2.79 works. (lol)
I guess you are referring to the way that in 2.79, each workspace could have a different scene open (and changing the scene in one would not change the scene in the others)?
And you want to use that behaviour in 2.8?

Hmm.
I’m not sure what the best way would be to achieve this in an add-on, but I think it could be done

1 Like

@Flattiefolks

This is just a rough 1st attempt (not finished), but if you run this script it approximately replicates the automatic scene-switching behaviour from 2.79 by using a modal operator.

NOTE: there are some problems, including

  • Currently no way to turn it off without closing the .blend file
  • the scenes are not saved in the .blend file
  • This seems to break the feature in 2.8* of automatic switching between different modes when changing workspaces (e.g. changing to sculpt mode when changing to the “sculpting” workspace)
  • While trying to test how it broke that ^, I managed to crash blender, so be careful
    EDIT: the crash was actually caused by a bug in blender, not caused by this script. I’ve reported the bug.

But if those problems could be solved, would something like this be useful for your problem?

import bpy

class ModalWorkspaceScene(bpy.types.Operator):
    bl_idname = "wm.modal_workspace_scene"
    bl_label = "Each Workspace Remembers A Scene"
    
    def __init__(self):
        print("Start")
        
    def __del__(self):
        print("End")
    
    def modal(self, context, event):
        if event.type == 'MOUSEMOVE':
            if not (self.lastWorkspace == context.workspace):
                self.keepingTrack[self.lastWorkspace] = context.scene
                if context.workspace in self.keepingTrack.keys():
                    context.window.scene = self.keepingTrack[context.workspace]
                else:
                    self.keepingTrack[context.workspace] = context.scene
                self.lastWorkspace = context.workspace
        
        return {'PASS_THROUGH'}
    
    def invoke(self, context, event):
        self.keepingTrack = {context.workspace:context.scene}
        self.lastWorkspace = context.workspace
        
        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}

def register():
    bpy.utils.register_class(ModalWorkspaceScene)

def unregister():
    bpy.utils.unregister_class(ModalWorkspaceScene)

if __name__ == '__main__':
    register()
    bpy.ops.wm.modal_workspace_scene('INVOKE_DEFAULT')

EDIT: there was an error in the last line which prevented the script from running. It previously read bpy.ops.object.modal_workspace_scene when it should have read bpy.ops.wm.modal_workspace_scene. I have fixed that now

1 Like

Thank you so much for helping me on.:slightly_smiling_face:
Impressive! This 1st attempt of yours goes absolutely in the right direction.

:triangular_flag_on_post:Please beware, before investing more of your efforts in this, I have to inform you that I am looking for a free solution that must become freely available for all blender users. That’s because we’re talking about a “Creative Commons Attribution Share­Alike 4.0 License” project I invented to give back my support & gratitude to the blender community. Thanks for your understanding.

So now if you’re further willing to support on finding a acceptable solution on the isssue,
let’s see what we’ve got:

  • a script for selecting workspaces via dropdown menu: :white_check_mark:
    2.80 / How switch scene from text editor?
  • scenes newly created saved in -blend file: :white_check_mark:
  • scenes permanently assigned to workspaces::negative_squared_cross_mark:
  • “assigning scenes to workspaces script” as blender addon or its integration in the first mentioned addon: :negative_squared_cross_mark:
  • bonus: storing & keeping modes (object mode, edit mode, etc. ) within workspace switching (but we also could live without that feature: :negative_squared_cross_mark::wink:

If there’s a chance to get this done, “Flattifolks Animation Kit” might become rescued for the future.:crossed_fingers:

I’m happy for my contributions to this project to be freely and publicly available, as long as they remain freely and publicly available.

To that end, I propose that any further code that I contribute to your project be published under GPLv3 license. (This also ensures that it complies with blender’s GPLv3 license)

As far as I can tell, GPLv3 and CC BY-SA 4 licenses are compatible so this should not be a problem.
https://wiki.creativecommons.org/wiki/ShareAlike_compatibility

Would this be acceptable to you?

Having found this thread, and having now downloaded and tried your animation kit, I think I understand your problem much better. :slight_smile: I understand now why it is so important for you to restore the 2.79 way of working to 2.8*

I am happy to help you solve your problem, and I think I know how to fix the last few issues…
I propose the following:

  1. a pair of python scripts which will read the screen/scene pairs from the 2.79-compatible blend-file, and then set up the same workspace/scene pairing in 2.8 using custom properties to store the scene name in each workspace. By storing the scene name in a custom property, the information will be saved in the .blend file, so this would only need to be set up once.

  2. Combining all of the scripts and work-arounds you have found into one add-on, which can be used to replicate the functionality of 2.79 (i.e. no workspace tabs, automatic scene-switching, and selecting workspaces from a drop-down menu.)
    This add-on could be published separately (for the benefit of others who want to use old 2.79 way of doing things), but also it could be bundled with your animation kit.

What do you think?

This is great news - very helpful and very kind of you, thank you!

I think your suggested approach seems to be exactly the right and reasonable
way to handle this & to achieve results from which all blender users could benefit,
no matter if they are into 2D, 3D animation or whatever when choosing blender.
(By all means you also shall be credited for work that’s done by you.)

“To that end, I propose that any further code that I contribute to your project be published under GPLv3 license. (This also ensures that it complies with blender’s GPLv3 license)

As far as I can tell, GLPv3 and CC BY-SA 4 licenses are compatible so this should not be a problem.

This add-on could be published separately (for the benefit of others who want to use old 2.79 way of doing things), but also it could be bundled with your animation kit.”

100% agreed, also your two points roadmap sounds perfect & wise.

Again thank you for your support, :sparkling_heart:BA community.

@Flattiefolks

I’ve written two python scripts which implement the first part of my roadmap.

I’ve also setup a github repository for this project, I’ve called it “scene-per-screen”.
https://github.com/alisealy/scene-per-screen
(Not strictly necessary, but it gave me an excuse to learn how github repos work, which is something I’ve been meaning to do for a while now)

Sadly I’m quite busy at the moment (hence the slow progress), but hopefully I will have some time later this week to finish it off