Easy Creation Of Enumerated Menus

Blender’s Python API has yet to make any use of the enums introduced in Python 3.4. However, it is still possible to use them yourself, to simplify some code in your addons.

When I began hacking on the stairbuilder from the add_mesh_building_objects addon, I found it had a lot of places where the user could pick from a set of fixed options: e.g. stair type, tread type and so on. These were natural places to use enums.

So I defined a custom EnumPropItems base class, to simplify the job of creating such enumerations. For each enumeration item, you need an internal identifier, plus a descriptive string to show to the user in the menu. For example, for stair types:

class STAIRTYPE(EnumPropItems) :
    "overall types of stairs."
    FREESTANDING = ("Freestanding", "Generate a freestanding staircase.")
    HOUSED_OPEN = ("Housed-Open", "Generate a housed-open staircase.")
    BOX = ("Box", "Generate a box staircase.")
    CIRCULAR = ("Circular", "Generate a circular or spiral staircase.")
#end STAIRTYPE

Actually creating a menu to let the user choose from these items is as simple as:

    stair_type = EnumProperty \
      (
        name = "Type",
        description = "Type of staircase to generate",
        items = STAIRTYPE.all_items()
      )

As you can see, this saved a lot of repetitive coding.