meshOpNode.cpp
#include "meshOpNode.h"
#include <maya/MFnTypedAttribute.h>
#include <maya/MFnMeshData.h>
#include <maya/MFnComponentListData.h>
#include <maya/MFnSingleIndexedComponent.h>
#include <maya/MFnEnumAttribute.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 meshOpNode::id( 0x00085000 );
MObject meshOpNode::cpList;
MObject meshOpNode::opType;
meshOpNode::meshOpNode()
{}
meshOpNode::~meshOpNode()
{}
MStatus meshOpNode::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 inputIDs = data.inputValue( cpList, &status);
MCheckStatus(status,"ERROR getting componentList");
MDataHandle opTypeData = data.inputValue( opType, &status);
MCheckStatus(status,"ERROR getting opType");
outputData.set(inputData.asMesh());
MObject mesh = outputData.asMesh();
MObject compList = inputIDs.data();
MFnComponentListData compListFn( compList );
MeshOperation operationType = (MeshOperation) opTypeData.asShort();
MFn::Type componentType =
meshOpFty::getExpectedComponentType(operationType);
unsigned i;
int j;
MIntArray cpIds;
for( i = 0; i < compListFn.length(); i++ )
{
MObject comp = compListFn[i];
if( comp.apiType() == componentType )
{
MFnSingleIndexedComponent siComp( comp );
for( j = 0; j < siComp.elementCount(); j++ )
cpIds.append( siComp.element(j) );
}
}
fmeshOpFactory.setMesh( mesh );
fmeshOpFactory.setComponentList( compList );
fmeshOpFactory.setComponentIDs( cpIds );
fmeshOpFactory.setMeshOperation( operationType );
status = fmeshOpFactory.doIt();
outputData.setClean();
}
else
{
status = MS::kUnknownParameter;
}
}
return status;
}
void* meshOpNode::creator()
{
return new meshOpNode();
}
MStatus meshOpNode::initialize()
{
MStatus status;
MFnTypedAttribute attrFn;
MFnEnumAttribute enumFn;
cpList = attrFn.create("inputComponents", "ics",
MFnComponentListData::kComponentList);
attrFn.setStorable(true);
opType = enumFn.create("operationType", "oprt", 0, &status);
enumFn.addField("subd_edges", 0);
enumFn.addField("subd_faces", 1);
enumFn.setHidden(false);
enumFn.setKeyable(true);
enumFn.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( cpList );
if (!status)
{
status.perror("addAttribute");
return status;
}
status = addAttribute( opType );
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( cpList, outMesh );
if (!status)
{
status.perror("attributeAffects");
return status;
}
status = attributeAffects( opType, outMesh );
if (!status)
{
status.perror("attributeAffects");
return status;
}
return MS::kSuccess;
}