1
$\begingroup$

am trying to add a menu into the shader editor that can be accessed by SHIFT Q shortcut, if i add the same menu in the 3d viewport it works but not in the editor

my code

class shader_menu(bpy.types.Menu):
    bl_idname = "evx.shader_menu"
    bl_label = "Shader_menu"

    def draw(self, context):
        layout = self.layout
        print(123)
        layout.operator("wm.open_mainfile")
        layout.operator("wm.save_as_mainfile")


def register():
    
    for cls in pie_classes:
        bpy.utils.register_class(cls)

    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon
    if kc:
        km = wm.keyconfigs.addon.keymaps.new(name='3D View', space_type='VIEW_3D')
        kmi = km.keymap_items.new(main_shortcut.bl_idname, type='Q', value='PRESS',shift=True)

        

        addon_keymaps.append((km, kmi))

        km = wm.keyconfigs.addon.keymaps.new(name='shade Menu', space_type='NODE_EDITOR')
        kmi = km.keymap_items.new('wm.call_menu', type='Q', value='PRESS', shift=True)
        # kmi = km.keymap_items.new('mesh.primitive_cube_add', type='U', value='PRESS',shift=True)
        kmi.properties.name =  shader_menu.bl_idname
        addon_keymaps.append((km, kmi))
    # print(addon_keymaps)
$\endgroup$
1
  • 1
    $\begingroup$ Hello, could you post a sample of your code ? $\endgroup$
    – Gorgious
    Dec 23, 2021 at 7:36

1 Answer 1

0
$\begingroup$

TL;DNR: As far as why the shortcut is not working:

    km = wm.keyconfigs.addon.keymaps.new(name='3D View', space_type='VIEW_3D')

You need to change VIEW_3D to NODE_EDITOR.

Long answer:

It is not entirely clear what you want to do, but to add a new menu to the Node editor header, you need a command like

    bpy.types.NODE_HT_header.append(shader_menu_draw)

Here is an simple example, although it puts a button in place, you can make a menu instead fairly easily. Note that the menu entry goes at the right end of the header -- that's a limitation in how Blender handles additions to menus.

import bpy
from bpy.types import Operator


bl_info = {
    "name" : "sshade",
    "description" : "HOWTO examples for adding a menu to the shader editor",
    "author" : "Marty",
    "version" : (0, 0, 1),
    "blender" : (2, 93, 0),
    "location" : "Node",
    "warning" : "",
    "support" : "COMMUNITY",
    "doc_url" : "",
    "category" : "3D View"
}

menu_keymap = None

def initialize_keymap():
    key_config = bpy.context.window_manager.keyconfigs.addon
    if key_config:
        key_map = key_config.keymaps.new(name='Node Editor', space_type='NODE_EDITOR')
        key_entry = key_map.keymap_items.new(SHADE_OT_demo.bl_idname,
                                                            type='Q',
                                                            value='PRESS',
        )
        menu_keymap = (key_map, key_entry)
            
def deinitialize_keymap():
    if hello_keymap:
        key_map, key_entry = hello_keymap
        key_map.keymap_items.remove(key_entry)
        bpy.context.window_manager.keyconfigs.addon.keymaps.remove(key_map)
            
def shader_menu_draw(self, context):
    """ adds a menu entry when the function is added to an existing menu"""
    self.layout.operator("shade.demo")

class SHADE_OT_demo(Operator):
    """A very stupid operator entirely for the purpose of demonstration"""
    bl_idname = "shade.demo"
    bl_label = "say hello"
    bl_description = "say hello"
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(self, context):
        return True

    def execute(self, context):
        self.report({'INFO'}, f"menu hit")
        return {'FINISHED'}
    

classes = [
    SHADE_OT_demo,
]

def register():
    for c in classes:
        bpy.utils.register_class(c)
    initialize_keymap()
    bpy.types.NODE_HT_header.append(shader_menu_draw)
        
def unregister():
    for c in classes:
        bpy.utils.unregister_class(c)
    deinitialize_keymap()
    bpy.types.NODE_HT_header.remove(shader_menu_draw)
        
if __name__ == '__main__':
    register()
```
$\endgroup$

You must log in to answer this question.

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