With the Python API we can customize Blender interface as we need. For example, we can place the most frequently used operators to the header (top menu) of any workspace area. This way we can assemble our own ribbon with “quick favorite” buttons.
The header of each area in Blender is described in the API by its own type. For example, the 3D Viewport header is described in bpy.types.VIEW3D_HT_header.
The easiest way to determine the header type for the desired area is to click on any button on it with the right mouse button, select “Edit Source” in the drop-down menu (“Developer Extras” must be enabled in the preferences) and follow the code that opens in the Text Editor up to the class definition.
For the main Blender areas, the header types are:
For example, the button for the operator adding the default cube “bpy.ops.mesh.primitive_cube_add” must be defined with the function as follws:
1 2 3 4 5 6 7 |
@persistent def TEST_HT_view3d(self, context): self.layout.operator( operator='mesh.primitive_cube_add', icon='MESH_CUBE', text='' ) |
Not to loose the function if the scene is reloaded, we wrapped it in the @persistent decorator.
Now, to place this operator button in the 3D viewport header, let’s add our function to the header with the “prepend” method.
1 |
bpy.types.VIEW3D_HT_header.prepend(TEST_HT_view3d) |
When using “prepend”, all interface elements defined in our function will be drawn in the header before its own buttons. We can also use the “append” method, in which case all elements from our function will be placed in the header after its own buttons.
To remove our button from the area header, we need to remove our function from the header type by calling its “remove” method:
1 |
bpy.types.VIEW3D_HT_header.remove(TEST_HT_view3d) |