Getting Cluster Weights using Pymel

I am trying to get the weights of a cluster using pymel.


cluster = pm.ls(sl=True)
cluster[0].getWeights()

I looked at the help and it gives me this.


Help on method getWeights in module pymel.internal.factories:

getWeights(self, index) method of pymel.core.nodetypes.Cluster instance
    Gets the weights of the components that correspond to the geometry at the specified plug index.
    
    :Parameters:
        index : `int`
            the plug index corresponding to the shape that has the components 
    
    
    :rtype: (`PyNode`, `float` list)
    
    Derived from api method `maya.OpenMayaAnim.MFnWeightGeometryFilter.getWeights`

I ended up doing this but I am getting an error.


cluster[0].getWeights(0)

What does this mean?
"the plug index corresponding to the shape that has the components "

Deformers in maya have an input array attribute, where each deformed shape is connected to a different index of the input array. The plug index the help docs is referring to is the index of the input array that the shape you want to query is connected to. 0 should be the first item being deformed, and my guess is that the error you are getting is a bug in PyMel, and not because you are doing something wrong. I got this to work using the API, so I’d recommend trying it with that. Here’s a quick example using PyMel’s mobject methods as shortcuts. (The PyMel docs have this to say about these methods: "Be aware that this is still considered internal magic, and the names of these methods are subject to change ") I’d probably recommend just using straight API commands, but as an example this was easier for me to set up.


import maya.OpenMaya as om
import pymel.core as pm

def getClusterWeights(cluster, index):
    cluster = pm.PyNode(cluster)
    mobjComponents = cluster.deformerSet().members()[index].__apimobject__()
    mfnDeformer = cluster.__apimfn__()
    weights = om.MFloatArray()
    mfnDeformer.getWeights(index, mobjComponents, weights)
    
    return list(weights)

Thanks for the information capper! I haven’t dived that much into the API so maybe this is good starting place. :slight_smile: