4
$\begingroup$

For example, I'd like to set a custom hotkey to change a window to shader editor specifically.

The way it works right now is that you have major types of area types and you cycle through their different versions. But I don't want to cycle, I want to set a shader editor.

keybinds that cycle through types

I know it's possible with Python, but I'm not sure if it's possible with just the user interface? And I'm not sure how would I implement a python script into my entire program if I had to, I'd probably need to code an addon?

$\endgroup$

1 Answer 1

7
$\begingroup$

It looks like by default you are only able to change the hotkeys to cycle through editor modes, or switch to the space if it's the only one in that space type.

However, it's really easy to make your own operators to accomplish simple tasks like this.

Switch to the Scripting workspace, and at the top click Templates > Python > Simple Operator.

Location of simple operator template

Then in one of the other windows, perform the actions you want your new operator to accomplish.

You'll see in the info bar that when you switch to the Shader editor, blender is setting your bpy.context.area.ui_type attribute to "ShaderNodeTree".

info bar in python console

So copy that line and plop it into the execute section of your operator. Before the return {"FINISHED"}. We can also remove the part bpy from the beginning, since the execute method is already getting context from the operator class.

We can also clean up the rest of the template, and get rid of alot of unnecessary stuff, to get it ready to use long term.

Operator cleanup

We also need to grab a snippet from another template, the Addon Add Object template. Copy, the part at the top that looks like:

bl_info = {
    "name": "New Object",
    "author": "Your Name Here",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "View3D > Add > Mesh > New Object",
    "description": "Adds a new Mesh Object",
    "warning": "",
    "doc_url": "",
    "category": "Add Mesh",
}

Paste into your script and edit it to match your operator. Most of it can be left blank. But it's necessary to include it to install your script as an addon.

The final result should look like:

bl_info = {
    "name": "Switch to Shader Editor",
    "author": "Your Name Here",
    "version": (1, 0),
    "blender": (2, 80, 0),
#    "location": "View3D > Add > Mesh > New Object", # Don't need to inlcude this because it's not in  menu
    "description": "Switches Directly to Shader Node Editor",
    "warning": "",
    "doc_url": "",
    "category": "Screen",
}


import bpy

class SwitchToShaderEditor(bpy.types.Operator):
    """Switches Directly to Shader Node Editor"""
    bl_idname = "screen.switch_to_shader_editor"
    bl_label = "Switch to Shader Editor"

    def execute(self, context):
        context.area.ui_type = "ShaderNodeTree"
        return {'FINISHED'}


def register():
    bpy.utils.register_class(SwitchToShaderEditor)
    
def unregister():
    bpy.utils.unregister_class(SwitchToShaderEditor)
   
if __name__ == "__main__":
    register()

Then save this file somewhere you can remember, save it as something like "switch_to_shader_editor.py". Then go to Edit > Preferences > Addons > Install, find that file and double click it to install it.

You should see it pop up into your addons, check it to activate it.

Where you set the keymap is totally dependent on what current context you're dealing with, so it's important to think carefully about where you set your hotkey. (If you set it in 3D View (Global) for instance, if you were hovering your mouse in the UV editor and tried to use the hotkey, it wouldn't work. The context matters.)

So, with this in mind, go to Keymaps > Screen > Screen (Global) and scroll to the bottom and click "Add New".

In the field where it says "none" replace that with the value from "bl_idname" in your operator script. it should look like:

enter image description here

Set whatever hotkey you want. Then save your preferences. Close that window, and test it out in the viewport. It should be working.

You now have your very own custom operator with hotkey!

There's alot more to cover about addon creation, but this process can basically be followed for 75%-80% of any process you want to turn into an Operator.

$\endgroup$
11
  • $\begingroup$ if this works (and i don't know)....but then Martys answer is just wrong.... ;) $\endgroup$
    – Chris
    May 14 at 14:15
  • 1
    $\begingroup$ I tested it as I was writing it. Marty's answer is basically the first section of my answer. Where operator attached to the current is not able to do this by default. But when you create your own operator you can basically do whatever you want. $\endgroup$
    – Jakemoyo
    May 14 at 14:41
  • $\begingroup$ @Jakemoyo This is similar to what I was preparing but I'm surprised it works, (and yes, it does work), because I thought setting ui_type should only work if you're already in the right space type, so I thought you'd have to make the call to space_type_set_or_cycle and then make this call. Any idea how this actually works? $\endgroup$ May 14 at 14:54
  • $\begingroup$ Omg you're the best! Just finished implementing this. You saved me like a good week of research and fighting basics! I didn't even know there are templates. I'm incredibly impressed you wrote it out like this. I'm going to develop it further :) Thank you so much! $\endgroup$
    – AlexM
    May 14 at 14:59
  • $\begingroup$ @AlexM Glad to help! "Teach a man to fish" as they say. $\endgroup$
    – Jakemoyo
    May 14 at 17:09

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.