Hello, I am new to the Maya API and I dont really know what is possible and what is not possible and I'm suspecting that what I want to do is not possible.
I put the names of all the objects that are selected in an array. THen during the initialize() function of my node I create a new attribute based on the names i read from the array for every name that is in the array.
Well, i get that to compile, but now I want to set these new attributes as output values in teh compute function...but I dont know how to access those attributes to set their output values since obviously data.outputvalue() needs an MObject parameter, and i only create those attributes in the for loop..
.... thanks, here the code for my node:
CODE
//... includes
MTypeId BoidPositionsNode::id(0x43214);
//MObject BoidPositionsNode::oldBoidPositionsArray;
MObject BoidPositionsNode::finalVelocityInX;
MObject BoidPositionsNode::finalVelocityInY;
MObject BoidPositionsNode::finalVelocityInZ;
MObject BoidPositionsNode::positionChangeX;
MPointArray BoidPositionsNode::oldBoidPositionsArray;
//Creator function
void *BoidPositionsNode::creator()
{
return new BoidPositionsNode;
}
//Initializer (sets up the nodes attributes)
MStatus BoidPositionsNode::initialize()
{
//-----------------------------
//--- Create Attributes ---
//-----------------------------
MFnNumericAttribute numericAttr;
finalVelocityInX = numericAttr.create("FinalVelocityInX", "fvInX", MFnNumericData::kDouble, 0.0);
finalVelocityInY = numericAttr.create("FinalVelocityInY", "fvInY", MFnNumericData::kDouble, 0.0);
finalVelocityInZ = numericAttr.create("FinalVelocityInZ", "fvInZ", MFnNumericData::kDouble, 0.0);
positionChangeX = numericAttr.create("positionChangeOutX", "pcoX", MFnNumericData::kDouble, 0.0);
//--------------------------
//--- Add the Attributes ---
//--------------------------
addAttribute(finalVelocityInX);
addAttribute(finalVelocityInY);
addAttribute(finalVelocityInZ);
addAttribute(positionChangeX);
//-----------------------------------
//--- Specify Attribute Relations ---
//-----------------------------------
attributeAffects(finalVelocityInX, positionChangeX);
attributeAffects(finalVelocityInY, positionChangeX);
attributeAffects(finalVelocityInZ, positionChangeX);
//----------------------------------------
//--- Create DYNAMIC Attribute -----------
//----- for every element in the array ---
//----------------------------------------
MFnNumericAttribute dynamicAttrs;
int elements = ::globalBoidNameArray.length();
for(int i=0; i < elements; i++){
MString boidName = ::globalBoidNameArray[i];
// create the dynamic attribute
MObject dynObj1 = dynamicAttrs.create(boidName+"AttrX", boidName,MFnNumericData::kDouble);
MObject dynObj2 = dynamicAttrs.create(boidName+"AttrY", boidName,MFnNumericData::kDouble);
MObject dynObj3 = dynamicAttrs.create(boidName+"AttrZ", boidName,MFnNumericData::kDouble);
//--------------------------
//--- Add the DYNAMIC Attributes ---
//--------------------------
addAttribute(dynObj1);
addAttribute(dynObj2);
addAttribute(dynObj3);
//-----------------------------------
//--- Specify DYNAMIC Attribute Relations ---
//-----------------------------------
attributeAffects(finalVelocityInX, dynObj1);
attributeAffects(finalVelocityInY, dynObj2);
attributeAffects(finalVelocityInZ, dynObj3);
}
return(MS::kSuccess);
}
//compute function to do the real work
MStatus BoidPositionsNode::compute (const MPlug &plug, MDataBlock &data)
{
MStatus stat;
if( plug == finalVelocityInX)
{
//-----------------------------
//--- Get Input Data Handles---
//-----------------------------
//handles for the Final Velocity
MDataHandle cVelocityDataX = data.inputValue(finalVelocityInX);
MDataHandle cVelocityDataY = data.inputValue(finalVelocityInY);
MDataHandle cVelocityDataZ = data.inputValue(finalVelocityInZ);
//----------------------------------------
//--- Turn the Input Handles into Data ---
//----------------------------------------
//data for the velocity
double fVelocityX = cVelocityDataX.asDouble();
double fVelocityY = cVelocityDataY.asDouble();
double fVelocityZ = cVelocityDataZ.asDouble();
//----------------------------------------
//--- Get the current time ---------------
//----------------------------------------
MTime currentTime = MAnimControl::currentTime();
currentTime.setUnit(MTime::kNTSCFrame);
double t;
t = currentTime.value();
int elements = ::globalBoidNameArray.length();
for(int i=0; i < elements; i++){
MString boidName = ::globalBoidNameArray[i];
//------------------------------------------
//--- get initial displacements ------------
//------------------------------------------
MPoint oldBoidPosition = oldBoidPositionsArray[i];
double oldPositionX = oldBoidPosition.x;
double oldPositionY = oldBoidPosition.y;
double oldPositionZ = oldBoidPosition.z;
//------------------------------------------
//--- calculate change in position using eq:
// S = V*t
//------------------------------------------
double deltaDisplacementX = fVelocityX*t;
double deltaDisplacementY = fVelocityY*t;
double deltaDisplacementZ = fVelocityZ*t;
double newPositionX = deltaDisplacementX + oldPositionX;
double newPositionY = deltaDisplacementY + oldPositionY;
double newPositionZ = deltaDisplacementZ + oldPositionZ;
oldBoidPosition.x = newPositionX;
oldBoidPosition.y = newPositionY;
oldBoidPosition.z = newPositionZ;
/// here i need to set the output attributes but how??? <------------------
}
//---------------------------------------
//--- Get Handles for the Output Data ---
//---------------------------------------
//get the handle for new position
MDataHandle outputChangeXData = data.outputValue(positionChangeX);
//--------------------------------
//--- Close Up the Computation ---
//--------------------------------
//set the output attribute
outputChangeXData.set(5.5);
//--------------------------------
//--- Set Final Velocity ---------
//--- As new Initial Velocity ----
//--------------------------------
//mark the plug as updated
data.setClean(plug);
}
else
stat = MS::kUnknownParameter;
return(stat);
}