#include "splitUVCmd.h"
#include "splitUVNode.h"
#include <maya/MFnDependencyNode.h>
#include <maya/MFnMesh.h>
#include <maya/MFnSingleIndexedComponent.h>
#include <maya/MItSelectionList.h>
#include <maya/MItMeshPolygon.h>
#include <maya/MGlobal.h>
#include <maya/MSelectionList.h>
#include <maya/MPlug.h>
#include <maya/MIOStream.h>
#define MCheckStatus(status,message) \
if( MS::kSuccess != status ) { \
cerr << message << "\n"; \
return status; \
}
splitUV::splitUV()
{}
splitUV::~splitUV()
{}
void* splitUV::creator()
{
return new splitUV();
}
bool splitUV::isUndoable() const
{
return true;
}
{
bool found = false;
bool foundMultiple = false;
for( ; !selListIter.isDone(); selListIter.next() )
{
selListIter.getDagPath( dagPath, component );
{
if( !found )
{
compListFn.
add( component );
fComponentList = compListFn.
object();
compFn.getElements( fSelUVs );
setMeshNode( dagPath );
found = true;
}
else
{
foundMultiple = true;
break;
}
}
}
if( foundMultiple )
{
displayWarning("Found more than one object with selected UVs - Only operating on first found object.");
}
setModifierNodeType( splitUVNode::id );
if( found )
{
if( validateUVs() )
{
status = doModifyPoly();
if( status == MS::kSuccess )
{
setResult( "splitUV command succeeded!" );
}
else
{
displayError( "splitUV command failed!" );
}
}
else
{
displayError( "splitUV command failed: Selected UVs are not splittable" );
status = MS::kFailure;
}
}
else
{
displayError( "splitUV command failed: Unable to find selected UVs" );
status = MS::kFailure;
}
return status;
}
{
status = redoModifyPoly();
if( status == MS::kSuccess )
{
setResult( "splitUV command succeeded!" );
}
else
{
displayError( "splitUV command failed!" );
}
return status;
}
{
status = undoModifyPoly();
if( status == MS::kSuccess )
{
setResult( "splitUV undo succeeded!" );
}
else
{
setResult( "splitUV undo failed!" );
}
return status;
}
{
uvListAttr = depNodeFn.attribute( "inputComponents" );
MPlug uvListPlug( modifierNode, uvListAttr );
status = uvListPlug.setValue( fComponentList );
return status;
}
{
fSplitUVFactory.setMesh( mesh );
fSplitUVFactory.setUVIds( fSelUVs );
status = fSplitUVFactory.doIt();
return status;
}
bool splitUV::validateUVs()
{
int i;
int j;
int count = 0;
int selUVsCount = fSelUVs.
length();
for( i = 0; i < selUVsCount; i++ )
{
for( ; !polyIter.isDone(); polyIter.next() )
{
if( polyIter.hasUVs() )
{
int polyVertCount = polyIter.polygonVertexCount();
for( j = 0; j < polyVertCount; j++ )
{
int UVIndex = 0;
polyIter.getUVIndex(j, UVIndex);
if( UVIndex == fSelUVs[i] )
{
count++;
break;
}
}
}
}
selUVFaceCountArray.
append(count);
}
bool isValid = false;
for( i = 0; i < selUVsCount; i++ )
{
if( selUVFaceCountArray[i] > 1 )
{
isValid = true;
}
}
if( isValid )
{
pruneUVs( validUVIndices );
}
return isValid;
}
{
unsigned i;
for( i = 0; i < validUVIndices.
length(); i++ )
{
int uvIndex = validUVIndices[i];
validUVIds.
append( fSelUVs[uvIndex] );
}
fSelUVs = validUVIds;
MCheckStatus( status, "compFn.create( MFn::kMeshMapComponent )" );
MCheckStatus( status, "compFn.addElements( validUVIds )" );
status = compListFn.
add( component );
MCheckStatus( status, "compListFn.add( component )" );
fComponentList = compListFn.
object();
return status;
}