Selecting a custom attribute using Python

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.

1 Like

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)