Skip to content

Instantly share code, notes, and snippets.

@nathanielanozie
Created February 19, 2015 01:14
Show Gist options
  • Save nathanielanozie/91cf4da13651bc200e94 to your computer and use it in GitHub Desktop.
Save nathanielanozie/91cf4da13651bc200e94 to your computer and use it in GitHub Desktop.
this test program prints all the weights for all vertices of selected polygon shape node
//this program prints all the weights for all vertices of selected polygon shape node
/*
This file is part of printWeightCmd.
printWeightCmd is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/lgpl.html>.
Author: Nathaniel Anozie ogbonnawork at gmail dot com
Url: nathananozie dot blogspot dot com
Date: 2015 / 02 / 18
*/
//Inspired by: Bruno Heidelberger's online tutorials on find skinclusters on scene
//02-18-2015 working on initial release
#include <maya/MSimple.h>
#include <maya/MGlobal.h>
#include <maya/MItSelectionList.h>
#include <maya/MItDependencyNodes.h>
//for poly and vertex query stuff
#include <maya/MFnMesh.h>
#include <maya/MPointArray.h>
//skin related
//OpenMayaAnim -- Needed for skin related stuff
#include <maya/MFnSkinCluster.h>
#include <maya/MDagPath.h>
#include <maya/MDagPathArray.h>
#include <maya/MItGeometry.h>
#include <maya/MDoubleArray.h>
//help with error
#define CheckError(stat,msg) \
if ( MS::kSuccess != stat ) { \
displayError(msg); \
}
// continue; \
DeclareSimpleCommand( printWeightCmd, "Test SkinCluster Weight Printing", "1.0");
MStatus printWeightCmd::doIt( const MArgList& args )
{
MStatus stat = MS::kSuccess;
MString eMessage = "Required a Poly Selected !!!\n ";
//get selected poly
MSelectionList sel;
MGlobal::getActiveSelectionList(sel);
if(sel.length() == 1){
MObject obj;
sel.getDependNode(0,obj);
MFnMesh meshFn( obj );
if(obj.apiType()==MFn::kMesh){
//we have our poly
MGlobal::displayInfo("Found Polygon Computing Vertex Translation ... \n");
//we will search all the skinclusters in scene and see which one is attached to our mesh
MObject skinClusterObj;
bool hasSkinCluster = false;
MItDependencyNodes kDepNodeIt( MFn::kSkinClusterFilter);
//stop when found first skin cluster
for( ; !kDepNodeIt.isDone() && !hasSkinCluster; kDepNodeIt.next() )
{
MGlobal::displayInfo( MString("Finding Skincluster:")+"\n" );
MObject kObject = kDepNodeIt.item(); //get candidate
MFnSkinCluster kSkinClusterFn( kObject, &stat );
CheckError( stat, "Error geting candidate SkinCluster path" );
unsigned int numSkinOutput = kSkinClusterFn.numOutputConnections();
//go through each connection and see if it matches our mesh
for( unsigned int m = 0; m < numSkinOutput && !hasSkinCluster; ++m )
{
unsigned int candidateIndex = kSkinClusterFn.indexForOutputConnection( m, &stat );
//if the skincluster is part of two geometries this could be 0 or 1,
CheckError( stat, "Error geting candidate SkinCluster index" );
MObject candidateInputObject = kSkinClusterFn.inputShapeAtIndex( candidateIndex, &stat );
MObject candidateOutputObject = kSkinClusterFn.outputShapeAtIndex( candidateIndex, &stat);
if( candidateOutputObject == meshFn.object() )
{
hasSkinCluster = true; //found a skin cluster
skinClusterObj = kObject;//this is our skin cluster
}
}
}
//get skin path so can iterate to get weights
//expects a MFn::kSkinClusterFilter
MFnSkinCluster skinCluster(skinClusterObj);
unsigned int index = skinCluster.indexForOutputConnection( 0, &stat);
CheckError( stat, "Error with SkinCluster index" );
MDagPath boundGeoPath;
stat = skinCluster.getPathAtIndex( index, boundGeoPath);
CheckError( stat, "Error geting Skincluster output geometry path" );
//now we should have our skincluster path
MGlobal::displayInfo( MString("Great Found Geo with Skin:")+ boundGeoPath.partialPathName().asChar() +"\n" );
//get skin cluster influences
MDagPathArray jointInfluences;//will hold influences
unsigned int numInf = skinCluster.influenceObjects( jointInfluences, &stat);
CheckError( stat, "Error getting influences" );
if( numInf == 0)
{
stat = MS::kFailure;
CheckError(stat, "Error no influence objects found!!!");
}
MGlobal::displayInfo( MString("Great Found Influences:")+"\n" );
for( unsigned int i = 0; i < numInf; ++i)
{
MGlobal::displayInfo( MString("")+jointInfluences[i].partialPathName().asChar() );
}
MGlobal::displayInfo( MString("")+"\n" );
//get weights for all vertices
//need to iterate over vertices
MItGeometry boundGeoIter( boundGeoPath );
for( /*nothing*/; !boundGeoIter.isDone(); boundGeoIter.next() )
{
MObject vtx = boundGeoIter.component(&stat);
CheckError(stat, "Error getting vertex of skin");
//get one weight per each influence object
MDoubleArray wts;
unsigned int numInfVtx;
stat = skinCluster.getWeights( boundGeoPath, vtx, wts, numInfVtx);
CheckError(stat,"Error getting weights");
if( numInfVtx == 0 ){
stat = MS::kFailure;
CheckError(stat, "Error no influence objects found for a vtx");
}
//print weights
//MString str = ""+MString()+(pnt[i].x)+","+MString()+(pnt[i].y)+","+MString()+(pnt[i].z)+"\n";
//MGlobal::displayInfo(str);
MGlobal::displayInfo( MString("vtx index:")+ boundGeoIter.index() +"\n" );
MGlobal::displayInfo( MString("wts:")+"\n" );
for( unsigned int j = 0; j < numInfVtx; ++j)
{
MGlobal::displayInfo( MString("")+wts[j]+" " );//space between weights
}
}
//end weight related section
}
else{
displayError("Requires a Poly with Skin Selected !!!\n");
return MS::kFailure;
}
}
else{
displayError("Requires a Poly Shape Selected !!!\n");
return MS::kFailure;
}
return stat;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment