Hi, I have a problem understanging the getWeights()
and setWeights()
methods of MFnSkinCluster
. According to the docs (MFnSkinCluster) you’re supposed to provide MDagPath to the shape node and an MObject of components to get/set weights for. The name ‘components’ suggests you can provide more than one which I don’t know how to do.
The way I’ve beed doing it so far is by using MItMeshVertex
to get/set weights on single vertices in an iteration, which is terribly slow.
My typical approach looks like this:
import maya.api.OpenMaya as om
import maya.api.OpenMayaAnim as oma
import json
# the file provides a float[i][j] array
# where i = vertex index and j = influence index
file_path = 'path/to/a/file.json'
def set_weights_by_index():
# get weights from file
with open(file_path, 'r') as file_in:
weights = json.load(file_in)
# find shape node and create the iterator
sel_list = om.MGlobal.getActiveSelectionList()
sel_dag_path = sel_list.getDagPath(0)
shape_node_dag = sel_dag_path.extendToShape()
vert_iterator = om.MItMeshVertex(shape_node_dag)
# traverse the DG to find skin cluster node
shape_node = shape_node_dag.node()
dg_iterator = om.MItDependencyGraph(
shape_node,
om.MFn.kSkinClusterFilter,
om.MItDependencyGraph.kUpstream,
om.MItDependencyGraph.kPlugLevel)
while not dg_iterator.isDone():
current_node = dg_iterator.currentNode()
if not current_node.isNull():
skin_cluster_node = current_node
dg_iterator.next()
# iterate over vertices to set the weights
skin_cluster_fn = oma.MFnSkinCluster(skin_cluster_node)
while not vert_iterator.isDone():
current_index = vert_iterator.index()
weights_to_set = om.MDoubleArray(weights[current_index])
influence_indices = om.MIntArray([i for i in range(0, len(weights_to_set))])
skin_cluster_fn.setWeights(
shape_node_dag,
vert_iterator.currentItem(),
influence_indices,
weights_to_set,
True,
False)
vert_iterator.next()
set_weights_by_index()
As you can see I’m getting the current vertex with MItMeshVertex.currentItem()
and just setting the weights one by one. I’m pretty sure this is not the way to do this, but I have no idea how to get a single MObject representing a whole set of vertices. Does anyone have experience with this?