set enum as string?

1,358 views
Skip to first unread message

Martin Puttkammer

unread,
Apr 23, 2008, 10:24:12 AM4/23/08
to python_in...@googlegroups.com
greetings,

is it possible by any chance to set enum attributes in maya using strings?
i know how to read by *getAttr asString*, but set Attr doesn't work that way.
any way this could happen, otherwise i would have to write a dictionary for this one?

regards,
martin

Ofer Koren

unread,
Apr 23, 2008, 11:05:22 AM4/23/08
to python_in...@googlegroups.com
It's actually the addAttr command:

addAttr -e -enum "red:green:blue" obj.colorAttr
--


- Ofer
www.mrbroken.com

Martin Puttkammer

unread,
Apr 23, 2008, 11:16:00 AM4/23/08
to python_in...@googlegroups.com
Who would have thought of that...
Thanks Ofer, helps a great deal in keeping things clean and tidy.

Appreciate it,
Martin

Martin Puttkammer

unread,
Apr 24, 2008, 4:37:00 AM4/24/08
to python_in...@googlegroups.com
Sorry, a bit late.

When I tried implementing

"addAttr -e -enum "red:green:blue" obj.colorAttr"
I realized I didn't state my point clearly.

What I am looking for is setting the rotation order of an object by string.
Usually the 6 different rotation orders are an enum attr which is set by ints (0 to 5).
I want to avoid writing a dictionary for that in python, since for the script I am working on I need to handle
the string values mapped to the integers only. So I would rather skip the integers altogether.

So for that instead of using cmds.setAttr(myObject.rotateOrder, 1) I would rather use cmds.???(myObject.rotateOrder, 'xyz').
Is that possible by any chance?

Thanks, again,
Martin

Ofer Koren

unread,
Apr 24, 2008, 5:00:33 AM4/24/08
to python_in...@googlegroups.com
This is probably not what you want exactly... I'm not sure I understand completely, but how about:

# get the list of enums; you can create this as a constant somewhere in your module too...
enums = cmds.addAttr(myObject.rotateOrder, q=1, en=1).split(":")

# the index() method will find the first occurance of the specified item in that list and return its position
cmds.setAttr(myObject.rotateOrder, enums.index('xyz'))
--


- Ofer
www.mrbroken.com

Martin Puttkammer

unread,
Apr 24, 2008, 5:20:49 AM4/24/08
to python_in...@googlegroups.com
Sorry, I guess the description was too elaborated.
Index is helpful, I didn't know about that either.

To the point: MyObject in the maya scene now has a rotation order of 'zxy'.
Imagine I want a script to set the rotation order, say, to 'xyz'.

Which is the correct cmds.???? to set the current value of the enum with the -string-?
Setting the enum value as an integer is easy. But I do not wish to convert 'xyz' to integer
first, I would rather pass the string itself directly to maya and let the application do the correct translation
of 'xyz' to the required integer (which would be 0 in that case).

Is that possible?

Thanks,
Martin

Martin Puttkammer

unread,
Apr 24, 2008, 8:09:02 AM4/24/08
to python_in...@googlegroups.com
Ok, sorry everyone for the elongated conversation, I just happened to be able to ask someone
at Autodesk and the official answer is that there is no way to set an enum value directly as string
and not as integer.
Simply won't happen.

Big thanks to Ofer for keeping up with the explanations.

Martin

Chadrik

unread,
Apr 24, 2008, 11:44:29 AM4/24/08
to python_in...@googlegroups.com
this feature will be supported by the next version of pymel. currently
supported are:

o = PyNode('MyObj')

# set the enum list (assuming the attr is already added and is an int)
o.myAttr.setEnums( ['xyz', zyx', yxz' ] )

# get the enum list we just set
enums = o.myAttr.getEnums()

# get the enum as an int
val = o.myAttr.get()

# get the enum as a string
val = o.myAttr.get( asString=True )

# set the enum as an int
o.myAttr.set( 2 )

the new addition:

# set the enum as a string
o.myAttr.set( 'xyz', asString=True )

-chad

John Creson

unread,
Apr 24, 2008, 1:36:13 PM4/24/08
to python_in...@googlegroups.com
very nice
Reply all
Reply to author
Forward
0 new messages