Search code examples
pythonpython-2.xmaya

Maya Python skinCluster return type not string?


I'm trying to check if an object has a skinCluster on it. My code is pretty basic. Here's an example:

cmds.select(d=True)
joint = cmds.joint()
skinnedSphere = cmds.polySphere(r=2)
notSkinnedSphere = cmds.polySphere(r=2)

skinTestList = [skinnedSphere, notSkinnedSphere]

# Bind the joint chain that contains joint1 to pPlane1
# and assign a dropoff of 4.5 to all the joints
#
cmds.skinCluster( joint, skinnedSphere, dr=4.5)

for obj in skinTestList:

    objHist = cmds.listHistory(obj, pdo=True)

    skinCluster = cmds.ls(objHist, type="skinCluster")

    if skinCluster == "":
        print(obj + " has NO skinCluster, skipping.")
    else:
        print obj, skinCluster
        #cmds.select(obj, d=True)

My issue is that even if it can't find a skincluster, it still prints out the "obj, skincluster" rather than the error that it can't find a skinCluster.

I thought a skinCluster returns a string. So if the string is empty, it should print out the error rather than "obj, skincluster".

Any help would be appreciated!


Solution

  • This is a classic Maya issue -- the problem is that Maya frequently wants to give you lists, not single items, even when you know the result ought to be a single item. This means you end up writing a bunch of code to either get one item from a one-item list or to avoid errors that come from trying to get an index into an empty list.

    You've got the basics, it's the == "" which is messing you up:

    for obj in skinTestList:
         objHist = cmds.listHistory(obj, pdo=True)
         skinCluster = cmds.ls(objHist, type="skinCluster") or [None]
         cluster = skinCluster[0]
         print obj, cluster
    

    The or [None] guarantees that you'll always get a list with something in it so it's safe to use the [0] to get the single value. None is a good return value here because (as pointed out in the comments) you can if cluster: and skip empty values.