How to list custom attribute short names?

How to list custom attribute short names?

malcolm_341
Collaborator Collaborator
2,006 Views
6 Replies
Message 1 of 7

How to list custom attribute short names?

malcolm_341
Collaborator
Collaborator

Hi, from the Maya help file it says I can list the short names of custom attributes on a mesh using this command listAttr -userDefined -shortNames 

When I use that command it always returns the long names, is this a bug, or is there a trick or something to get it to work?

Accepted solutions (1)
2,007 Views
6 Replies
Replies (6)
Message 2 of 7

Kahylan
Advisor
Advisor

Hi!

 

How did you create the attribute?

 

The thing is that there are 3 types of attribute names in Maya. Longnames (Name Stored in the Node), Nicenames (Name displayed in channelbox and Attribute Editor) and short names (Name that can be used to address the attribute via code).

Long and nice name can be set through the UI, shortnames can only be set using the addAttr command using the -sn flag.

Could it be that you are confusing Nicename and Shortname? because I could not reproduce this issue. If I set the shortname using the addAttr command, maya (2022.4) will reliably return it when I use the listAttr command.

 

I hope it helps!

0 Likes
Message 3 of 7

malcolm_341
Collaborator
Collaborator

It's entirely possible I'm confused this is the first time I've worked with extra attributes. A person gave me a scene with the attributes already added and I'm trying to query the nice name I guess which I had thought was the short name. Is there a way to query the nice names for the extra attributes of a mesh and store them in an array similar to

listAttr -userDefined $meshName[0];

Message 4 of 7

Kahylan
Advisor
Advisor

You can use listAttr to get the userdefined attributes and then loop through them using attributeName.

 

Sadly I'm in a bit of a hurry today, so I only had time to write this in python. But I know you are good enough in MEL to convert this.

 

 

import maya.cmds as mc

sl = mc.ls(sl=True)[0]

attrs = mc.listAttr(sl,ud= True)

niceNameList = []
for a in attrs:
    nn = mc.attributeName("{0}.{1}".format(sl, a), n= True)
    niceNameList.append(nn)
    
print(niceNameList)

 

 

I hope it helps!

Message 5 of 7

malcolm_341
Collaborator
Collaborator

@Kahylanthanks for your help, I can kind of read the Python, but not really, it did lead me to a solution though. Maybe this can be written in less lines, but it seems to do the trick.

    //Get mesh name
    string $meshName[] = `ls -sl`;

    //Get long attribute names
    string $attrLongName[] = `listAttr -userDefined $meshName[0]`;

    //Set up arrays and counters
    int $counter = 0;
    string $attrNiceName[] = {};
    string $tempArray[] = {};
    
    //Loop to get nice names    
    for ($item in $attrLongName)
    {
        //Get nice name
        string $tempString = `attributeQuery -node $meshName[0] -niceName $attrLongName[$counter]`;
        //Convert string to string array
    	$tempArray = stringToStringArray($tempString, " ");
    	//Append to array
        appendStringArray($attrNiceName, $tempArray, 1);
        $counter = $counter + 1;
        
        //Print contents
        print $attrNiceName;
    }

 

 

0 Likes
Message 6 of 7

Kahylan
Advisor
Advisor
Accepted solution

Adding new items to an array is a bit cluncky in MEL.

This way is a bit more efficient:

    //Get mesh name
    string $meshName[] = `ls -sl`;

    //Get long attribute names
    string $attrLongName[] = `listAttr -userDefined $meshName[0]`;

    //Set up arrays and counters
    string $attrNiceName[] = {};
    
    //Loop to get nice names    
    for ($item in $attrLongName)
    {
        string $niceName = `attributeName -n ($meshName[0] +"."+ $item)`;
    	//Append to array
        $attrNiceName[size($attrNiceName)] = $niceName;
        
        //Print contents
        print $attrNiceName;}

 

Also if you want to allow multi-selection it would look like this:

    //Get mesh name
    string $meshNames[] = `ls -sl`;
    string $attrNiceName[] = {};
    
    for ($mesh in $meshNames){
        
            //Get long attribute names
            string $attrLongName[] = `listAttr -userDefined $mesh`;
        
            //Set up arrays and counters
        
            
            //Loop to get nice names    
            for ($item in $attrLongName)
            {
                string $niceName = `attributeName -n ($mesh +"."+ $item)`;
            	//Append to array
                $attrNiceName[size($attrNiceName)] = $niceName;
                
                //Print contents
                print $attrNiceName;}
    }

 

I hope it helps!

Message 7 of 7

malcolm_341
Collaborator
Collaborator

Oh cool thanks for the optimization and the original help.