Trying to get only names from cmds.listrelatives command

Trying to get only names from cmds.listrelatives command

joshua.zaparaniuk01
Explorer Explorer
895 Views
5 Replies
Message 1 of 6

Trying to get only names from cmds.listrelatives command

joshua.zaparaniuk01
Explorer
Explorer

I'm really new to using python in Maya, but right now when I'm using list relatives, I'm getting back: [u['l_shoulder'], u'l_elbow', u'l_wrist' and was wondering how I would just get the names and have it look like [l_shoulder, l_elbow, l_wrist]. 

0 Likes
Accepted solutions (1)
896 Views
5 Replies
Replies (5)
Message 2 of 6

mcw0
Advisor
Advisor

Ah, that also got me when I started with Python.  The "u" isn't really important.  If you run the following, you'll see you are getting what you want.

for x in yourArray:

    print x

0 Likes
Message 3 of 6

joshua.zaparaniuk01
Explorer
Explorer

The issue is that I'm using it in naming objects that I'm creating, and so right now when I'm creating them it has the u and a bunch of underscores. 

0 Likes
Message 4 of 6

mcw0
Advisor
Advisor

Can you provide an example?

0 Likes
Message 5 of 6

joshua.zaparaniuk01
Explorer
Explorer

this code is the main issue, im trying to name joints based on the gathered name, but because of the way the listrelatives function works its giving the joint the name _u__l_shoulder__ik instead of just l_shoulder_ik

 

jointRoot = cmds.ls(sl=1, type="joint", o=True)
tempjointList = cmds.listRelatives(jointRoot, ad=1, type="joint") ##Gather a list of selected joints
tempjointList.append(jointRoot)
rootParent = cmds.listRelatives(jointRoot, p=1, type="joint")

if AOJLmb == 2:
    jointList = [tempjointList[2], tempjointList[1], tempjointList[0]]
if AOJLmb == 3:
    jointList = [tempjointList[3], tempjointList[2], tempjointList[1], tempjointList[0]]

if "l_" in jointRoot:
    prefix = "l_"
if "r_" in jointRoot:
    prefix = "r_"
namingCon = prefix+"arm_"

if IKFK == 1:##see if ikfk checkbox has been selected
    newJointsList = ["_ik", "_fk", "_SquashAndStretch"] ##create the ik, fk, squash and stretch lines to be used when ##creating the joints

    for d in range(3):
        cmds.select(cl=True)
        for p in range(AOJLmb): ##go through each joint in the joint chain
            newJointName = str(jointList [p]) + newJointsList[d]
            cmds.joint(name = newJointName)
            cmds.matchTransform(newJointName, jointList [p])
            cmds.makeIdentity(newJointName, a=1, t=0, r=1, s=0)

0 Likes
Message 6 of 6

Kahylan
Advisor
Advisor
Accepted solution

Hi!

 

You could have Mentioned that Maya gives you the Error:

# Error: TypeError: file <maya console> line 24: Object ['l_shoulder']_ik is invalid

That usually helps finding problems.^^

 

What is happening here is that the ls() function always gives you a list, so when you use the line:

tempjointList.append(jointRoot)

You are appending a list within a list. Instead of appending the string in the list to the list.

 

You can either use:

tempjointList.append(jointRoot[0])

if you want to have the selection limited to the first selected element. which would make sense with the rest of the code

or you can use:

tempjointList = tempjointList + rootJoint

This merges the lists, which gives the possibility of multiple selections. But since you are directly relying on list index later on thats probably not what you want here.

 

Also, you'll need to use rootJoint[0] as well for your if statements, otherwise they will never activate.

 

I hope this helps!

0 Likes