Created
October 15, 2024 03:19
-
-
Save gregoiredehame/ce9668719ac49cdab82574cee0d82b36 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
| import maya.api.OpenMaya as om2 | |
| def maya_useNewAPI(): | |
| pass | |
| class curveConstraint(om2.MPxNode): | |
| id_ = om2.MTypeId(0x00124ddf) | |
| def __init__(self) -> None: | |
| super(curveConstraint, self).__init__() | |
| @classmethod | |
| def creator(cls) -> om2.MPxNode: | |
| return cls() | |
| @classmethod | |
| def initialize(cls) -> None: | |
| fn_matrix = om2.MFnMatrixAttribute() | |
| fn_typed = om2.MFnTypedAttribute() | |
| # INPUTS | |
| cls.input_matrix = fn_matrix.create("inputMatrix", "ia", om2.MFnMatrixAttribute.kDouble) | |
| fn_matrix.array = True | |
| fn_matrix.usesArrayDataBuilder = True | |
| fn_matrix.storable = True | |
| fn_matrix.writable = True | |
| cls.addAttribute(cls.input_matrix) | |
| # OUTPUTS | |
| cls.output_curve = fn_typed.create("outputCurve", "oc", om2.MFnData.kNurbsCurve) | |
| fn_typed.storable = False | |
| fn_typed.writable = False | |
| cls.addAttribute(cls.output_curve) | |
| # CONNECT | |
| cls.attributeAffects(cls.input_matrix, cls.output_curve) | |
| def get_points(self, data: om2.MDataBlock) -> om2.MPointArray: | |
| input_matrix_handle = data.inputArrayValue(self.input_matrix) | |
| points = om2.MPointArray() | |
| while not input_matrix_handle.isDone(): | |
| matrix = input_matrix_handle.inputValue().asMatrix() | |
| points.append(om2.MPoint(om2.MTransformationMatrix(matrix).translation(om2.MSpace.kWorld))) | |
| input_matrix_handle.next() | |
| return points | |
| def compute(self, plug: om2.MPlug, data: om2.MDataBlock) -> None: | |
| if plug == self.output_curve: | |
| points = self.get_points(data) | |
| num_points = len(points) | |
| if num_points < 2: | |
| return | |
| degree = 1 if num_points == 2 else 2 if num_points == 3 else 3 | |
| num_knots = num_points + degree - 1 | |
| knots = om2.MDoubleArray([0.0 if i < degree else num_points - degree if i >= num_knots - degree else i - degree + 1 for i in range(num_knots)]) | |
| curve_fn = om2.MFnNurbsCurve() | |
| curve_data = om2.MFnNurbsCurveData().create() | |
| curve_fn.create(points, knots, degree, om2.MFnNurbsCurve.kOpen, False, False, curve_data) | |
| output_curve_handle = data.outputValue(self.output_curve) | |
| output_curve_handle.setMObject(curve_data) | |
| output_curve_handle.setClean() | |
| data.setClean(plug) | |
| def initializePlugin(obj: om2.MObject) -> None: | |
| fn_plugin = om2.MFnPlugin(obj) | |
| fn_plugin.registerNode(curveConstraint.__name__, curveConstraint.id_, curveConstraint.creator, curveConstraint.initialize) | |
| def uninitializePlugin(obj: om2.MObject) -> None: | |
| fn_plugin = om2.MFnPlugin(obj) | |
| fn_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