Aim Constraint Keeping offset



What is the best way to keep the offset of the current rotation.
Meaning A is pointing B
but I want to keep A at the same rotation when the constraint is applied.


You may be looking for the ‘maintainOffset’ flag

pm.aimConstraint( 'driverObjectName','drivenObjectName' , maintainOffset=True )

If manually doing this, it’s a checkbox in the options box of the menu item


Thanks mambo but Im using Maya api to build an aim constraint, I will like some math to do so.


just in case somebody need it

   MStatus aimLocator::compute(const MPlug& plug, MDataBlock& data){
MStatus status;

if (plug != aPoint && plug.parent() != aPoint)
	return MS::kInvalidParameter;

//Up Vector
MVector vUp = data.inputValue(aVector).asVector();
//Aim Target Matrix
MMatrix mmTarget = data.inputValue(aMatrixTarget).asMatrix();
MMatrix mmTargetInit = data.inputValue(aMatrixTargetInit).asMatrix();
//Target Matrix
MMatrix mmUp = data.inputValue(aMatrixUp).asMatrix();
MMatrix mmMatrixO = data.inputValue(aMatrixOriginal).asMatrix();
//Need in order to dirty inputs
MMatrix mmMatrixO2 = data.inputValue(aMatrixThis).asMatrix();
MMatrix mmMatrixO3 = data.inputValue(aMatrixThis2).asMatrix();

//Getting the transfrom node of this object
MDagPath mdpThis;
MFnDagNode mfdnThis(thisMObject());
MFnDependencyNode depNodeFn(thisMObject());
MFnTransform mftThis(mdpThis.transform());

bool option = data.inputValue(aOptionOff).asBool();
bool optionUp = data.inputValue(aOptionUp).asBool();

//World Matrix this object
MMatrix mmMatrix = mdpThis.inclusiveMatrix();

//Converting MMatrix to MTransformationMatrix 
MTransformationMatrix mmtMatrixO = mmMatrixO;
MTransformationMatrix mmtTarget = mmTarget;
MTransformationMatrix mmtTargetInit = mmTargetInit;
MMatrix mmUp2 = mmUp;

if (option) {
	//Offset object in X
	MVector v(10, 0, 0);
	mmtMatrixO.addTranslation(v, MSpace::kObject);
	//Creating dummy object parented to target to keep the offset
	MVector posBase = mmtMatrixO.getTranslation(MSpace::kWorld);
	MVector posTarget = mmtTarget.getTranslation(MSpace::kWorld);
	MVector posTargetInit = mmtTargetInit.getTranslation(MSpace::kWorld);
	posTarget = (posBase - posTargetInit) + posTarget;
	mmtTarget.setTranslation(posTarget, MSpace::kWorld);
	mmTarget = mmtTarget.asMatrix();

	MTransformationMatrix mmtUp = mmUp;
	MVector v2(0, 0, 100);
	mmtUp.addTranslation(v2, MSpace::kWorld);
	mmUp2 = mmtUp.asMatrix();

//Get the transformation matrix for this object
MTransformationMatrix mftOut = lookAt(mmMatrix, mmTarget, mmUp2, vUp, optionUp);
mftOut = lookAt(mftOut.asMatrix(), mmTarget, mmUp, vUp, optionUp);
//Geting final rotation
double rotation[3];
MTransformationMatrix::RotationOrder rotOrder;
mftOut.getRotation(rotation, rotOrder);

//Setting the final matrix to this object

//Setting the final roation output
MDataHandle hOutput = data.outputValue(aPoint);
hOutput.set(rotation[0], rotation[1], rotation[2]);

return MS::kSuccess;

    MTransformationMatrix aimLocator::lookAt(MMatrix & mmThis, MMatrix & mmTarget, MMatrix & mmUp, MVector vUp, bool bUp)

//Up Vector Set-UP
MTransformationMatrix mftThisU = mmThis;
MVector posBase = mftThisU.getTranslation(MSpace::kWorld);
vUp += posBase;
mftThisU.setTranslation(vUp, MSpace::kWorld);
vUp = mftThisU.getTranslation(MSpace::kWorld);
MPoint mpUp(0.0, 0.0, 0.0);
if (bUp) {
	mpUp = MPoint::MPoint(mmUp[3][0], mmUp[3][1], mmUp[3][2]);
	vUp = MVector(mpUp);


/////////////Rotate to Target with Aim Vector in X

//Get Target position in World Space
MPoint mpTarget(mmTarget[3][0], mmTarget[3][1], mmTarget[3][2]);
MTransformationMatrix mftThis = mmThis;

//Create a vector from this object to target
MVector mvTarget = (MVector)(mpTarget * mmThis.inverse());
//TODO: create attr for the quaternion vector
//Rotate by a quaternion with +x with the vector
MQuaternion qToTarget(MVector(1.0, 0.0, 0.0).rotateTo(mvTarget));
mftThis.rotateBy(qToTarget, MSpace::kPreTransform);

/////////Rotate to Up Vector using +Y as Up vector

//Get Up Object position in World Space 
vUp = (MVector)(mpUp *  mmThis.inverse());

//Rotate by a quaternion with +Y with the vector
//TODO: create attr for the quaternion vector
MQuaternion qToUp(MVector(0.0, 1.0, 0.0).rotateTo(vUp));
MEulerRotation merToWorldUp(qToUp.asEulerRotation());
//Zero out Y Z because X is the twist vector
merToWorldUp.y = 0.0;
merToWorldUp.z = 0.0;
//Rotate output matrix in X
mftThis.rotateBy(merToWorldUp, MSpace::kPreTransform);

return mftThis;