#include <maya/MPxCommand.h>
#include <maya/MArgList.h>
#include <maya/MArgParser.h>
#include <maya/MDagPath.h>
#include <maya/MItSelectionList.h>
#include <maya/MSelectionList.h>
#include <maya/MFnPlugin.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MPlugArray.h>
#include <maya/MPlug.h>
#include <maya/MItDependencyGraph.h>
#include <maya/MString.h>
#include <maya/MObjectArray.h>
#include <maya/MDoubleArray.h>
#include <maya/MDagPathArray.h>
#include <maya/MGlobal.h>
#include <maya/MFnSkinCluster.h>
#include <maya/MItMeshVertex.h>
#include <maya/MItSurfaceCV.h>
#include <maya/MItCurveCV.h>
#include <math.h>
#define kEditFlag "-e"
#define kEditFlagLong "-edit"
#define kQueryFlag "-q"
#define kQueryFlagLong "-query"
#define kInfluenceFlag "-inf"
#define kInfluenceFlagLong "-influences"
#define kSkinClusterFlag "-sc"
#define kSkinClusterFlagLong "-skinClusters"
#define kWeightFlag "-w"
#define kWeightFlagLong "-weights"
#define kAssignAllToSingleFlag "-as"
#define kAssignAllToSingleFlagLong "-assignAllToSingle"
{
public:
skinClusterWeights();
~skinClusterWeights() override;
static void* creator();
private:
void doItEdit();
void doItQuery();
bool editUsed;
bool queryUsed;
bool isSkinClusterIncluded(
MObject&);
bool assignAllToSingle;
};
skinClusterWeights::skinClusterWeights() : assignAllToSingle(false)
{
fInfluenceIndexArrayPtrArray = NULL;
fWeightsPtrArray = NULL;
}
skinClusterWeights::~skinClusterWeights()
{
if (fInfluenceIndexArrayPtrArray != NULL) delete [] fInfluenceIndexArrayPtrArray;
if (fWeightsPtrArray != NULL) delete [] fWeightsPtrArray;
}
void* skinClusterWeights::creator()
{
return new skinClusterWeights;
}
{
editUsed = true;
queryUsed = false;
unsigned int i, nth = 0;
unsigned int numArgs = args.
length();
while(status == MS::kSuccess && nth < numArgs-1) {
if (status != MS::kSuccess) {
return status;
}
if (inputString == kEditFlag || inputString == kEditFlagLong) {
editUsed = true;
queryUsed = false;
nth++;
continue;
}
if (inputString == kQueryFlag || inputString == kQueryFlagLong) {
queryUsed = true;
editUsed = false;
nth++;
continue;
}
if (inputString == kInfluenceFlag || inputString == kInfluenceFlagLong) {
nth++;
for (i = 0; i < stringArray.
length(); i++) {
selList.
add(stringArray[i]);
}
for (i = 0; i < selList.
length(); i++) {
influenceArray.append(dagPath);
} else {
return status;
}
}
nth++;
continue;
}
if (inputString == kSkinClusterFlag || inputString == kSkinClusterFlagLong) {
nth++;
for (i = 0; i < stringArray.
length(); i++) {
selList.
add(stringArray[i]);
}
for (i = 0; i < selList.
length(); i++) {
skinClusterArray.append(node);
} else {
return status;
}
}
nth++;
continue;
}
if (inputString == kWeightFlag || inputString == kWeightFlagLong) {
nth++;
if (status != MS::kSuccess) {
}
nth++;
continue;
}
if (inputString == kAssignAllToSingleFlag || inputString == kAssignAllToSingleFlagLong) {
assignAllToSingle = true;
nth++;
continue;
}
return MS::kFailure;
}
if (status != MS::kSuccess) {
return status;
}
if (queryUsed) {
if (assignAllToSingle) {
}
if (weightArray.length() > 0) {
}
}
return status;
}
{
if (status != MS::kSuccess) return status;
if (editUsed) {
redoIt();
} else if (queryUsed) {
doItQuery();
}
return MS::kSuccess;
}
MStatus skinClusterWeights::redoIt()
{
unsigned int ptr = 0;
int geomLen = geometryArray.
length();
fDagPathArray.setLength(geomLen);
fComponentArray.setLength(geomLen);
fInfluenceIndexArrayPtrArray =
new MIntArray[geomLen];
for (int i = 0; i < geomLen; i++) {
selList.
add(geometryArray[i]);
if (status != MS::kSuccess) {
continue;
}
MObject skinCluster = findSkinCluster(dagPath);
if (!isSkinClusterIncluded(skinCluster)) {
continue;
}
if (status != MS::kSuccess) {
continue;
}
populateInfluenceIndexArray(skinClusterFn, influenceIndexArray);
unsigned numInf = influenceIndexArray.
length();
if (numInf == 0) continue;
unsigned numCV = 0;
if (status == MS::kSuccess) {
numCV = polyIter.count();
}
if (status == MS::kSuccess) {
while (!nurbsIter.isDone()) {
numCV++;
nurbsIter.next();
}
}
MItCurveCV curveIter(dagPath, component, &status);
if (status == MS::kSuccess) {
while (!curveIter.isDone()) {
numCV++;
curveIter.next();
}
}
}
unsigned numEntry = numCV * numInf;
if (numEntry > 0) {
unsigned int numWeights = weightArray.length();
if (assignAllToSingle) {
if (numInf <= numWeights) {
for (unsigned j = 0; j < numEntry; j++) {
weights[j] = weightArray[j % numInf];
}
} else {
return MS::kFailure;
}
} else {
for (unsigned j = 0; j < numEntry; j++, ptr++) {
if (ptr < numWeights) {
weights[j] = weightArray[ptr];
} else {
return MS::kFailure;
}
}
}
fDagPathArray[i] = dagPath;
fComponentArray[i] = component;
fInfluenceIndexArrayPtrArray[i] = influenceIndexArray;
skinClusterFn.getWeights(dagPath, component, influenceIndexArray, oldWeights);
fWeightsPtrArray[i] = oldWeights;
skinClusterFn.setWeights(dagPath, component, influenceIndexArray, weights);
}
}
return MS::kSuccess;
}
MStatus skinClusterWeights::undoIt()
{
for (unsigned i = 0; i < fDagPathArray.length(); i++) {
MObject skinCluster = findSkinCluster(dagPath);
if (!isSkinClusterIncluded(skinCluster)) {
continue;
}
if (status != MS::kSuccess) {
continue;
}
MObject &component = fComponentArray[i];
fInfluenceIndexArrayPtrArray[i].length() > 0 && fWeightsPtrArray[i].length() > 0) {
skinClusterFn.setWeights(dagPath, component, fInfluenceIndexArrayPtrArray[i], fWeightsPtrArray[i]);
}
}
fDagPathArray.clear();
fComponentArray.clear();
delete [] fInfluenceIndexArrayPtrArray;
delete [] fWeightsPtrArray;
fInfluenceIndexArrayPtrArray = NULL;
fWeightsPtrArray = NULL;
return MS::kSuccess;
}
void skinClusterWeights::doItQuery()
{
unsigned int i, j;
setResult(weights);
for (i = 0; i < geometryArray.length(); i++) {
selList.
add(geometryArray[i]);
if (status != MS::kSuccess) {
continue;
}
MObject skinCluster = findSkinCluster(dagPath);
if (!isSkinClusterIncluded(skinCluster)) {
continue;
}
if (status != MS::kSuccess) {
continue;
}
populateInfluenceIndexArray(skinClusterFn, influenceIndexArray);
skinClusterFn.getWeights(dagPath, component, influenceIndexArray, weights);
for (j = 0; j < weights.
length(); j++) {
appendToResult(weights[j]);
}
}
}
}
{
if (!dgIt.isDone()) {
skinCluster = dgIt.currentItem();
}
return skinCluster;
}
bool skinClusterWeights::isSkinClusterIncluded(
MObject &node)
{
unsigned int i;
if (skinClusterArray.length() == 0) return true;
for (i = 0; i < skinClusterArray.length(); i++) {
if (skinClusterArray[i] == node) return true;
}
return false;
}
void skinClusterWeights::populateInfluenceIndexArray(
MFnSkinCluster &skinClusterFn,
MIntArray &influenceIndexArray)
{
for (
unsigned j = 0; j < pathArray.
length(); j++) {
}
if (influenceArray.length() > 0) {
for (unsigned j = 0; j < influenceArray.length(); j++) {
for (
unsigned k = 0; k < allIndexArray.
length(); k++) {
if ((int)index == allIndexArray[k]) {
influenceIndexArray.
append(k);
}
}
}
} else {
for (
unsigned j = 0; j < allIndexArray.
length(); j++) {
influenceIndexArray.
append(j);
}
}
}
{
status = plugin.registerCommand( "skinClusterWeights",
skinClusterWeights::creator);
if (!status) {
status.
perror(
"registerCommand");
return status;
}
return status;
}
{
status = plugin.deregisterCommand( "skinClusterWeights" );
if (!status) {
status.
perror(
"deregisterCommand");
return status;
}
return status;
}