Custom Transform Node

Custom Transform Node

jmreinhart
Advisor Advisor
2,109 Views
5 Replies
Message 1 of 6

Custom Transform Node

jmreinhart
Advisor
Advisor

I am making a custom transform node that has offset translate, rotate, and scale, attributes that get added to the regular transform attributes to create the new matrix.

 

I have everything working properly, except that the children of my custom node do not update when I change one of custom transform attributes. The will update to the correct value once I update one of the regular transform attributes. 

 

I've looked through the documentation and the rockingTransform example but I cannot figure out the issue.

 

Any help would be greatly appreciated.

0 Likes
Accepted solutions (1)
2,110 Views
5 Replies
Replies (5)
Message 2 of 6

jmreinhart
Advisor
Advisor
#include <maya/MMatrix.h>
#include <maya/MPxTransformationMatrix.h>
#include <maya/MPxTransform.h>
#include <maya/MFnPlugin.h>
#include <maya/MPlug.h>
#include <maya/MDGContext.h>
#include <maya/MFnUnitAttribute.h>
#define _USE_MATH_DEFINES
#include <math.h>

class normalTransformMatrix : public MPxTransformationMatrix
{

	// - The virtual asMatrix() method which passes the matrix 
	// back to Maya when the command "xform -q -ws -m" is invoked
public:
	normalTransformMatrix();
	static void *creator();

	//virtual method which passes the matrix when you use the xform command
	virtual MMatrix asMatrix() const;
	virtual MMatrix asMatrix(double percent) const;
	virtual MMatrix asRotateMatrix() const;

	//methods to set and get the new localScale attributes we will add
	double getLocalScaleX() const;
	double getLocalScaleY() const;
	double getLocalScaleZ() const;

	void setLocalScaleX(double localScaleX);
	void setLocalScaleY(double localScaleY);
	void setLocalScaleZ(double localScaleZ);

	//methods to set and get the new offset attributes we will add
	double getOffsetTranslateX() const;
	double getOffsetTranslateY() const;
	double getOffsetTranslateZ() const;

	void setOffsetTranslateX(double offsetTranslateX);
	void setOffsetTranslateY(double offsetTranslateY);
	void setOffsetTranslateZ(double offsetTranslateZ);
	//
	double getOffsetRotateX() const;
	double getOffsetRotateY() const;
	double getOffsetRotateZ() const;

	void setOffsetRotateX(double offsetRotateX);
	void setOffsetRotateY(double offsetRotateY);
	void setOffsetRotateZ(double offsetRotateZ);
	//
	double getOffsetScaleX() const;
	double getOffsetScaleY() const;
	double getOffsetScaleZ() const;

	void setOffsetScaleX(double offsetScaleX);
	void setOffsetScaleY(double offsetScaleY);
	void setOffsetScaleZ(double offsetScaleZ);
	//
	static MTypeId xformId;
protected:
	typedef MPxTransformationMatrix ParentClass;
	//variables to store extra values that are used to calculate the matrix
	// local scale
	double localScaleX_val = 1.0;
	double localScaleY_val = 1.0;
	double localScaleZ_val = 1.0;
	//offset translate
	double offsetTranslateX_val = 0.0;
	double offsetTranslateY_val = 0.0;
	double offsetTranslateZ_val = 0.0;
	//offset rotate
	double offsetRotateX_val = 0.0;
	double offsetRotateY_val = 0.0;
	double offsetRotateZ_val = 0.0;
	//offset scale
	double offsetScaleX_val = 1.0;
	double offsetScaleY_val = 1.0;
	double offsetScaleZ_val = 1.0;

};

// Matrix constructor. Initialize any class variables.
normalTransformMatrix::normalTransformMatrix()
{
	localScaleX_val = 1.0;
	localScaleY_val = 1.0;
	localScaleZ_val = 1.0;
}

void *normalTransformMatrix::creator()
{
	return new normalTransformMatrix();
}

//
// Utility method for getting the rock
// motion in the X axis
//
double normalTransformMatrix::getLocalScaleX() const
{
	return localScaleX_val;
}

double normalTransformMatrix::getLocalScaleY() const
{
	return localScaleY_val;
}

double normalTransformMatrix::getLocalScaleZ() const
{
	return localScaleZ_val;
}
//
double normalTransformMatrix::getOffsetTranslateX() const
{
	return offsetTranslateX_val;
}

double normalTransformMatrix::getOffsetTranslateY() const
{
	return offsetTranslateY_val;
}

double normalTransformMatrix::getOffsetTranslateZ() const
{
	return offsetTranslateZ_val;
}
//
double normalTransformMatrix::getOffsetRotateX() const
{
	return offsetRotateX_val;
}

double normalTransformMatrix::getOffsetRotateY() const
{
	return offsetRotateY_val;
}

double normalTransformMatrix::getOffsetRotateZ() const
{
	return offsetRotateZ_val;
}
//
double normalTransformMatrix::getOffsetScaleX() const
{
	return offsetScaleX_val;
}

double normalTransformMatrix::getOffsetScaleY() const
{
	return offsetScaleY_val;
}

double normalTransformMatrix::getOffsetScaleZ() const
{
	return offsetScaleZ_val;
}

//
// Utility method for setting the rcok 
// motion in the X axis
//
void normalTransformMatrix::setLocalScaleX(double scale)
{
	localScaleX_val = scale;
}

void normalTransformMatrix::setLocalScaleY(double scale)
{
	localScaleY_val = scale;
}

void normalTransformMatrix::setLocalScaleZ(double scale)
{
	localScaleZ_val = scale;
}
//
void normalTransformMatrix::setOffsetTranslateX(double scale)
{
	offsetTranslateX_val = scale;
}

void normalTransformMatrix::setOffsetTranslateY(double scale)
{
	offsetTranslateY_val = scale;
}

void normalTransformMatrix::setOffsetTranslateZ(double scale)
{
	offsetTranslateZ_val = scale;
}
//
void normalTransformMatrix::setOffsetRotateX(double scale)
{
	offsetRotateX_val = scale;
}

void normalTransformMatrix::setOffsetRotateY(double scale)
{
	offsetRotateY_val = scale;
}

void normalTransformMatrix::setOffsetRotateZ(double scale)
{
	offsetRotateZ_val = scale;
}
//
void normalTransformMatrix::setOffsetScaleX(double scale)
{
	offsetScaleX_val = scale;
}

void normalTransformMatrix::setOffsetScaleY(double scale)
{
	offsetScaleY_val = scale;
}

void normalTransformMatrix::setOffsetScaleZ(double scale)
{
	offsetScaleZ_val = scale;
}


//
// This method will be used to return information to
// Maya. Use the attributes which are outside of
// the regular transform attributes to build a new
// matrix. This new matrix will be passed back to
// Maya.
//
MMatrix normalTransformMatrix::asMatrix() const
{
	// Get the current transform matrix
	MMatrix mm = ParentClass::asMatrix();
	//Convert It To A MTransformationMatrix
	MTransformationMatrix m(mm);

	//Get the translate that appears in the channel box
	MVector trans = m.getTranslation(MSpace::kTransform);

	//Add the offset 
	trans += MVector(offsetTranslateX_val, offsetTranslateY_val, offsetTranslateZ_val);
	//Scale it by the localScale
	trans[0] *= localScaleX_val;
	trans[1] *= localScaleY_val;
	trans[2] *= localScaleZ_val;

	//Put that value into the transformaton matrix
	m.setTranslation(trans, MSpace::kTransform);

	//Get the rotate that appears in the channel box
	MQuaternion quatRotate = m.rotation();
	//Apply the offset rotation
	double convert_constant = M_PI / 180;
	MEulerRotation eulerOffsetRotate(offsetRotateX_val*convert_constant, offsetRotateY_val*convert_constant, offsetRotateZ_val*convert_constant);
	MQuaternion quatOffsetRotate = eulerOffsetRotate.asQuaternion();
	quatRotate *= quatOffsetRotate;
	MEulerRotation eulRotate = quatRotate.asEulerRotation();
	//Put that value into the transformaton matrix
	m.rotateTo(eulRotate);

	//Get the scale that appears in the channel box
	double s[3];
	m.getScale(s, MSpace::kTransform);

	//Apply the offset scale
	s[0] *= offsetScaleX_val;
	s[1] *= offsetScaleY_val;
	s[2] *= offsetScaleZ_val;

	m.setScale(s, MSpace::kTransform);
	return m.asMatrix();
}

MMatrix normalTransformMatrix::asMatrix(double percent) const//not used by the viewport
{
	MGlobal::displayInfo("calling the percent func");
	MPxTransformationMatrix m(*this);

	//Get the translate that appears in the channel box
	MVector trans = m.translation();
	//Add the offset translate
	trans += MVector(offsetTranslateX_val, offsetTranslateY_val, offsetTranslateZ_val);
	//Scale it by the localScale
	trans[0] *= localScaleX_val;
	trans[1] *= localScaleY_val;
	trans[2] *= localScaleZ_val;
	//Apply the percent function (so the xform command can use it)
	trans *= percent;
	//Put that value into the transformaton matrix
	m.translateTo(trans);
	/*
	//Make any changes to the rotate and scale pivots (None for us)
	MPoint rotatePivotTrans = m.rotatePivot();
	rotatePivotTrans = rotatePivotTrans * percent;
	m.setRotatePivot(rotatePivotTrans);
	
	MPoint scalePivotTrans = m.scalePivotTranslation();
	scalePivotTrans = scalePivotTrans * percent;
	m.setScalePivotTranslation(scalePivotTrans);

	//Get the rotate that appears in the channel box
	MQuaternion quatRotate = m.rotation();
	//Apply the offset rotation
	MEulerRotation eulerOffsetRotate(offsetRotateX_val, offsetRotateY_val, offsetRotateZ_val);
	MQuaternion quatOffsetRotate = eulerOffsetRotate.asQuaternion();
	quatRotate *= quatOffsetRotate;
	MEulerRotation eulRotate = quatRotate.asEulerRotation();
	//Apply the percentage to the rotate value.
	m.rotateTo(eulRotate * percent);

	//Get the scale that appears in the channel box
	MVector s = m.scale(MSpace::kTransform);
	//Apply the offset scale
	s.x *= offsetScaleX_val;
	s.y *= offsetScaleY_val;
	s.z *= offsetScaleZ_val;
	//Apply the percentage to the scale
	s.x = 1.0 + (s.x - 1.0)*percent;
	s.y = 1.0 + (s.y - 1.0)*percent;
	s.z = 1.0 + (s.z - 1.0)*percent;
	m.scaleTo(s, MSpace::kTransform);
	*/
	return m.asMatrix();
}

MMatrix	normalTransformMatrix::asRotateMatrix() const
{
	// To be implemented
	return ParentClass::asRotateMatrix();
}

MTypeId normalTransformMatrix::xformId(0x00000007);

class normalTransformNode : public MPxTransform
{
	// A really simple custom transform.
	public:
		normalTransformNode();
		normalTransformNode(normalTransformMatrix *);

		virtual ~normalTransformNode();
		virtual MPxTransformationMatrix *createTransformationMatrix();//???

		virtual void postConstructor();

		virtual MStatus validateAndSetValue(
			const MPlug& plug,
			const MDataHandle& handle);

		virtual void resetTransformation(MPxTransformationMatrix *);
		virtual void resetTransformation(const MMatrix &);

		// Utility for getting the related matrix pointer
		normalTransformMatrix *getNormalTransformMatrix();

		virtual MMatrix getMatrix(MDGContext & 	context, MStatus *);

		virtual MStatus compute(const MPlug& plug, MDataBlock& block);

		const char*className();
		static void* creator();
		static MStatus	initialize();

		static MObject aLocalScale;
		static MObject aLocalScaleX;
		static MObject aLocalScaleY;
		static MObject aLocalScaleZ;

		static MObject aOffsetTranslate;
		static MObject aOffsetTranslateX;
		static MObject aOffsetTranslateY;
		static MObject aOffsetTranslateZ;

		static MObject aOffsetRotate;
		static MObject aOffsetRotateX;
		static MObject aOffsetRotateY;
		static MObject aOffsetRotateZ;

		static MObject aOffsetScale;
		static MObject aOffsetScaleX;
		static MObject aOffsetScaleY;
		static MObject aOffsetScaleZ;

		static MObject aParentOffsetMatrix;

		static	MTypeId	id;
	protected:
		// Degrees
		double localScaleX_val = 1.0;
		double localScaleY_val = 1.0;
		double localScaleZ_val = 1.0;

		double offsetTranslateX_val = 0.0;
		double offsetTranslateY_val = 0.0;
		double offsetTranslateZ_val = 0.0;

		double offsetRotateX_val = 0.0;
		double offsetRotateY_val = 0.0;
		double offsetRotateZ_val = 0.0;

		double offsetScaleX_val = 1.0;
		double offsetScaleY_val = 1.0;
		double offsetScaleZ_val = 1.0;

		typedef MPxTransform ParentClass;
};

MMatrix normalTransformNode::getMatrix(MDGContext & 	context, MStatus *)
{
	return 	MMatrix();
}

MObject normalTransformNode::aLocalScale;
MObject normalTransformNode::aLocalScaleX;
MObject normalTransformNode::aLocalScaleY;
MObject normalTransformNode::aLocalScaleZ;

MObject normalTransformNode::aOffsetTranslate;
MObject normalTransformNode::aOffsetTranslateX;
MObject normalTransformNode::aOffsetTranslateY;
MObject normalTransformNode::aOffsetTranslateZ;

MObject normalTransformNode::aOffsetRotate;
MObject normalTransformNode::aOffsetRotateX;
MObject normalTransformNode::aOffsetRotateY;
MObject normalTransformNode::aOffsetRotateZ;

MObject normalTransformNode::aOffsetScale;
MObject normalTransformNode::aOffsetScaleX;
MObject normalTransformNode::aOffsetScaleY;
MObject normalTransformNode::aOffsetScaleZ;

MObject normalTransformNode::aParentOffsetMatrix;

MTypeId normalTransformNode::id(0x00000006);


//
//	Constructor of the transform node
//
normalTransformNode::normalTransformNode()
	: ParentClass()
{
}

//	Post constructor method.  Have access to *this.  Node setup
//	operations that do not go into the initialize() method should go
//	here.
void normalTransformNode::postConstructor()
{
	//	Make sure the parent takes care of anything it needs.
	//
	ParentClass::postConstructor();

	// 	The baseTransformationMatrix pointer should be setup properly
	//	at this point, but just in case, set the value if it is missing.
	//
	if (NULL == transformationMatrixPtr()) {
		//baseTransformationMatrix = new MPxTransformationMatrix();//???
	}
	//MPlug aLocalScaleX_plug(thisMObject(), aLocalScaleX);//???
	//MPlug aLocalScaleY_plug(thisMObject(), aLocalScaleY);
	//MPlug aLocalScaleZ_plug(thisMObject(), aLocalScaleZ);
}

MStatus normalTransformNode::compute(const MPlug& plug, MDataBlock& block)
{
	MGlobal::displayInfo("running compute");
	MStatus returnStatus;
	if ((plug.attribute() == MPxTransform::matrix)
		|| (plug.attribute() == MPxTransform::inverseMatrix)
		|| (plug.attribute() == MPxTransform::worldMatrix)
		|| (plug.attribute() == MPxTransform::worldInverseMatrix)
		|| (plug.attribute() == MPxTransform::parentMatrix)
		|| (plug.attribute() == MPxTransform::parentInverseMatrix))
	{
		MStatus status = MS::kSuccess;

		//get the MPxTransformationMatrix instance that is paired with this node
		normalTransformMatrix *ltm = getNormalTransformMatrix();

		//get the localScale translate plug values
		MPlug localScaleX_plug(thisMObject(), aLocalScaleX);
		MPlug localScaleY_plug(thisMObject(), aLocalScaleY);
		MPlug localScaleZ_plug(thisMObject(), aLocalScaleZ);

		MDataHandle localScaleXHandle = block.inputValue(localScaleX_plug, &status);
		MDataHandle localScaleYHandle = block.inputValue(localScaleY_plug, &status);
		MDataHandle localScaleZHandle = block.inputValue(localScaleZ_plug, &status);

		localScaleX_val = localScaleXHandle.asDouble();
		localScaleY_val = localScaleYHandle.asDouble();
		localScaleZ_val = localScaleZHandle.asDouble();

		//get the offset translate plug values
		MPlug offsetTranslateX_plug(thisMObject(), aOffsetTranslateX);
		MPlug offsetTranslateY_plug(thisMObject(), aOffsetTranslateY);
		MPlug offsetTranslateZ_plug(thisMObject(), aOffsetTranslateZ);

		MDataHandle offsetTranslateXHandle = block.inputValue(offsetTranslateX_plug, &status);
		MDataHandle offsetTranslateYHandle = block.inputValue(offsetTranslateY_plug, &status);
		MDataHandle offsetTranslateZHandle = block.inputValue(offsetTranslateZ_plug, &status);

		offsetTranslateX_val = offsetTranslateXHandle.asDouble();
		offsetTranslateY_val = offsetTranslateYHandle.asDouble();
		offsetTranslateZ_val = offsetTranslateZHandle.asDouble();

		//get the offset rotate plug values
		MPlug offsetRotateX_plug(thisMObject(), aOffsetRotateX);
		MPlug offsetRotateY_plug(thisMObject(), aOffsetRotateY);
		MPlug offsetRotateZ_plug(thisMObject(), aOffsetRotateZ);

		MDataHandle offsetRotateXHandle = block.inputValue(offsetRotateX_plug, &status);
		MDataHandle offsetRotateYHandle = block.inputValue(offsetRotateY_plug, &status);
		MDataHandle offsetRotateZHandle = block.inputValue(offsetRotateZ_plug, &status);

		offsetRotateX_val = offsetRotateXHandle.asDouble();
		offsetRotateY_val = offsetRotateYHandle.asDouble();
		offsetRotateZ_val = offsetRotateZHandle.asDouble();

		//get the offset rotate plug values
		MPlug offsetScaleX_plug(thisMObject(), aOffsetScaleX);
		MPlug offsetScaleY_plug(thisMObject(), aOffsetScaleY);
		MPlug offsetScaleZ_plug(thisMObject(), aOffsetScaleZ);

		MDataHandle offsetScaleXHandle = block.inputValue(offsetScaleX_plug, &status);
		MDataHandle offsetScaleYHandle = block.inputValue(offsetScaleY_plug, &status);
		MDataHandle offsetScaleZHandle = block.inputValue(offsetScaleZ_plug, &status);

		offsetScaleX_val = offsetScaleXHandle.asDouble();
		offsetScaleY_val = offsetScaleYHandle.asDouble();
		offsetScaleZ_val = offsetScaleZHandle.asDouble();

		// Update the values in the MPxTransformationMatrix

		if (ltm)
		{
			ltm->setLocalScaleX(localScaleX_val);
			ltm->setLocalScaleY(localScaleY_val);
			ltm->setLocalScaleZ(localScaleZ_val);

			ltm->setOffsetTranslateX(offsetTranslateX_val);
			ltm->setOffsetTranslateY(offsetTranslateY_val);
			ltm->setOffsetTranslateZ(offsetTranslateZ_val);

			ltm->setOffsetRotateX(offsetRotateX_val);
			ltm->setOffsetRotateY(offsetRotateY_val);
			ltm->setOffsetRotateZ(offsetRotateZ_val);

			ltm->setOffsetScaleX(offsetScaleX_val);
			ltm->setOffsetScaleY(offsetScaleY_val);
			ltm->setOffsetScaleZ(offsetScaleZ_val);
		}
		else
		{
			MGlobal::displayInfo("Failed to get normal transform matrix");
		}
	}
	return MPxTransform::compute(plug, block);
}

//
// Destructor of the normal transform
//
normalTransformNode::~normalTransformNode()
{
}

//
// Method that returns the new transformation matrix
//
MPxTransformationMatrix *normalTransformNode::createTransformationMatrix()
{
	return new normalTransformMatrix();
}


//
// Method that returns a new transform node
//
void *normalTransformNode::creator()
{
	return new normalTransformNode();
}

//
//    Node initialize method. We configure node
//    attributes here. Static method so
//    the *this pointer is not available.
//
MStatus normalTransformNode::initialize()
{
	MFnCompoundAttribute compoundFn;
	MFnUnitAttribute unitFn;

	//local scale attributes
	aLocalScale = compoundFn.create("localScale", "ls");

	aLocalScaleX = unitFn.create("localScaleX", "lsX", MFnUnitAttribute::kDistance, 1.0);
	unitFn.setKeyable(true);
	unitFn.setAffectsWorldSpace(true);
	compoundFn.addChild(aLocalScaleX);

	aLocalScaleY = unitFn.create("localScaleY", "lsY", MFnUnitAttribute::kDistance, 1.0);
	unitFn.setKeyable(true);
	unitFn.setAffectsWorldSpace(true);
	compoundFn.addChild(aLocalScaleY);

	aLocalScaleZ = unitFn.create("localScaleZ", "lsZ", MFnUnitAttribute::kDistance, 1.0);
	unitFn.setKeyable(true);
	unitFn.setAffectsWorldSpace(true);
	compoundFn.addChild(aLocalScaleZ);

	//offset translate attributes
	aOffsetTranslate = compoundFn.create("offsetTranslate", "ot");

	aOffsetTranslateX = unitFn.create("offsetTranslateX", "otX", MFnUnitAttribute::kDistance, 0.0);
	unitFn.setKeyable(true);
	unitFn.setAffectsWorldSpace(true);
	compoundFn.addChild(aOffsetTranslateX);

	aOffsetTranslateY = unitFn.create("offsetTranslateY", "otY", MFnUnitAttribute::kDistance, 0.0);
	unitFn.setKeyable(true);
	unitFn.setAffectsWorldSpace(true);
	compoundFn.addChild(aOffsetTranslateY);

	aOffsetTranslateZ = unitFn.create("offsetTranslateZ", "otZ", MFnUnitAttribute::kDistance, 0.0);
	unitFn.setKeyable(true);
	unitFn.setAffectsWorldSpace(true);
	compoundFn.addChild(aOffsetTranslateZ);

	//offset rotate attributes
	aOffsetRotate = compoundFn.create("offsetRotate", "or");

	aOffsetRotateX = unitFn.create("offsetRotateX", "orX", MFnUnitAttribute::kDistance, 0.0);
	unitFn.setKeyable(true);
	unitFn.setAffectsWorldSpace(true);
	compoundFn.addChild(aOffsetRotateX);

	aOffsetRotateY = unitFn.create("offsetRotateY", "orY", MFnUnitAttribute::kDistance, 0.0);
	unitFn.setKeyable(true);
	unitFn.setAffectsWorldSpace(true);
	compoundFn.addChild(aOffsetRotateY);

	aOffsetRotateZ = unitFn.create("offsetRotateZ", "orZ", MFnUnitAttribute::kDistance, 0.0);
	unitFn.setKeyable(true);
	unitFn.setAffectsWorldSpace(true);
	compoundFn.addChild(aOffsetRotateZ);

	//offset Scale attributes
	aOffsetScale = compoundFn.create("offsetScale", "os");

	aOffsetScaleX = unitFn.create("offsetScaleX", "osX", MFnUnitAttribute::kDistance, 1.0);
	unitFn.setKeyable(true);
	unitFn.setAffectsWorldSpace(true);
	compoundFn.addChild(aOffsetScaleX);

	aOffsetScaleY = unitFn.create("offsetScaleY", "osY", MFnUnitAttribute::kDistance, 1.0);
	unitFn.setKeyable(true);
	unitFn.setAffectsWorldSpace(true);
	compoundFn.addChild(aOffsetScaleY);

	aOffsetScaleZ = unitFn.create("offsetScaleZ", "osZ", MFnUnitAttribute::kDistance, 1.0);
	unitFn.setKeyable(true);
	unitFn.setAffectsWorldSpace(true);
	compoundFn.addChild(aOffsetScaleZ);

	addAttribute(aLocalScale);
	addAttribute(aOffsetTranslate);
	addAttribute(aOffsetRotate);
	addAttribute(aOffsetScale);

	// Need to update matrix when attribute is modified
	attributeAffects(aLocalScaleX, matrix);
	attributeAffects(aLocalScaleY, matrix);
	attributeAffects(aLocalScaleZ, matrix);

	attributeAffects(aOffsetTranslateX, matrix);
	attributeAffects(aOffsetTranslateY, matrix);
	attributeAffects(aOffsetTranslateZ, matrix);

	attributeAffects(aOffsetRotateX, matrix);
	attributeAffects(aOffsetRotateY, matrix);
	attributeAffects(aOffsetRotateZ, matrix);

	attributeAffects(aOffsetScaleX, matrix);
	attributeAffects(aOffsetScaleY, matrix);
	attributeAffects(aOffsetScaleZ, matrix);

	// This is required so that the validateAndSet method 
	// is called
	mustCallValidateAndSet(aLocalScaleX);
	mustCallValidateAndSet(aLocalScaleY);
	mustCallValidateAndSet(aLocalScaleZ);

	mustCallValidateAndSet(aOffsetTranslateX);
	mustCallValidateAndSet(aOffsetTranslateY);
	mustCallValidateAndSet(aOffsetTranslateZ);

	mustCallValidateAndSet(aOffsetRotateX);
	mustCallValidateAndSet(aOffsetRotateY);
	mustCallValidateAndSet(aOffsetRotateZ);

	mustCallValidateAndSet(aOffsetScaleX);
	mustCallValidateAndSet(aOffsetScaleY);
	mustCallValidateAndSet(aOffsetScaleZ);
	return MS::kSuccess;
}

//
//	Debugging method
//
const char* normalTransformNode::className()
{
	return "normalTransformNode";
}

//
//	Reset transformation
//
void  normalTransformNode::resetTransformation(const MMatrix &matrix)
{
	ParentClass::resetTransformation(matrix);
}

//
//	Reset transformation
//
void  normalTransformNode::resetTransformation(MPxTransformationMatrix *resetMatrix)
{
	ParentClass::resetTransformation(resetMatrix);
}


//this method updates the data in the MTransformationMatrix
MStatus normalTransformNode::validateAndSetValue(
const MPlug& plug,
    const MDataHandle& handle)
{
    MStatus status = MS::kSuccess;
    // Make sure that there is something interesting to process.
    //
    if (plug.isNull())
        return MS::kFailure;

    MDataBlock block = forceCache();
    MDataHandle blockHandle = block.outputValue(plug, &status);
    
	//========================
	//	LocalScale
	//========================

	if (plug == aLocalScaleX)
	{
		// Update the value
		double localScaleXin = handle.asDouble();
		blockHandle.set(localScaleXin);
		localScaleX_val = localScaleXin;
		// Update the custom transformation matrix to the correct value
		normalTransformMatrix *ltm = getNormalTransformMatrix();
		if (ltm) {
				MGlobal::displayInfo(MString("setting local scale X:") + localScaleX_val); 
				ltm->setLocalScaleX(localScaleXin);
		}
		else {
			MGlobal::displayError("Failed to get rock transform matrix");
		}
            
        blockHandle.setClean();
        
        // Mark the matrix as dirty so that DG information will update.
		MGlobal::displayInfo("matrix is being dirted");
        dirtyMatrix();  
		return MS::kSuccess;
    }

	if (plug == aLocalScaleY)
	{
		// Update the value
		double localScaleYin = handle.asDouble();
		blockHandle.set(localScaleYin);
		localScaleY_val = localScaleYin;

		// Update the custom transformation matrix to the correct value
		normalTransformMatrix *ltm = getNormalTransformMatrix();
		if (ltm)
			ltm->setLocalScaleY(localScaleYin);
		else
			MGlobal::displayError("Failed to get rock transform matrix");

		blockHandle.setClean();

		// Mark the matrix as dirty so that DG information will update.
		dirtyMatrix();
		return MS::kSuccess;
	}
    
	if (plug == aLocalScaleZ)
	{
		// Update the value
		double localScaleZin = handle.asDouble();
		blockHandle.set(localScaleZin);
		localScaleZ_val = localScaleZin;

		// Update the custom transformation matrix to the correct value
		normalTransformMatrix *ltm = getNormalTransformMatrix();
		if (ltm)
			ltm->setLocalScaleZ(localScaleZin);
		else
			MGlobal::displayError("Failed to get rock transform matrix");//!!! remove these

		blockHandle.setClean();

		// Mark the matrix as dirty so that DG information will update.
		dirtyMatrix();
		return MS::kSuccess;
	}

	//========================
	//	offsetTranslate
	//========================

	if (plug == aOffsetTranslateX)
	{
		// Update the value
		double offsetTranslateXin = handle.asDouble();
		blockHandle.set(offsetTranslateXin);
		offsetTranslateX_val = offsetTranslateXin;

		// Update the custom transformation matrix to the correct value
		normalTransformMatrix *ltm = getNormalTransformMatrix();
		if (ltm)
			ltm->setOffsetTranslateX(offsetTranslateXin);
		else
			MGlobal::displayError("Failed to get rock transform matrix");//!!! remove these

		blockHandle.setClean();

		// Mark the matrix as dirty so that DG information will update.
		dirtyMatrix();
		return MS::kSuccess;
	}

	if (plug == aOffsetTranslateY)
	{
		// Update the value
		double offsetTranslateYin = handle.asDouble();
		blockHandle.set(offsetTranslateYin);
		offsetTranslateY_val = offsetTranslateYin;

		// Update the custom transformation matrix to the correct value
		normalTransformMatrix *ltm = getNormalTransformMatrix();
		if (ltm)
			ltm->setOffsetTranslateY(offsetTranslateYin);
		else
			MGlobal::displayError("Failed to get rock transform matrix");//!!! remove these

		blockHandle.setClean();

		// Mark the matrix as dirty so that DG information will update.
		dirtyMatrix();
		return MS::kSuccess;
	}
	
	if (plug == aOffsetTranslateZ)
	{
		// Update the value
		double offsetTranslateZin = handle.asDouble();
		blockHandle.set(offsetTranslateZin);
		offsetTranslateZ_val = offsetTranslateZin;

		// Update the custom transformation matrix to the correct value
		normalTransformMatrix *ltm = getNormalTransformMatrix();
		if (ltm)
			ltm->setOffsetTranslateZ(offsetTranslateZin);
		else
			MGlobal::displayError("Failed to get rock transform matrix");//!!! remove these

		blockHandle.setClean();

		// Mark the matrix as dirty so that DG information will update.
		dirtyMatrix();
		return MS::kSuccess;
	}

	//========================
	//	offsetRotate
	//========================

	if (plug == aOffsetRotateX)
	{
		// Update the value
		double offsetRotateXin = handle.asDouble();
		blockHandle.set(offsetRotateXin);
		offsetRotateX_val = offsetRotateXin;

		// Update the custom transformation matrix to the correct value
		normalTransformMatrix *ltm = getNormalTransformMatrix();
		if (ltm)
			ltm->setOffsetRotateX(offsetRotateXin);
		else
			MGlobal::displayError("Failed to get rock transform matrix");//!!! remove these

		blockHandle.setClean();

		// Mark the matrix as dirty so that DG information will update.
		dirtyMatrix();
		return MS::kSuccess;
	}

	if (plug == aOffsetRotateY)
	{
		// Update the value
		double offsetRotateYin = handle.asDouble();
		blockHandle.set(offsetRotateYin);
		offsetRotateY_val = offsetRotateYin;

		// Update the custom transformation matrix to the correct value
		normalTransformMatrix *ltm = getNormalTransformMatrix();
		if (ltm)
			ltm->setOffsetRotateY(offsetRotateYin);
		else
			MGlobal::displayError("Failed to get rock transform matrix");//!!! remove these

		blockHandle.setClean();

		// Mark the matrix as dirty so that DG information will update.
		dirtyMatrix();
		return MS::kSuccess;
	}

	if (plug == aOffsetRotateZ)
	{
		// Update the value
		double offsetRotateZin = handle.asDouble();
		blockHandle.set(offsetRotateZin);
		offsetRotateZ_val = offsetRotateZin;

		// Update the custom transformation matrix to the correct value
		normalTransformMatrix *ltm = getNormalTransformMatrix();
		if (ltm)
			ltm->setOffsetRotateZ(offsetRotateZin);
		else
			MGlobal::displayError("Failed to get rock transform matrix");//!!! remove these

		blockHandle.setClean();

		// Mark the matrix as dirty so that DG information will update.
		dirtyMatrix();
		return MS::kSuccess;
	}


	//========================
	//	offsetScale
	//========================
	if (plug == aOffsetScaleX)
	{
		// Update the value
		double offsetScaleXin = handle.asDouble();
		blockHandle.set(offsetScaleXin);
		offsetScaleX_val = offsetScaleXin;

		// Update the custom transformation matrix to the correct value
		normalTransformMatrix *ltm = getNormalTransformMatrix();
		if (ltm)
			ltm->setOffsetScaleX(offsetScaleXin);
		else
			MGlobal::displayError("Failed to get rock transform matrix");//!!! remove these

		blockHandle.setClean();

		// Mark the matrix as dirty so that DG information will update.
		dirtyMatrix();
		return MS::kSuccess;
	}

	if (plug == aOffsetScaleY)
	{
		// Update the value
		double offsetScaleYin = handle.asDouble();
		blockHandle.set(offsetScaleYin);
		offsetScaleY_val = offsetScaleYin;

		// Update the custom transformation matrix to the correct value
		normalTransformMatrix *ltm = getNormalTransformMatrix();
		if (ltm)
			ltm->setOffsetScaleY(offsetScaleYin);
		else
			MGlobal::displayError("Failed to get rock transform matrix");//!!! remove these

		blockHandle.setClean();

		// Mark the matrix as dirty so that DG information will update.
		dirtyMatrix();
		return MS::kSuccess;
	}

	if (plug == aOffsetScaleZ)
	{
		// Update the value
		double offsetScaleZin = handle.asDouble();
		blockHandle.set(offsetScaleZin);
		offsetScaleZ_val = offsetScaleZin;

		// Update the custom transformation matrix to the correct value
		normalTransformMatrix *ltm = getNormalTransformMatrix();
		if (ltm)
			ltm->setOffsetScaleZ(offsetScaleZin);
		else
			MGlobal::displayError("Failed to get rock transform matrix");//!!! remove these

		blockHandle.setClean();

		// Mark the matrix as dirty so that DG information will update.
		dirtyMatrix();
		return MS::kSuccess;
	}

    // Allow processing for other attributes
    return ParentClass::validateAndSetValue(plug, handle);
}

//
//	Method for returning the current rocking transformation matrix
//
normalTransformMatrix *normalTransformNode::getNormalTransformMatrix()
{
	normalTransformMatrix *ltm = (normalTransformMatrix *)transformationMatrixPtr();
	return ltm;
}

//fix the manipulator stuff!!!
Message 3 of 6

jmreinhart
Advisor
Advisor
Accepted solution

I was able to find the issue. The compound attributes I added needed to be set using setAffectsWorldSpace(true), not just their children. Something to do with how Maya deals with dirty propagation for compound attributes. 

Message 4 of 6

chirieacam
Enthusiast
Enthusiast

Hi! Did you managed to "fix the manipulator stuff", or the other issues involved with a custom transform?

 

I also was trying to create a custom transform node to add offset values for translate/rotate/scale, but I got stuck with mainly 3 issues:

 

  •  The rotate manipulator doesn't take the offset into consideration, so the orientation is wrong. It seems we need a custom manipulator for that, but I couldn't find a way to replace it with the manipulator that gets created when choosing the default rotate tool. If only we could just get a handle for the active manipulator and update its orientation...

 

  • Getting the transform's world rotation using xform doesn't work, it always return without the offset. I couldn't find what else I need to override in my node to make it work. The default joint or hikEffector both have extra pre-rotation/offset, and they do return the correct results, so there must be a way to do it I guess. 
    maya.cmds.xform(obj, query=True, rotation=True, worldSpace=True)
     
  • Using Modify->Match Transformations->Match Rotation command also fails to take the offset into account. It might be related to issue #2 using xform.


Did you found a way to deal with all those issues for your custom transform node?

 

Currently the best way I found to have a node with offset transformations is to use the joint transform node. We can use the jointOrient for rotation, the inverseScale for scale offset (by dividing the values, 1/x), and for translation just updating the translation part of the offsetParentMatrix. I have a custom locator shape parented under the joint to make it easier to handle:

 

maya_offset_values.jpg

 
It seems to work well enough, but if I have inheritsTransform set to False on the joint the jointOrient doesn't mark the worldMatrix (and all its children) as dirty. So, if the worldMatrix[0] is connected to a target object's offsetParentMatrix the object will not update. Interestingly enough, if I check inheritsTransform and uncheck it right after the node gets updated, even if the value for inheritsTransform is still false. Weird...

With a custom transform node I could have overriden the setDependentsDirty method and mark the necessary plugs as dirty manually.
Message 5 of 6

jmreinhart
Advisor
Advisor

So you're correct you need custom manipulators so you can define how to take the extra attributes into account. I made some good progress on them but then found out that the way Maya's manipulators work and how they are described by the MPxManipContainer documentation are not the same. I haven't had time to revisit the manipulator issue. 

 

I'm not sure what could cause the xform issue but it's definitely inconsequential without a working custom manipulator.

 

It's funny that you came up with that joint and buffer node workaround because I did the same thing. https://www.jonah-reinhart.com/single-post/normalized-translation-in-vanilla-maya

 

not sure why you have inheritTransforms off, but having it off will prevent the worldMatrix from changing because when it's off the matrix and worldMatrix outputs should be the same (though I haven't checked how the offsetParentMatrix affects the worldMatrix output)

0 Likes
Message 6 of 6

chirieacam
Enthusiast
Enthusiast

Thanks for letting me know!

 

I gave up on the custom transform for now and I'm just using the joint to have pre-translate/rotate/scale values. Seems to work just fine in most cases, though there are some issues to watch for.

 

For example, doing constraints on the joint itself then reconnecting the output values to my desired "buffer" node gave me problems in getting the transformations work as expected. I just want the joint to act as a normal transform node, but constraints are aware of joints and do a bunch of other stuff specific to them.

So, the solution was to not use the constraints commands anymore, but just manually creating the constraint using the createNode command and do the proper constraint configuration/connections myself.

 

Regarding the inheritsTransforms off, I was doing that because my node will have dynamic space-switching, so the parent will be whatever the user selects, no matter the real hierarchy. Using inheritsTransforms seemed to be the easiest thing to ignore the hierarchy, but since that it's not working I guess I'll have to manually remove the parent transformation myself from the final output.

0 Likes