#include "latticeNoise.h"
#include <string.h>
#include <maya/MIOStream.h>
#include <stdio.h>
#include <math.h>
#include <maya/MFnUnitAttribute.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MFnLattice.h>
#include <maya/MFnLatticeData.h>
#include <maya/MString.h>
#include <maya/MTypeId.h>
#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MTime.h>
#define DEF_AMPLITUDE 0.2
#define DEF_FREQ 4.0
#define McheckErr(stat,msg) \
if ( MS::kSuccess != stat ) { \
cerr << msg; \
return MS::kFailure; \
}
MTypeId latticeNoiseNode::id( 0x80010 );
MObject latticeNoiseNode::input;
MObject latticeNoiseNode::amplitude;
MObject latticeNoiseNode::frequency;
MObject latticeNoiseNode::time;
MObject latticeNoiseNode::output;
MStatus latticeNoiseNode::compute( const MPlug& plug, MDataBlock& data )
{
MStatus returnStatus;
float noiseAmplitude;
float noiseFreq;
if( plug == output )
{
MDataHandle inputData = data.inputValue( input, &returnStatus );
McheckErr( returnStatus, "ERROR getting lattice data handle\n" );
MObject latticeData = inputData.data();
MFnLatticeData dataFn( latticeData );
MObject lattice = dataFn.lattice();
MFnLattice lattFn( lattice, &returnStatus );
McheckErr( returnStatus, "ERROR getting lattice geometry\n" );
MDataHandle outputData = data.outputValue( output, &returnStatus );
McheckErr( returnStatus, "ERROR getting lattice data handle\n" );
latticeData = outputData.data();
if ( latticeData.isNull() ) {
latticeData = dataFn.create();
} else {
dataFn.setObject( latticeData );
}
MObject outLattice = dataFn.lattice();
MFnLattice outLattFn( outLattice, &returnStatus );
McheckErr( returnStatus, "ERROR getting lattice geometry\n" );
MDataHandle ampData = data.inputValue( amplitude, &returnStatus );
McheckErr( returnStatus, "ERROR getting amplitude\n" );
noiseAmplitude = ampData.asFloat();
MDataHandle freqData = data.inputValue( frequency, &returnStatus );
McheckErr( returnStatus, "ERROR getting frequency\n" );
noiseFreq = freqData.asFloat();
MDataHandle timeData = data.inputValue( time, &returnStatus );
McheckErr( returnStatus, "ERROR getting time data handle\n" );
MTime time = timeData.asTime();
float seconds = (float)time.as( MTime::kSeconds );
seconds = seconds * noiseFreq;
unsigned s, t, u;
lattFn.getDivisions( s, t, u );
outLattFn.setDivisions( s, t, u );
for ( unsigned i = 0; i < s; i++ ) {
for ( unsigned j = 0; j < t; j++ ) {
for ( unsigned k = 0; k < u; k++ ) {
MPoint & point = lattFn.point( i, j, k );
MPoint & outPoint = outLattFn.point( i, j, k );
pnt noisePnt = noise::atPointAndTime( (float)point.x, (float)point.y,
(float)point.z, seconds );
noisePnt.x = ( noisePnt.x * 2.0F ) - 1.0F;
noisePnt.y = ( noisePnt.y * 2.0F ) - 1.0F;
noisePnt.z = ( noisePnt.z * 2.0F ) - 1.0F;
outPoint.x = point.x + ( noisePnt.x * noiseAmplitude );
outPoint.y = point.y + ( noisePnt.y * noiseAmplitude );
outPoint.z = point.z + ( noisePnt.z * noiseAmplitude );
}
}
}
outputData.set( latticeData );
data.setClean(plug);
} else {
return MS::kUnknownParameter;
}
return MS::kSuccess;
}
void* latticeNoiseNode::creator()
{
return new latticeNoiseNode;
}
MStatus latticeNoiseNode::initialize()
{
MFnUnitAttribute unitAttr;
MFnTypedAttribute typedAttr;
MFnNumericAttribute numAttr;
MStatus stat;
input = typedAttr.create( "input", "in", MFnData::kLattice, &stat );
if ( !stat ) {
stat.perror("ERROR creating latticeNoiseNode input lattice attribute");
return stat;
}
amplitude = numAttr.create( "amplitude", "amp",
MFnNumericData::kFloat,
DEF_AMPLITUDE,
&stat );
if ( !stat ) {
stat.perror("ERROR creating latticeNoiseNode amplitude attribute");
return stat;
}
frequency = numAttr.create( "frequency", "fq",
MFnNumericData::kFloat,
DEF_FREQ,
&stat );
if ( !stat ) {
stat.perror("ERROR creating latticeNoiseNode frequency attribute");
return stat;
}
time = unitAttr.create( "time", "tm", MFnUnitAttribute::kTime,
0.0, &stat );
if ( !stat ) {
stat.perror("ERROR creating latticeNoiseNode time attribute");
return stat;
}
output = typedAttr.create( "output", "out", MFnData::kLattice, &stat );
if ( !stat ) {
stat.perror("ERROR creating latticeNoiseNode output attribute");
return stat;
}
typedAttr.setWritable(false);
stat = addAttribute( input );
if (!stat) { stat.perror("addAttribute"); return stat;}
stat = addAttribute( amplitude );
if (!stat) { stat.perror("addAttribute"); return stat;}
stat = addAttribute( frequency );
if (!stat) { stat.perror("addAttribute"); return stat;}
stat = addAttribute( time );
if (!stat) { stat.perror("addAttribute"); return stat;}
stat = addAttribute( output );
if (!stat) { stat.perror("addAttribute"); return stat;}
stat = attributeAffects( input, output );
if (!stat) { stat.perror("attributeAffects"); return stat;}
stat = attributeAffects( amplitude, output );
if (!stat) { stat.perror("attributeAffects"); return stat;}
stat = attributeAffects( frequency, output );
if (!stat) { stat.perror("attributeAffects"); return stat;}
stat = attributeAffects( time, output );
if (!stat) { stat.perror("attributeAffects"); return stat;}
return MS::kSuccess;
}