Thursday, July 11, 2013

cool python comprehension and maya.cmds


here is a cool and simple way of combining python comprehension and maya.cmds. comprehension is an concise way of populating lists, which means that in a few lines you can run loops and gather elements from, in this case, a few maya commands.

in this example i needed to find all the skinned objects whose weights where not normalised as 'interactive'. therefore, after gathering all objects in the scene, i need to: -find all skinClusters. -check normalizeWeights. -return the transform linked to the skinCluster.

import maya.cmds as mc

def findOddNomalizedSkin():
    # get all shapes
    allshapes = ['mesh','nurbsSurface','nurbsCurve','lattice'])

    # get all skinClusters
    history = [a for a in mc.listHistory(allshapes, pruneDagObjects=True)]
    sCluster = [h for h in history if mc.nodeType(h) == 'skinCluster']
    # check all normalizeWeights
    oddSkinned = [s for s in sCluster if mc.getAttr(s+'.normalizeWeights') != 1]
    # get all transforms if oddSkinned is not empty
        oddSkinnedTransforms = [o for o in mc.listConnections(oddSkinned, type='mesh')]
        return oddSkinnedTransforms
        return None

with what is essentially 4 lines of code i have run a bunch of loops in one-liners via list-returning cmds. this is an example that can be applied to loads of stuff.

keep in mind that for the sake of the example i have avoided doing this to gather my skinClusters:
sCluster ='skinCluster')
in the grand scheme of things one might have separate functions that gather all skinClusters, related shapes, transforms and so on, as they are quite recurrent things, but say this, can be used as series of shell buttoned stuff for checking and pruning.