Blender 3D: Noob to Pro/Advanced Tutorials/Python Scripting/Addon User Interface

Start
So you’ve defined a new operator in your addon. Now it would be nice to give that addon a proper interface, so the user doesn’t have to hunt through the spacebar menu to find that operator.

The nicest way to do this is to define a panel that shows up in a window somewhere, with controls in it that the user can click on to operate your addon. You can put the panel in all kinds of places, but here we will insert it in the Tool Shelf (which can be hidden or shown on the left side of the 3D View by pressing.

Your First Panel
A panel is defined by subclassing the bpy.types.Panel class. You set the value of various attributes (bl_space_type, bl_region_type, bl_category and bl_context) to determine the context in which the panel will appear, and also give a title to the panel (bl_label):

Those first three attributes cause the panel to appear in the Tool Shelf, but only while the 3D View is in Object mode.The bl_category line determines the toolbar tab the addon is placed in, and only applies to the toolbars with tabs (added 2.7). Specify any existing tab, or define a new one.

Your class also needs to define a draw method, which defines the items that go into the panel. This example creates a new column of UI elements for the panel, and inserts a single UI element which is a button that will invoke the operator with the name you previously defined when clicked:

Adding Undo Support
While we’re at it, go back and add the following line to the operator definition:

This allows the user to undo the addition of the tetrahedron in the usual way with, and redo it with.

Put It All Together
Here is the complete script for the addon as it stands now:

Note the addition of another  call for our custom panel.

As before, hit to execute it. Nothing should appear to happen; Blender processes your subclass definitions, and registers them for use, as requested, in the appropriate places.

But now, go back to the 3D View. Make sure you are in Object mode. Bring up the Tool Shelf if it isn’t visible, by pressing. At the bottom; in the tab you have specified, you should see your new panel appear:



Note the triangle next to the title that Blender automatically gives you, to collapse or expand the panel without any extra work on your part. Get rid of any tetrahedron object that might have been created by following the tutorial on the previous page; now click your new “Add Tetrahedron” button, and watch the object be created again!

Space Types, Region Types, Contexts, Oh My!
Those values for bl_space_type, <TT>bl_region_type</TT> and <TT>bl_context</TT> are (partially) listed here in the Blender API documentation, but not fully explained. Here’s a list of permissible values for each that I found from looking at source code:

Presumably, not all combinations will make sense.