splitUVNode.cpp
#include "splitUVNode.h"
#include <maya/MFnTypedAttribute.h>
#include <maya/MFnMeshData.h>
#include <maya/MFnComponentListData.h>
#include <maya/MFnSingleIndexedComponent.h>
#include <maya/MGlobal.h>
#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MIOStream.h>
#define MCheckStatus(status,message) \
if( MStatus::kSuccess != status ) { \
cerr << message << "\n"; \
return status; \
}
MTypeId splitUVNode::id( 0x34567 );
MObject splitUVNode::uvList;
splitUVNode::splitUVNode()
{}
splitUVNode::~splitUVNode()
{}
MStatus splitUVNode::compute( const MPlug& plug, MDataBlock& data )
{
MStatus status = MS::kSuccess;
MDataHandle stateData = data.outputValue( state, &status );
MCheckStatus( status, "ERROR getting state" );
if( stateData.asShort() == 1 )
{
MDataHandle inputData = data.inputValue( inMesh, &status );
MCheckStatus(status,"ERROR getting inMesh");
MDataHandle outputData = data.outputValue( outMesh, &status );
MCheckStatus(status,"ERROR getting outMesh");
outputData.set(inputData.asMesh());
}
else
{
if (plug == outMesh)
{
MDataHandle inputData = data.inputValue( inMesh, &status );
MCheckStatus(status,"ERROR getting inMesh");
MDataHandle outputData = data.outputValue( outMesh, &status );
MCheckStatus(status,"ERROR getting outMesh");
MDataHandle inputUVs = data.inputValue( uvList, &status);
MCheckStatus(status,"ERROR getting uvList");
outputData.set(inputData.asMesh());
MObject mesh = outputData.asMesh();
MObject compList = inputUVs.data();
MFnComponentListData compListFn( compList );
unsigned i;
int j;
MIntArray uvIds;
for( i = 0; i < compListFn.length(); i++ )
{
MObject comp = compListFn[i];
if( comp.apiType() == MFn::kMeshMapComponent )
{
MFnSingleIndexedComponent uvComp( comp );
for( j = 0; j < uvComp.elementCount(); j++ )
{
int uvId = uvComp.element(j);
uvIds.append( uvId );
}
}
}
fSplitUVFactory.setMesh( mesh );
fSplitUVFactory.setUVIds( uvIds );
status = fSplitUVFactory.doIt();
outputData.setClean();
}
else
{
status = MS::kUnknownParameter;
}
}
return status;
}
void* splitUVNode::creator()
{
return new splitUVNode();
}
MStatus splitUVNode::initialize()
{
MStatus status;
MFnTypedAttribute attrFn;
uvList = attrFn.create("inputComponents", "ics", MFnComponentListData::kComponentList);
attrFn.setStorable(true);
inMesh = attrFn.create("inMesh", "im", MFnMeshData::kMesh);
attrFn.setStorable(true);
outMesh = attrFn.create("outMesh", "om", MFnMeshData::kMesh);
attrFn.setStorable(false);
attrFn.setWritable(false);
status = addAttribute( uvList );
if (!status)
{
status.perror("addAttribute");
return status;
}
status = addAttribute( inMesh );
if (!status)
{
status.perror("addAttribute");
return status;
}
status = addAttribute( outMesh);
if (!status)
{
status.perror("addAttribute");
return status;
}
status = attributeAffects( inMesh, outMesh );
if (!status)
{
status.perror("attributeAffects");
return status;
}
status = attributeAffects( uvList, outMesh );
if (!status)
{
status.perror("attributeAffects");
return status;
}
return MS::kSuccess;
}