A quickie, set a Maya enum by string

I’m just adding this here on my site because I always need to find it in older scripts and I don’t want to keep digging for it. So, as far as I know, there’s no easy way to set an enum attribute by a string value in Python. You can only set it by index value, which doesn’t do you much good if you don’t know the index of the value you need. In case you need it here’s a quick little function to use a string value to set the enum:

def set_enum_by_string(node, attribute, string):
    enum_string = pm.attributeQuery(attribute, node=node, listEnum=True)[0]
    enum_options = enum_string.split(":")
    index = enum_options.index(string)
    pm.setAttr("%s.%s" % (node, attribute), index)

Example use:
set_enum_by_string(“my_super_cube”, “color”, “green”)

Share This: