Creating panels for placing Blender add-ons user interface (UI)

One of the essential conditions of comfortable work is the convenience and speed of accessing add-ons functional. It is not enough to write add-on and register it in Blender API system. Be sure to give the easily call add-on functions ability to user. So, any addon should determine a place to put interface buttons that provide access to its functionality.

All available Blender user interface located on panels. Three panels are the most used: T-bar – opens/hides by pressing the t keyboard button, N-bar – opens/hides by pressing the n button, and the Properties panel – called in the main menu of any window typed it as Properties.

Blender API allows to define users panels to place add-on functional buttons on any of this three main panels.

Consider the placement of add-ons buttons with simple demonstration add-on that adds a cube to the scene. That default cube, may be it useful at least? ).

The code of this add-on:

In this example the system operator bpy.ops.mesh.primitive_cube_add() is wrapped into a user defined class to call it through specified identifier. There may be any other content in execute function, we just created our personal operator.

Our operator functionality can be called by opening the Python Console window and typing bpy.ops.mesh.add_cube_sample() command. But, of course, it is very inconvenient.

Lets append our own panels to each of the main Blender interface panel and place our add-on call buttons on them. Totally we will create 4 panels – two on different areas of T-bar, one on the N-bar and one on the Render tab of Properties window.

Creating panels for user add-on functionality buttons
Creating panels for user add-on functionality buttons

Like user defined operators Blender API panels define as classes. Panels classes need to inherit from bpy.types.Panel.

In the panel class is necessary to override the parent function draw, which responsibles for the panel output. This function is executed during panel class registration through register_class function. Inside the draw function is necessary to fill the layot object with displayed on the panel structure: buttons, input fields, checkboxes, all that add-on functionality requires.

When add-on powers off, registered panel classes, as well as operators, should be removed from the Blender API with the unregister_class function.

As for operators, in the panel class is necessary to define a constant – the class identifier with a predefined name bl_idname. Text variable bl_label used to store the panel header text.

There are several variables with predefined names need to define in panel class to determint the location point of panel in Blender interface:

  1. bl_space_type
    • Specifies the space (in fact – the window), where the panel will be located. It can take the following values: EMPTY, VIEW_3D, TIMELINE, GRAPH_EDITOR, DOPESHEET_EDITOR, NLA_EDITOR, IMAGE_EDITOR, SEQUENCE_EDITOR, CLIP_EDITOR, TEXT_EDITOR, NODE_EDITOR, LOGIC_EDITOR, PROPERTIES, OUTLINER, USER_PREFERENCES, INFO, FILE_BROWSER, CONSOLE.
  2. bl_region_type
    • Specifies the region in space to place the panel. It can take the following values: WINDOW, HEADER, CHANNELS, TEMPORARY, UI, TOOLS, TOOL_PROPS, PREVIEW.
  3. bl_context
    • Specifies the panel placement context.
  4. bl_category
    • Specifies the panel placement group.

Using this rules lets create 4 classes to accommodate 4 custom panels in the right places. In all panels lets create single button to call previously created operator with add_cube_sample identifier.

  1. T-bar, toos region:

Set bl_space_type to ‘VIEW_3D’ – to indicate 3D View window. The region is T-bar – the tools pane, so set bl_region_type to ‘TOOLS’. If we want to place our panel on one of the existing tabs, we need to specify the category of the tab, for example ‘Tools’ – top Tools tab with basic modeling tools buttons. Or we can create new tab specify non-existent category.

The required panel class code:

  1. T-bar, properties region:

All the same as for Tools region of the T-bar, but the region needs to specity as ‘TOOLS_PROPS’. Categories are not necessary.

  1. N-bar:

Region – ‘UI’.

  1. Properties window, place our panel on Render tab:

Set space to ‘PROPERTIES’, region to ‘WINDOW’, and to determine the tab, specify the context – ‘render’ (lowercase).

Panel classes are finished. To correctly panel creation and deletion during add-on initialization and completion, fill register and unregister add-on functions.

Demonstration addon completed. After its installation and activation, in certain areas the panels with the cube creation button appear.

The demonstration addon full code: