How to know what enums are valid for an operator?

Hi,
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.

Thanks

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.
    http://www.blender.org/documentation/blender_python_api_2_64_release/bpy.ops.object.html?highlight=bpy.ops.object.edit#bpy.ops.object.editmode_toggle

  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\ops.py", 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.

1 Like

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")
bpy.utils.register_class(ModeSwitchMenu)




# 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 = bpy.types.OBJECT_OT_mode_set.bl_rna.properties['mode'].enum_items.keys()
ob_types = bpy.types.Object.bl_rna.properties['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
    
    bpy.ops.object.add(type=ob_type)
    ob = bpy.context.object
    obs.append(ob)
    
    modes_available = []
    
    for mode in modes:
        #print(mode, ob_type)
        try:
            bpy.ops.object.mode_set(mode=mode)
        except TypeError:
            continue
        else:
            modes_available.append(mode)
    print("Object type '%s' supports modes:" % ob_type)
    [print(" -", m) for m in modes_available]
    print()
    
bpy.ops.object.mode_set() # object mode
for ob in obs:
    ob.select = True
bpy.ops.object.delete()

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:

https://sites.google.com/site/bartiuscrouch/scripts/icon_display