Along with the scene objects, the Blender Python API allows us to interact with the Blender interface. For example, using the Python API, we can show and hide the most commonly used toolbar – the N-panel.
For toggling (switching between showing and hiding) N-panels in Blender UI, the wm.context_toggle() operator is used.
The full context data path to the required area is passed in the operator parameter. For the N-panel it will be ‘space_data.show_region_ui’.
To show or hide the N-panel in the current area, we need to call this operator in the simple way:
1 |
bpy.ops.wm.context_toggle(data_path='space_data.show_region_ui') |
To hide or show the N-panel in another area, for example in the 3D viewport, we need to override the context to the required area.
Let’s get the 3D viewport area context and call the wm.context_toggle() operator with an overridden context:
1 2 3 4 |
area = next((area for area in bpy.context.screen.areas if area.type == 'VIEW_3D')) with bpy.context.temp_override(area=area): bpy.ops.wm.context_toggle(data_path='space_data.show_region_ui') |
The N-Panel will be toggled – opened or closed in the 3D Viewport area.