Editing content of an enum attribute

2,636 views
Skip to first unread message

Bay

unread,
Feb 23, 2015, 8:04:22 PM2/23/15
to python_in...@googlegroups.com
Hi guys, 
             I've been working with an user interface that requires me to edit the content of an enum attribute (basically removing and adding to its contents) but i seemed to run into a snag so would appreciate any input. 

I created the attribute like so :
cmds.addAttr(at = 'enum', keyable=True, en = 'string1:string2:string3:string4:string5:string6', ln='Parent') 

and sought to adjust its content like so 

cmds.addAttr(e=True, en = 'string1:string2:string4:', ln='Parent')  

But i kept getting a 
# Error: line 1: RuntimeError: file <maya console> line 1: Attribute not found/supplied # 

Would anyone happen to know what I'm doing wrong ? 

Thank you
Gann Boon Bay

damon shelton

unread,
Feb 23, 2015, 8:21:43 PM2/23/15
to python_in...@googlegroups.com
you should provide object arguments when coding to edit stuff, relying on selection only can cause issues, you can use cmds.ls(sl=True) as the argument as well
to edit the enum you have to provide the object.attr argument
using ln=  tries to rename the longname

cmds.addAttr('locator1', at = 'enum', keyable=True, en = 'string1:string2:string3:string4:string5:string6', ln='Parent') 
cmds.addAttr('locator1.Parent', e=True, en = 'string1:string2:string4:')

or 

cmds.addAttr(cmds.ls(sl=True)[0], at = 'enum', keyable=True, en = 'string1:string2:string3:string4:string5:string6', ln='Parent') 
cmds.addAttr(cmds.ls(sl=True)[0]+'.Parent', e=True, en = 'string1:string2:string4:')

or 
obj_name = cmds.ls(sl=True)[0]
cmds.addAttr(obj_name, at = 'enum', keyable=True, en = 'string1:string2:string3:string4:string5:string6', ln='Parent') 
cmds.addAttr(obj_name+'.Parent', e=True, en = 'string1:string2:string4:')

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/f98e24dd-ebdd-43a4-9cc4-97f5902ae9c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bay

unread,
Feb 24, 2015, 11:15:47 AM2/24/15
to python_in...@googlegroups.com
Aha....I didn't know that. Thank you very much for the help! :) 

Benson Black

unread,
Aug 31, 2023, 6:31:50 AM8/31/23
to Python Programming for Autodesk Maya
Thank you! This has been very helpful to me. Cheers!
Message has been deleted

Bryan Money

unread,
Sep 28, 2023, 5:20:32 PM9/28/23
to Python Programming for Autodesk Maya
if we use pymel is there a more object oriented way of doing this?

import pymel.core as pm
sel = pm.ls(sl = True)
sel[0].addAttr('myEnum', at = 'enum', en = 'herp:aderp', k = True, h = False)

is nice as I can affect each PyNode directly but I get the same error Bay gets if I then use
sel[0].addAttr('myEnum', e = True, en = 'no:herps')

Marcelo

unread,
Sep 28, 2023, 6:52:28 PM9/28/23
to python_in...@googlegroups.com

On Thu, Sep 28, 2023 at 2:20 PM Bryan Money <1975g...@gmail.com> wrote:
Anyone know how to do this in pymel?
if I assign a maya transform via pymel and add an attribute it works fine

import pymel.all as pm
sel = pm.ls(sl = True)
sel[0].addAttr('myEnum', at = 'enum', en = 'herp:aderp:adoo', k = True, h = False)

but if I try to edit the options via the same command
sel[0].addAttr('myEnum', e = True, en = 'herp:aderp:adee:adoo')

if I have to do it via the cmds way (but with pm) that.. kinda ruins the whole object oriented programing that pymel is good-ish at.

On Thursday, August 31, 2023 at 3:31:50 AM UTC-7 Benson Black wrote:

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Bryan Money

unread,
Sep 28, 2023, 8:21:43 PM9/28/23
to Python Programming for Autodesk Maya
one would think but I get the "invalid flag" error.

Marcelo

unread,
Sep 28, 2023, 10:26:47 PM9/28/23
to python_in...@googlegroups.com
Hmm that's odd, the following works fine here on my end:

sel[0].myEnum.setEnums('herp:aderp:adee:adoo')


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Bryan Money

unread,
Oct 9, 2023, 5:44:28 PM10/9/23
to Python Programming for Autodesk Maya
hmm.. it does work if I pass it as its own object, I might have to rewrite a bit of code then.. sweet!

quick question, is there any way to edit the value of an enum value? when I do it it looks fine on the surface but when trying to listEnums next I get "[label]=[int]" as the enumerate value when all I want is the "[label]" and said label to equate to [int]

example:

sel[0].myEnum.setEnums('herp:aderp:adee:adoo')

has labels of "herp" "aderp" "adee" "adoo", with respective values of 0, 1, 2, 3 .
but when I want to remove "adee" I want the values to remain the same, just skipping adee's value.
"herp" "aderp" "adoo" with respective values of 0, 1, 3

but if I do it this way
pm.addAttr(spcatr, e=True,
enumName=':'.join(['{}={}'.format(k, e) for k, e in spcatr.getEnums().items() if k != cntspc]))

where spcatr = sel[0].myEnum and cntspc = "adee" 
everything after "adee" (which no longer exists) comes in as the string "{k}={e}" 
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages