instancerListCmd.cpp
#include <maya/MSimple.h>
#include <maya/MSelectionList.h>
#include <maya/MGlobal.h>
#include <maya/MItSelectionList.h>
#include <maya/MString.h>
#include <maya/MFnInstancer.h>
#include <maya/MItInstancer.h>
#include <maya/MDagPathArray.h>
#include <maya/MPoint.h>
#include <maya/MMatrixArray.h>
DeclareSimpleCommand( listParticleInstances, PLUGIN_COMPANY, "8.0");
void printInstancerUsingFunctionSet( const MDagPath& instancerPath )
{
char str[256];
MString pathName = instancerPath.fullPathName();
sprintf( str, "Instancer %s:", pathName.asChar() );
MGlobal::displayInfo( MString(str) );
MFnInstancer fnInst( instancerPath );
int numParticles = fnInst.particleCount();
sprintf( str, " num particles = %d", numParticles );
MGlobal::displayInfo( " Using instancesForParticle()...." );
MMatrix instancerWorldMatrix = instancerPath.inclusiveMatrix();
int p = 0;
for( p = 0; p < numParticles; p++ )
{
MMatrix particleMatrix;
MDagPathArray particlePaths;
int numInstances = fnInst.instancesForParticle( p, particlePaths, particleMatrix );
for( int i = 0; i < numInstances; i++ )
{
const MDagPath& instancedPath = particlePaths[i];
MMatrix instancedPathMatrix = instancedPath.inclusiveMatrix();
MMatrix finalMatrixForPath = instancedPathMatrix * particleMatrix;
MPoint finalPoint = MPoint::origin * finalMatrixForPath;
MString instancedPathName = instancedPath.fullPathName();
sprintf( str, " Path %-50s at position (%lf,%lf,%lf)", instancedPathName.asChar(), finalPoint.x, finalPoint.y, finalPoint.z );
MGlobal::displayInfo( str );
}
}
MGlobal::displayInfo( " Using allInstances()...." );
MDagPathArray allPaths;
MMatrixArray allMatrices;
MIntArray pathIndices;
MIntArray pathStartIndices;
fnInst.allInstances( allPaths, allMatrices, pathStartIndices, pathIndices );
for( p = 0; p < numParticles; p++ )
{
MMatrix particleMatrix = allMatrices[p];
int numPaths = pathStartIndices[p+1]-pathStartIndices[p];
int pathStart = pathStartIndices[p];
for( int i = pathStart; i < pathStart+numPaths; i++ )
{
int curPathIndex = pathIndices[i];
const MDagPath& curPath = allPaths[curPathIndex];
MMatrix instancedPathMatrix = curPath.inclusiveMatrix();
MMatrix finalMatrixForPath = instancedPathMatrix * particleMatrix;
MPoint finalPoint = MPoint::origin * finalMatrixForPath;
MString instancedPathName = curPath.fullPathName();
sprintf( str, " Path %-50s at position (%lf,%lf,%lf)", instancedPathName.asChar(), finalPoint.x, finalPoint.y, finalPoint.z );
MGlobal::displayInfo( str );
}
}
}
void printAllInstancesUsingIterator()
{
MItInstancer it;
while( !it.isDone() )
{
MObject instancerNode = it.instancer();
MDagPath instancerPath = it.instancerPath();
MDagPath instancePath = it.path();
MMatrix instanceMatrix = it.matrix();
MString instancerNodeName = MFnDependencyNode(instancerNode).name();
MString instancerPathName = instancerPath.fullPathName();
MString instancePathName = instancePath.fullPathName();
MMatrix pathMatrix = instancePath.inclusiveMatrix();
MMatrix finalMatrixForPath = pathMatrix * instanceMatrix;
MPoint pos = MPoint::origin * finalMatrixForPath;
char str[512];
sprintf( str, "Instancer node %s, instancer path %s, instancing path %s at position (%lf,%lf,%lf)",
instancerNodeName.asChar(), instancerPathName.asChar(), instancePathName.asChar(), pos.x, pos.y, pos.z );
MGlobal::displayInfo( MString(str) );
it.next();
}
}
MStatus listParticleInstances::doIt( const MArgList& )
{
MSelectionList curSel;
MGlobal::getActiveSelectionList( curSel );
MItSelectionList it( curSel, MFn::kInstancer );
if( it.isDone() )
{
MGlobal::displayInfo( "Using iterator to enumerate all particle instances..." );
printAllInstancesUsingIterator();
}
else
{
while( !it.isDone() )
{
MDagPath instancerPath;
if( it.getDagPath( instancerPath ) == MS::kSuccess )
{
printInstancerUsingFunctionSet( instancerPath );
}
else
{
MGlobal::displayError( "Error retrieving instancer" );
return MS::kFailure;
}
it.next();
}
}
return MS::kSuccess;
}