#include "tweakMetadataNode.h"
#include <string.h>
#include <sstream>
#include <maya/MIOStream.h>
#include <math.h>
#include <maya/MPxNode.h>
#include <maya/MFnMesh.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MFnEnumAttribute.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MTypeId.h>
#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MArrayDataHandle.h>
#include <maya/adskDataAssociations.h>
#include <maya/adskDataChannel.h>
#include <maya/adskDataStream.h>
#include <maya/adskDataHandle.h>
#include <maya/adskDataMember.h>
#define MCheckErr(stat,msg)     \
    if ( MS::kSuccess != stat ) {   \
        cerr << msg;        \
        return MS::kFailure;    \
    }
MTypeId tweakMetadataNode::id( 0x8104F );
 
MObject tweakMetadataNode::aInMesh;
 
MObject tweakMetadataNode::aOutMesh;
 
MObject tweakMetadataNode::aOperation;
 
tweakMetadataNode::tweakMetadataNode    () {}
tweakMetadataNode::~tweakMetadataNode   () {}
void* tweakMetadataNode::creator()
{
    return new tweakMetadataNode();
}
const char* tweakMetadataNode::nodeName()
{
    return "tweakMetadata";
}
MStatus tweakMetadataNode::initialize()
 
{
    
    aOperation = eAttr.
create( 
"operation", 
"op", kOpNone );
        eAttr.
addField( 
"random", kOpRandomize );
    MCheckErr( status, "failed to create operation attribute" );
    addAttribute( aOperation );
    
    MCheckErr( status, "failed to create inMesh attribute" );
    addAttribute( aInMesh );
    
    MCheckErr( status, "failed to create outMesh attribute" );
    addAttribute( aOutMesh );
    
    attributeAffects(aInMesh, aOutMesh);
    attributeAffects(aOperation, aOutMesh);
}
{
    MPlug opPlug(thisNode, aOperation);
 
    {
        return status;
    }
    MPlug inMeshPlug(thisNode, aInMesh);
 
    MCheckErr(status,"ERROR getting inMesh"); 
    
    
    MPlug outMeshPlug(thisNode, aOutMesh);
 
    MCheckErr(status,"ERROR getting outMesh"); 
    outMeshHandle.
set(inMeshObj);
    
    if( oldAssociations )
    {
        
        associations.makeUnique();
        for( unsigned int c=0; c<associations.channelCount(); ++c )
        {
            
            for( unsigned int s=0; s<channel.dataStreamCount(); ++s )
            {
                if( ! channel.dataStream( s ) ) continue; 
                {
                    if( ! sHandle.
hasData() ) 
continue;
 
                    if( sHandle.
dataType() != adsk::Data::Member::kInt32 ) 
continue;
 
    
                    if( ! hValue ) continue;
    
                    for( 
unsigned int l=0; l<sHandle.
dataLength(); ++l )
 
                    {
                        switch( opType )
                        {
                            case kOpRandomize:
                                
                                
                                hValue[l] = int(rand() % 2001) - 1000;
                                break;
    
                            
                            case kOpFill:
                                
                                
                                
                                
                                hValue[l] = (size_t)(chStream) % 0xff;
                                break;
    
                            
                            case kOpDouble:
                                
                                hValue[l] *= 2;
                                break;
    
                            
                            case kOpNone:
                            default:
                                
                                break;
                        }
                    }
                }
                channel.setDataStream( *chStream );
            }
            associations.setChannel( channel );
        }
        
        
        outputMesh.setMetadata( associations );
    }
    return status;
}