How would I go about selecting a custom attribute using python ?
âlistAttrâ command has âudâ (userDefined) flag - set it to True, to query for user created (custom) attributes.
import pymel.core as pmc
sel = pmc.selected()[0]
user_defined_attrs = pmc.listAttr(sel, ud=True) # or with OOP approach: user_defined_attrs = sel.listAttr(ud=True)
P.S. This is for getting the names of attributes, if thatâs what youâre looking for; if by âselectingâ you mean to select it in channel box (not sure what else could you do?), you may pass in these attribute names to the âchannelBoxâ commandâs âsâ (select) flag:
import maya.mel as mel
gChannelBoxName = mel.eval('$temp=$gChannelBoxName')
pmc.channelBox(gChannelBoxName, e=True, s=user_defined_attrs) # user_defined_attrs is variable from above
Thank you ! âŚYes I want to select a custom attribute in the channel box.
I entered the following code, no error message but not selecting the attribute:
import maya.mel as mel
FK_to_IK = mel.eval(â$temp=$gChannelBoxNameâ)
pmc.channelBox(gChannelBoxName, e=True, s=FK_to_IK) # FK_IK name of custom attribute
Hey, youâve tumbled up code a bit - hereâs a version heavily commented, so you can understand what each line does:
import pymel.core as pmc
import maya.mel as mel
sel = pmc.selected()[0]
# query all user defined attributes (you can combine this with other flags: 'keyable', 'locked', etc.)
user_defined_attrs = sel.listAttr(ud=True)
# get channel box - leave this line as it is
gChannelBoxName = mel.eval('$temp=$gChannelBoxName')
# first argument to channelBox command is gChannelBoxName variable (it is just a string "mainChannelBox")
# attribute, or list of attributes is passed to 'select' flag
pmc.channelBox(gChannelBoxName, e=True, s=user_defined_attrs)
In your case, you have changed variable name for channel box to âFK_to_IKâ, and then youâre passing it to âselectâ flag, instead of actual attributes.
Also, in this code snippet, youâre not declaring âgChannelBoxNameâ variable anywhere, so it must give you a NameError (unless you have declared it somewhere else, or itâs already in memory of your current Maya session).
Hope it clarifies it for you a bit now.
Ok⌠That helped⌠So I,m just trying to create a shelf button, that selects my custom FK / IK switch
attribute in the channel box ( FK_to_IK). How does the code below look ?
Charcoal Its giving me error â> line 3: list index out of range
import pymel.core as pmc
import maya.mel as mel
sel = pmc.selected()[0]
user_defined_attrs = sel.listAttr(ud=True)
gChannelBoxName = mel.eval(â$temp=$gChannelBoxNameâ)
pmc.channelBox(gChannelBoxName, e=True, s=FK_to_IK)
You havenât selected anything - thatâs what IndexError is telling you in this case.
pmc.selected() returns list of all selected objects (just like pmc.ls(sl=True))
pmc.selected()[0] returns first item in that list (at index 0, since pythonâs indexing starts at 0)
When you get IndexError, it means youâre trying to access item at index that list doesnât have (like trying to access third element in a list that has only 2 items in it).
In this case, since youâre trying to access first element (index 0), and youâre getting IndexError, it means you donât have even one element in a list - meaning, your list is empty (you havenât selected anything).
So, just select your object (that has user defined attributes), and then press shelf button.
Ok, thanks for bearing with me. Yes you are right, I didnât have anything selected.
I ran it again using the code below with the group node selected, now im getting
this error: line 6: name âFK_to_IKâ is not defined. But this is the name of my custom
attribute inside of my group node.
import pymel.core as pmc
import maya.mel as mel
sel = pmc.selected()[0]
user_defined_attrs = sel.listAttr(ud=True)
gChannelBoxName = mel.eval(â$temp=$gChannelBoxNameâ)
pmc.channelBox(gChannelBoxName, e=True, s=FK_to_IK)
Ultimately I am trying to get away from having to select anything, just the shelf
button. So by pressing the shelf button,
the group node ( L_Arm_IK_FK_GRP) and the
custom attribute NAME would select. Can you show me how I could do that ?
Would you be interested in helping me on more complex Python problems,
I, of course, would pay for your time. ???
Not sure why you keep changing the code?
So, either:
in the last line,âs=FK_to_IKâ, change to âs=user_defined_attrsâ
OR
the 4th line, user_defined_attrs = sel.listAttr(ud=True), change to FK_to_IK = sel.listAttr(ud=True)
That should work for your original question (selecting custom attributes in channel box; all of custom attributes, not just one).
Now, from your last reply, looks like you donât need that exactly; rather you want to select specific attribute (âFK_to_IKâ in this case - itâs irrelevant whether itâs custom or not).
For that matter you donât need to query user defined attributes at all, using âlistAttrâ command. Furthermore, it would create issues for your functionality (whatever that might be), since it will list all custom attributes, and select them afterwards.
***NOTE: Channel box is something like realtime view of your active selection. So if you donât want to have anything selected prior to hitting shelf button, then youâll have to select that group node from code also, since channel box wonât show anything if nothingâs selected.
Barebone hard-coded script, without any dignity in it, to select âFK_to_IKâ attribute in channel box on âL_Arm_IK_FK_GRPâ (if you can guarantee that this object exists, and itâs name is unique in the scene):
import pymel.core as pmc
import maya.mel as mel
pmc.select('L_Arm_IK_FK_GRP')
gChannelBoxName = mel.eval('$temp=$gChannelBoxName')
select = "pmc.channelBox(gChannelBoxName, e=True, s='L_Arm_IK_FK_GRP.FK_to_IK')"
pmc.evalDeferred(select)
I would advice you to avoid this kind of scripting, and, for your case, go with either providing some simple UI for entering name of object and name of attribute, perhaps (so you could select different attributes on different objects, without having to change code each time), or create version based on current selection (in latter case, for example, you could have 2 groups, one for left side, one for right, both having this attribute; and you could select either one, or both of them (since selection of common attributes for multiple selection is allowed in the channel box)).
P.S. Think you could ask here freely questions about complex (or simple) problems you face, and someone would surely try to help you. And others could benefit from it also.
IncredibleâŚthat was it. Worked perfectly. I am studying that code now. I understand what you mean
about instead creating a simple UI. With your current code below, do you think the eval() function is safe to use ?
import pymel.core as pmc
import maya.mel as mel
pmc.select(âL_Arm_IK_FK_GRPâ)
gChannelBoxName = mel.eval(â$temp=$gChannelBoxNameâ)
select = âpmc.channelBox(gChannelBoxName, e=True, s=âL_Arm_IK_FK_GRP.FK_to_IKâ)â
pmc.evalDeferred(select)