Created
October 15, 2024 03:20
-
-
Save gregoiredehame/266a97e89fdd68177edfec079b8c88de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <maya/MPxNode.h> | |
| #include <maya/MFnNumericAttribute.h> | |
| #include <maya/MFnTypedAttribute.h> | |
| #include <maya/MFnMatrixAttribute.h> | |
| #include <maya/MFnNurbsCurve.h> | |
| #include <maya/MFnNurbsCurveData.h> | |
| #include <maya/MTransformationMatrix.h> | |
| #include <maya/MArrayDataHandle.h> | |
| #include <maya/MPointArray.h> | |
| #include <maya/MGlobal.h> | |
| #include <maya/MMatrix.h> | |
| #include <maya/MFnPlugin.h> | |
| class CurveConstraint : public MPxNode | |
| { | |
| public: | |
| CurveConstraint() {} | |
| virtual ~CurveConstraint() override {} | |
| static void* creator() { return new CurveConstraint(); } | |
| static MStatus initialize(); | |
| virtual MStatus compute(const MPlug& plug, MDataBlock& data) override; | |
| static MTypeId id; | |
| static MObject inputMatrix; | |
| static MObject outputCurve; | |
| }; | |
| MTypeId CurveConstraint::id(0x00124ddf); | |
| MObject CurveConstraint::inputMatrix; | |
| MObject CurveConstraint::outputCurve; | |
| MStatus CurveConstraint::initialize() | |
| { | |
| MFnMatrixAttribute fnMatrix; | |
| MFnTypedAttribute fnTyped; | |
| // INPUTS | |
| inputMatrix = fnMatrix.create("inputMatrix", "ia", MFnMatrixAttribute::kDouble); | |
| fnMatrix.setArray(true); | |
| fnMatrix.setUsesArrayDataBuilder(true); | |
| fnMatrix.setStorable(true); | |
| fnMatrix.setWritable(true); | |
| addAttribute(inputMatrix); | |
| // OUTPUTS | |
| outputCurve = fnTyped.create("outputCurve", "oc", MFnData::kNurbsCurve); | |
| fnTyped.setStorable(false); | |
| fnTyped.setWritable(false); | |
| addAttribute(outputCurve); | |
| // CONNECT | |
| attributeAffects(inputMatrix, outputCurve); | |
| return MS::kSuccess; | |
| } | |
| MStatus CurveConstraint::compute(const MPlug& plug, MDataBlock& data) | |
| { | |
| if (plug != outputCurve) | |
| { | |
| return MS::kUnknownParameter; | |
| } | |
| MArrayDataHandle inputMatrixHandle = data.inputArrayValue(inputMatrix); | |
| MPointArray points; | |
| for (unsigned int i = 0; i < inputMatrixHandle.elementCount(); ++i, inputMatrixHandle.next()) | |
| { | |
| MMatrix matrix = inputMatrixHandle.inputValue().asMatrix(); | |
| MTransformationMatrix transform(matrix); | |
| points.append(transform.getTranslation(MSpace::kWorld)); | |
| } | |
| unsigned int numPoints = points.length(); | |
| if (numPoints < 2) | |
| { | |
| return MS::kSuccess; | |
| } | |
| unsigned int degree = (numPoints == 2) ? 1 : (numPoints == 3) ? 2 : 3; | |
| unsigned int numKnots = numPoints + degree - 1; | |
| MDoubleArray knots; | |
| for (unsigned int i = 0; i < numKnots; ++i) | |
| { | |
| if (i < degree) | |
| { | |
| knots.append(0.0); | |
| } | |
| else if (i >= numKnots - degree) | |
| { | |
| knots.append(numPoints - degree); | |
| } | |
| else | |
| { | |
| knots.append(i - degree + 1); | |
| } | |
| } | |
| MFnNurbsCurve curveFn; | |
| MObject curveData = MFnNurbsCurveData().create(); | |
| curveFn.create(points, knots, degree, MFnNurbsCurve::kOpen, false, false, curveData); | |
| MDataHandle outputCurveHandle = data.outputValue(outputCurve); | |
| outputCurveHandle.setMObject(curveData); | |
| outputCurveHandle.setClean(); | |
| return MS::kSuccess; | |
| } | |
| MStatus initializePlugin(MObject obj) | |
| { | |
| MFnPlugin plugin(obj, "Gregoire Dehame", "1.0", "Any"); | |
| return plugin.registerNode("curveConstraint", CurveConstraint::id, CurveConstraint::creator, CurveConstraint::initialize); | |
| } | |
| MStatus uninitializePlugin(MObject obj) | |
| { | |
| MFnPlugin plugin(obj); | |
| return plugin.deregisterNode(CurveConstraint::id); | |
| } |
Author
gregoiredehame
commented
Oct 15, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment