0

How to change the weights of a deformer from a script in Maya?

This question is ideally for Pymel in Maya 2013, but if it is not possible I would still be interested to know the answer in Python, MEL, or using the C++ Maya-API.

Vincent Cantin
  • 16,192
  • 2
  • 35
  • 57

1 Answers1

3

for deformers, you can query weight in python as :

VertexNb = cmds.polyEvaluate(Mesh, v=1) - 1
weight = cmds.getAttr('{0}.weightList[0].weights[0:{1}]'.format(deformerNode, VertexNb))

for blendshape :

VertexNb = cmds.polyEvaluate(Mesh, v=1)
weight = cmds.getAttr('{0}.inputTarget[0].baseWeights[0:{1}]'.format(blendShapeNode, VertexNb))

To set value :

cmds.setAttr('{0}.weightList[0].weights[0:{1}]'.format(deformerNode, VertexNb), *weight, size=len(weight))
DrWeeny
  • 2,487
  • 1
  • 14
  • 17
  • 1
    you may be tempted to use .weightList[0].weights[:] but maya do not return all the values everytime – DrWeeny Nov 18 '15 at 10:04
  • I would like to verify your answer before I mark it as a solution, but I am puzzled by the `*` in `*weight`. What is its meaning in Python? – Vincent Cantin Nov 27 '15 at 02:35
  • 1
    *weight is pythonic, it is used to unpack value. as example, when you use : def myFunc(*args), you see the same use of *, the double star ** is used with kwargs (dictionnary) – DrWeeny Nov 27 '15 at 19:23
  • It tested only the query of the weights so far. I would like to precise that the attribut `.weightList[0].weights` will return 1 for all indices which are not in the deformed set. We can have the indices of deformed vertices in Pymel using `.weightList[0].weights.getArrayIndices()` – Vincent Cantin Dec 03 '15 at 03:24