Getting enum option strings

Something rather difficult is to reverse engineer the strings of an enum, as it is impossible using normal commands. Here’s a function using the Python API that gives a list of enum string options for use in any visual representations and interface elements that allow attribute editing.

from maya.OpenMaya import *


def getEnumOptions(in_node, in_attr):
    list = MSelectionList()
    MGlobal.getSelectionListByName(in_node, list)
    node = MObject()
    list.getDependNode(0, node)
    node = MFnDependencyNode(node)
    attr = MFnEnumAttribute( node.attribute(in_attr) )
    #min/max maybe can be get using getattr
    util = MScriptUtil()
    ptr = util.asShortPtr()
    attr.getMin(ptr)
    min = util.getShort(ptr)
    attr.getMax(ptr)
    max = util.getShort(ptr)
    enumoptions = []
    for i in range(min, max, 1):
        try:
            enumoptions.append( attr.fieldName(i) )
        except:
            #enums can be all over the place
            #i.e. node behaviour having 5 options but a max of 10
            pass
    return enumoptions

Leave a Reply

Your email address will not be published. Required fields are marked *