Register a keymap for all editors in Blender 2.8


(Ethan Aeris) #1

Hi everyone,

i am writing a free addon : a pie menu to switch between workspaces for Blender 2.8.

For now, it works only in one view (view 3d or node view, or script view).

trying to figure it out, to work everywhere,
I found the following lines on the net that work in 2.79 , but not in 2.80.
Respects to the author of the lines below , Trirop.

addon_keymaps = []
def register_keymaps():
    wm = bpy.context.window_manager
    km = wm.keyconfigs.addon.keymaps.new(name = "Window",space_type='EMPTY', region_type='WINDOW')
    kmi = km.keymap_items.new("wm.call_menu_pie", type = "E",alt=True, value = "PRESS")
    kmi.properties.name = "editor_switcher_pie_menu"
    addon_keymaps.append(km)

The empty space type do not seem to work in 2.8 everywhere as it should be like in 2.79.

I know that the api is not finished yet, but is there a new way to do it please ?

The code and link of my WIP addon below

# -*- coding: utf-8 -*-

# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

bl_info = {
    "name": "Pie Workspaces",
    "description": "Pie Workspaces",
    "author": "Laurent Laget, Lapineige",
    "version": (0, 1, 2),
    "blender": (2, 8, 0),
    "location": "Shift Tab",
    "warning": "",
    "wiki_url": "",
    "category": "3d View"
    }

import bpy
from bpy.types import Menu


# Pie Workspaces 
class VIEW3D_PIE_workspaces(Menu):
    bl_idname = "pie.workspaces"
    bl_label = "Workspaces"

    def draw(self, context):
        layout = self.layout
        pie = layout.menu_pie()
        col = pie.split().column()
        row = col.split(align=True)
 
        col.operator("class.layout", text ="Layout", icon ='SCENE_DATA')
        col = pie.column(align=True)
        
        col.operator("class.modeling", text ="Modeling", icon ='EDITMODE_HLT')
        col = pie.column(align=True)
        
        col.operator("class.worksculpt", text ="Sculpt", icon ='SCULPTMODE_HLT')
        col = pie.column(align=True)
        
        col.operator("class.uvediting", text ="UV Editing", icon ='UV_ISLANDSEL')
        col = pie.column(align=True)
        
        col.operator("class.texturepaint", text ="Texture Paint", icon ='BRUSH_DATA')
        col = pie.column(align=True)
        
        col.operator("class.shading", text ="Shading", icon ='MATERIAL_DATA')
        col = pie.column(align=True)
        
        col.operator("class.animation", text ="3D Animation", icon ='SEQUENCE')
        col.operator("class.da", text ="2D Animation", icon ='GREASEPENCIL')
        col.operator("class.scripting", text ="Scripting", icon ='SCRIPTPLUGINS')
        col = pie.column(align=True)
        
        col.operator("class.rendering", text ="Rendering", icon ='SCENE')
        col.operator("class.compositing", text ="Compositing", icon ='RENDER_RESULT')
        col = pie.column(align=True)
        

class layout(bpy.types.Operator):
    bl_idname = "class.layout"
    bl_label = "layout"
    
    def execute(self, context):
        
        layout = self.layout
        bpy.data.window_managers['WinMan'].windows[0].workspace = bpy.data.workspaces['Layout']
        return {'FINISHED'}

class modeling(bpy.types.Operator):
    bl_idname = "class.modeling"
    bl_label = "modeling"
    
    def execute(self, context):
        
        layout = self.layout
        bpy.data.window_managers['WinMan'].windows[0].workspace = bpy.data.workspaces['Modeling']
        return {'FINISHED'}

class worksculpt(bpy.types.Operator):
    bl_idname = "class.worksculpt"
    bl_label = "worksculpt"
    
    def execute(self, context):
        
        layout = self.layout
        bpy.data.window_managers['WinMan'].windows[0].workspace = bpy.data.workspaces['Sculpting']
        return {'FINISHED'}
    
class uvediting(bpy.types.Operator):
    bl_idname = "class.uvediting"
    bl_label = "uvediting"
    
    def execute(self, context):
        
        layout = self.layout
        bpy.data.window_managers['WinMan'].windows[0].workspace = bpy.data.workspaces['UV Editing']
        return {'FINISHED'}
    
class texturepaint(bpy.types.Operator):
    bl_idname = "class.texturepaint"
    bl_label = "texturepaint"
    
    def execute(self, context):
        
        layout = self.layout
        bpy.data.window_managers['WinMan'].windows[0].workspace = bpy.data.workspaces['Texture Paint']
        return {'FINISHED'}

class shading(bpy.types.Operator):
    bl_idname = "class.shading"
    bl_label = "shading"
    
    def execute(self, context):
        
        layout = self.layout
        bpy.data.window_managers['WinMan'].windows[0].workspace = bpy.data.workspaces['Shading']
        return {'FINISHED'}

class animation(bpy.types.Operator):
    bl_idname = "class.animation"
    bl_label = "animation"
    
    def execute(self, context):
        
        layout = self.layout
        bpy.data.window_managers['WinMan'].windows[0].workspace = bpy.data.workspaces['Animation']
        return {'FINISHED'}
    
class rendering(bpy.types.Operator):
    bl_idname = "class.rendering"
    bl_label = "rendering"
    
    def execute(self, context):
        
        layout = self.layout
        bpy.data.window_managers['WinMan'].windows[0].workspace = bpy.data.workspaces['Rendering']
        return {'FINISHED'}
    
class compositing(bpy.types.Operator):
    bl_idname = "class.compositing"
    bl_label = "compositing"
    
    def execute(self, context):
        
        layout = self.layout
        bpy.data.window_managers['WinMan'].windows[0].workspace = bpy.data.workspaces['Compositing']
        return {'FINISHED'}
    

class da(bpy.types.Operator):
    bl_idname = "class.da"
    bl_label = "da"
    
    def execute(self, context):
        
        layout = self.layout
        bpy.data.window_managers['WinMan'].windows[0].workspace = bpy.data.workspaces['2D Animation']
        return {'FINISHED'}
    
    
class scripting(bpy.types.Operator):
    bl_idname = "class.scripting"
    bl_label = "scripting"
    
    def execute(self, context):
        
        layout = self.layout
        bpy.data.window_managers['WinMan'].windows[0].workspace = bpy.data.workspaces['Scripting']
        return {'FINISHED'}

classes = (
    VIEW3D_PIE_workspaces,
    modeling,
    worksculpt,
    uvediting,
    layout,
    texturepaint,
    shading,
    animation,
    rendering,
    compositing,
    da,
    scripting,
    )

addon_keymaps = []


def register():

    for cls in classes:
        bpy.utils.register_class(cls)
    wm = bpy.context.window_manager

    if wm.keyconfigs.addon:
        # Object Modes
        km = wm.keyconfigs.addon.keymaps.new(name='Object Non-modal')
        kmi = km.keymap_items.new('wm.call_menu_pie', 'TAB', 'PRESS' ,shift=True)
        kmi.properties.name = "pie.workspaces"
        addon_keymaps.append((km, kmi))

def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)

    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon
    if kc:
        for km, kmi in addon_keymaps:
            km.keymap_items.remove(kmi)
    addon_keymaps.clear()


if __name__ == "__main__":
    register()

https://blenderartists.org/t/addon-free-work-in-progress-pie-workspaces/1132853

Thank you !