3
$\begingroup$

I have created a custom panel and when N is pressed on the keyboard in 3d view we get the Sidebar with the Item tab opened by default, but I don't want that. I have also added an operator that does the same as N but I want to add one more thing that I could not find.

When someone presses N or executes my operator by pressing Shift+N it should open the Sidebar tab with my add-on.

import bpy


class MY_PT_PANEL(bpy.types.Panel):
    bl_label = "My Addon"
    bl_idname = "MY_PT_TAB"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "MyTab"

    def draw(self, context):
        layout = self.layout
        layout.label(text='Welcome to my addon',icon='FORWARD')

class MY_OT_OPERATOR(bpy.types.Operator):
    bl_idname='addon.open_sidebar'
    bl_label = 'Open SideBar'

    def execute(self,context):
        areas = bpy.context.screen.areas
        view_3d = None
        for area in areas:
            if area.type == 'VIEW_3D':
                view_3d = area

        space = view_3d.spaces[0]
        space.show_gizmo = False
        space.show_region_ui = True

        return {'FINISHED'}

addon_keymap = []

def register():
    bpy.utils.register_class(MY_PT_PANEL)
    bpy.utils.register_class(MY_OT_OPERATOR)

    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon
    km = kc.keymaps.new('3D View', space_type = 'VIEW_3D')
    kmi = km.keymap_items.new('addon.open_sidebar','N','PRESS',shift=True)

    addon_keymap.append((km,kmi))

def unregister():
    bpy.utils.unregister_class(MY_PT_PANEL)
    bpy.utils.unregister_class(MY_OT_OPERATOR)

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

if __name__ == "__main__":
    register()
```
$\endgroup$
13
  • $\begingroup$ Hello! What exactly do you mean by "open an addon" ? $\endgroup$
    – Gorgious
    Commented Feb 9, 2023 at 15:05
  • $\begingroup$ I mean whenever we press N it opens the Item tab which I don't. I should open another tab like View or Tool or my addon. $\endgroup$
    – Muzammil
    Commented Feb 9, 2023 at 15:23
  • 1
    $\begingroup$ In this case this and this should answer your question $\endgroup$
    – Gorgious
    Commented Feb 9, 2023 at 16:32
  • $\begingroup$ no, I'm not looking for this. $\endgroup$
    – Muzammil
    Commented Feb 9, 2023 at 16:43
  • 1
    $\begingroup$ Seems like it is really impossible to change the active Sidebar tab from Blender's Python API. $\endgroup$ Commented Feb 22, 2023 at 8:44

1 Answer 1

1
$\begingroup$

In blender 4.2 they added the ability to change and set categories. There is another option, through ctypes, it is implemented in my addon UniV, in the operator SplitUVToggle, but it is too complex for the average programmer.

import bpy

def set_active_panel_category(category, area_type):
    if bpy.app.version >= (4, 2, 0):
        areas = (area for win in bpy.context.window_manager.windows for area in win.screen.areas if area.type == area_type)
        for a in areas:
            for r in a.regions:
                if r.type == 'UI':
                    if r.width == 1:
                        with bpy.context.temp_override(area=a):
                            bpy.ops.wm.context_toggle(data_path='space_data.show_region_ui')
                    try:
                        if r.active_panel_category != category:
                            r.active_panel_category = category
                            a.tag_redraw()
                    except NameError as e:
                        raise e
                    
area_type = 'VIEW_3D'
category = 'View'

set_active_panel_category(category, area_type)
```
$\endgroup$

You must log in to answer this question.

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