How to know what enums are valid for an operator?

Could some one help me with a py question?
How can I know what enums an operator can take in an argument?
For example mode changing in 3D view is done by:

bpy.ops.object.mode_set(mode='EDIT', toggle=False)

How do I find out what modes are available for this object type? For example only ‘OBJECT’ is available for lamps, but meshes also have ‘EDIT’ and so on.


Here are some ways I have done this in the past. Following your example.

  1. Use the wiki. This can be frustrating and you almost have to know what you are searching for beforehand.

  2. Try the operator in the console and intentionally make an error in your keyword argument. eg

bpy.ops.object.mode-set(mode = 'What the heck goes here')

will give you back

Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
  File "C:\Dev\Blender\blender-2.65-r53841-win64\2.65\scripts\modules\bpy\", line 188, in __call__
    ret = op_call(self.idname_py(), None, kw)
TypeError: Converting py args to operator properties:  enum "What the heck goes here" not found in ('OBJECT', 'EDIT', 'SCULPT', 'VERTEX_PAINT', 'WEIGHT_PAINT', 'TEXTURE_PAINT')

Other than that…I’m not really sure either.

Thanks, but that way around I know about.

From the “Blender Experimental 2012” key-map I have found the following code that makes a menu with the available modes.

class ModeSwitchMenu(bpy.types.Menu):
    """ A menu for switching between object modes.
    bl_idname = "OBJECT_MT_mode_switch_menu"
    bl_label = "Switch Mode"

    def draw(self, context):
        layout = self.layout
        layout.operator_enum("object.mode_set", "mode")

# Temporary work around: Blender does not properly limit the mode switch menu
# items until the first mode switch (e.g. mesh objects will show pose mode as
# an option).
# TODO: file a bug report for this behavior.
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

So this is almost what I want, but I don´t want the menu.

the mode menu is generated by c-code, operator_enum calls a c-code function as well. I don’t see a chance with current API to retrieve the possible modes. You better test all object types and write the modes down.

What do you need this for btw?

Here’s an automated approach for getting the available modes (however hacky):

import bpy

modes =['mode'].enum_items.keys()
ob_types =['type'].enum_items.keys()

window = bpy.context.window

obs = []

for ob_type in ob_types:
    if bpy.context.object and bpy.context.object.mode != 'OBJECT':
        bpy.ops.object.mode_set() # object mode
    ob = bpy.context.object
    modes_available = []
    for mode in modes:
        #print(mode, ob_type)
        except TypeError:
    print("Object type '%s' supports modes:" % ob_type)
    [print(" -", m) for m in modes_available]
bpy.ops.object.mode_set() # object mode
for ob in obs: = True

A bit hacky, so I think I will just hard code the values.

I´m just exploring the api by implementing a mode-switch similar how to alt + tab works in windows.

Nice…that is very handy

i got that idea from Bart Crouch’s icon display addon :slight_smile: