Maya API get the translation matrix at a specific time


#1

Hi,
I’m trying to do a Node, where the input is a mesh and get the translation vector of it in the previous frame. But I don’t get it. Can anyone help me?
Thanks


#2

I have tried to use MDGContext on the MDataBlock, if I only set the MDGContext one frame before the actual timeline, it works some frames and then it returns to the real time.
As I want to get the frame before and the one at the moment, it doesn’t work, this is the code I used:

MStatus node::compute(const MPlug& plug, MDataBlock& data){

if (plug == dirX) {
MDataHandle dirXHandle = data.outputValue(dirX);
MDataHandle dirXHandleOld = data.outputValue(dirXOld);
MTime time = MAnimControl::currentTime();
MDGContext timeContext(time);
MDGContext timeContextOld(time - 1);

  data.setContext(timeContextOld);
  MDataHandle inputMeshHandleOld = data.inputValue(input);
  MMatrix oldMatrix = inputMeshHandleOld.geometryTransformMatrix();
  // Puts matrix into transformation matrix
  MTransformationMatrix mTransformMtxOld(oldMatrix);
  // Translation Values
  MVector oldTrans = mTransformMtxOld.translation(MSpace::kWorld);

  data.setContext(timeContext);
  MDataHandle inputMeshHandle = data.inputValue(input);
  MMatrix matrix = inputMeshHandle.geometryTransformMatrix();
  // Puts matrix into transformation matrix
  MTransformationMatrix mTransformMtx(matrix);
  // Translation Values
  MVector trans = mTransformMtx.translation(MSpace::kWorld);

    data.setContext(timeContext);
  dirXHandle.setFloat(trans.x);
  dirXHandleOld.setFloat(oldTrans.x);

}else {
return MS::kUnknownParameter;
}

return MS::kSuccess;
}


#3

I also have tried using the MPlug getValue setting the MDGContext one frame before, but it doesn’t work either

MStatus node::compute(const MPlug& plug, MDataBlock& data){

if (plug == dirX) {
MDataHandle dirXHandle = data.outputValue(dirX);
MDataHandle dirXHandleOld = data.outputValue(dirXOld);
MTime time = MAnimControl::currentTime();

  MDGContext timeContextOld(time - 1);

  MObject inputMeshObj = inputMeshHandle.asMesh();
  MFnMesh meshFn(inputMeshObj);

  MDagPath dagPath;
  dagPath = meshFn.dagPath();
  MFnDagNode transFn(dagPath);

  MPlug matrixPlugArray = transFn.findPlug("worldMatrix");
  matrixPlugArray.evaluateNumElements();
  MPlug matrixPlug = matrixPlugArray.elementByPhysicalIndex(0);

  MObject matrixO;
  matrixPlug.getValue(matrixO, timeContextOld);
  MFnMatrixData fnMat(matrixO);
  MVector res = fnMat.transformation().getTranslation(MSpace::kWorld);

  // Set the output data.
  dirXHandle.setFloat(0.0);
  dirXHandleOld.setFloat(res.x);

}else {
return MS::kUnknownParameter;
}

return MS::kSuccess;
}

Does someone know what I’m doing wrong?
Thanks