Python maya.OpenMaya.MFnNurbsCurve() Examples

The following are 8 code examples of maya.OpenMaya.MFnNurbsCurve(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module maya.OpenMaya , or try the search function .
Example #1
Source File: instanceAlongCurve.py    From instanceAlongCurve with MIT License 6 votes vote down vote up
def getCurveFn(self):
        inputCurvePlug = OpenMaya.MPlug(self.thisMObject(), instanceAlongCurveLocator.inputCurveAttr)
        curve = getSingleSourceObjectFromPlug(inputCurvePlug)

        # Get Fn from a DAG path to get the world transformations correctly
        if curve is not None:
            path = OpenMaya.MDagPath()
            trFn = OpenMaya.MFnDagNode(curve)
            trFn.getPath(path)

            path.extendToShape()

            if path.node().hasFn(OpenMaya.MFn.kNurbsCurve):
                return OpenMaya.MFnNurbsCurve(path)

        return None

    # Calculate expected instances by the instancing mode 
Example #2
Source File: api.py    From maya-spline-ik with GNU General Public License v3.0 5 votes vote down vote up
def asMFnNurbsCurve(curve):
    """
    Convert a node into a OpenMaya.MFnNurbsCurve.

    :param str node:
    :return: MFnNurbsCurve of parsed curve
    :rtype: OpenMaya.MFnNurbsCurve
    """
    dag = toMDagPath(curve)
    nurbsCurveFn = OpenMaya.MFnNurbsCurve(dag)

    return nurbsCurveFn 
Example #3
Source File: curve.py    From mgear_core with MIT License 5 votes vote down vote up
def getCurveParamAtPosition(crv, position):
    """Get curve parameter from a position

    Arguments:
        position (list of float): Represents the position in worldSpace
            exp: [1.4, 3.55, 42.6]
        crv (curve): The  source curve to get the parameter.

    Returns:
        list: paramenter and curve length
    """
    point = om.MPoint(position[0], position[1], position[2])

    dag = om.MDagPath()
    obj = om.MObject()
    oList = om.MSelectionList()
    oList.add(crv.name())
    oList.getDagPath(0, dag, obj)

    curveFn = om.MFnNurbsCurve(dag)
    length = curveFn.length()
    crv.findParamFromLength(length)

    paramUtill = om.MScriptUtil()
    paramPtr = paramUtill.asDoublePtr()

    point = curveFn.closestPoint(point, paramPtr, 0.001, om.MSpace.kObject)
    curveFn.getParamAtPoint(point, paramPtr, 0.001, om.MSpace.kObject)

    param = paramUtill.getDouble(paramPtr)

    return param, length 
Example #4
Source File: cmdx.py    From cmdx with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def editCurve(parent, points, degree=1, form=kOpen):
    assert isinstance(parent, DagNode), (
        "parent must be of type cmdx.DagNode"
    )

    degree = min(3, max(1, degree))

    cvs = om1.MPointArray()
    curveFn = om1.MFnNurbsCurve()

    for point in points:
        cvs.append(om1.MPoint(*point))

    mobj = curveFn.createWithEditPoints(cvs,
                                        degree,
                                        form,
                                        False,
                                        False,
                                        True,
                                        _encode1(parent.path()))

    mod = om1.MDagModifier()
    mod.renameNode(mobj, parent.name(namespace=True) + "Shape")
    mod.doIt()

    def undo():
        mod.deleteNode(mobj)
        mod.doIt()

    def redo():
        mod.undoIt()

    commit(undo, redo)

    shapeFn = om1.MFnDagNode(mobj)
    return encode(shapeFn.fullPathName()) 
Example #5
Source File: cmdx.py    From cmdx with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def NurbsCurveData(points, degree=1, form=om1.MFnNurbsCurve.kOpen):
    """Tuple of points to MObject suitable for nurbsCurve-typed data

    Arguments:
        points (tuple): (x, y, z) tuples per point
        degree (int, optional): Defaults to 1 for linear
        form (int, optional): Defaults to MFnNurbsCurve.kOpen,
            also available kClosed

    Example:
        Create a new nurbs curve like this.

        >>> data = NurbsCurveData(
        ...     points=(
        ...         (0, 0, 0),
        ...         (0, 1, 0),
        ...         (0, 2, 0),
        ...     ))
        ...
        >>> parent = createNode("transform")
        >>> shape = createNode("nurbsCurve", parent=parent)
        >>> shape["cached"] = data

    """

    degree = min(3, max(1, degree))

    cvs = om1.MPointArray()
    curveFn = om1.MFnNurbsCurve()
    data = om1.MFnNurbsCurveData()
    mobj = data.create()

    for point in points:
        cvs.append(om1.MPoint(*point))

    curveFn.createWithEditPoints(cvs,
                                 degree,
                                 form,
                                 False,
                                 False,
                                 True,
                                 mobj)

    return mobj 
Example #6
Source File: closestPointOnCurve.py    From anima with MIT License 4 votes vote down vote up
def compute(self, plug, dataBlock):
        if plug == closestPointOnCurve.aOutPosition or plug == closestPointOnCurve.aOutParam:
            dataHandle = dataBlock.inputValue(closestPointOnCurve.aInCurve)
            inputAsCurve = dataHandle.asNurbsCurve()
            
            #if not inputAsCurve.hasFn(OpenMaya.MFn.kNurbsCurve):
            #    return OpenMaya.kUnknownParameter
            
            dataHandle = dataBlock.inputValue(closestPointOnCurve.aInPosition)
            
            inPositionAsFloat3 = dataHandle.asFloat3()
            inPosition = OpenMaya.MPoint(
                inPositionAsFloat3[0],
                inPositionAsFloat3[1],
                inPositionAsFloat3[2]
            )
            
            # connect the MFnNurbsCurve
            # and ask the closest point
            
            nurbsCurveFn = OpenMaya.MFnNurbsCurve(inputAsCurve)
            
            # get and set outPosition
            outParam = OpenMaya.MScriptUtil()
            outParam.createFromDouble(0)
            outParamPtr = outParam.asDoublePtr() 
            
            # get position and paramater
            outPosition = nurbsCurveFn.closestPoint(
                inPosition, True, outParamPtr, 0.001, OpenMaya.MSpace.kWorld
            )
            
            outputHandle = dataBlock.outputValue(
                closestPointOnCurve.aOutPosition
            )
            outputHandle.set3Float(outPosition.x, outPosition.y, outPosition.z)
            
            # get and set outNormal
            #outNormal = nurbsCurveFn.normal(parameter, OpenMaya.MSpace.kWorld)
            #outputHandle = dataBlock.outputValue(closestPointOnCurve.aOutNormal)
            #outputHandle.set3Float(outNormal.x, outNormal.y, outNormal.z)
            #outputHandle.set3Float(0, 1, 0 )
            
            # get and set the uvs
            outputHandle = dataBlock.outputValue(closestPointOnCurve.aOutParam)
            #outputHandle.setFloat(OpenMaya.MScriptUtil(outParamPtr).asDouble())
            outputHandle.setFloat(OpenMaya.MScriptUtil.getDouble(outParamPtr))
            
            dataBlock.setClean(plug)
        else:
            return OpenMaya.kUnknownParameter

# creator 
Example #7
Source File: instanceAlongCurve.py    From instanceAlongCurve with MIT License 4 votes vote down vote up
def getRotationForParam(self, param, axisHandlesSorted, curveForm, curveMaxParam):

        indexRange = (-1, -1)
        wrapAround = not (curveForm is OpenMaya.MFnNurbsCurve.kOpen)

        # Find the range of indices that make up this curve segment
        for i in xrange(len(axisHandlesSorted)):

            # TODO: could use a binary search
            if param < axisHandlesSorted[i][1]:

                if i > 0:
                    indexRange = (i - 1, i)
                    break
                elif wrapAround:
                    indexRange = (len(axisHandlesSorted) - 1, 0)
                    break
                else:
                    indexRange = (0, 0)
                    break

        # Edge case
        if indexRange[0] == -1 and indexRange[1] == -1 and len(axisHandlesSorted) > 0:
            if wrapAround:
                indexRange = (len(axisHandlesSorted) - 1, 0)
            else:
                indexRange = (len(axisHandlesSorted) - 1, len(axisHandlesSorted) - 1)
            
        # Now find the lerp value based on the range
        if indexRange[0] > -1 and indexRange[1] > -1:
            minParam = axisHandlesSorted[indexRange[0]][1]
            maxParam = axisHandlesSorted[indexRange[1]][1]

            minAxis = axisHandlesSorted[indexRange[0]][2]
            maxAxis = axisHandlesSorted[indexRange[1]][2]

            if(math.fabs(minParam - maxParam) > 0.001):

                if minParam > maxParam and wrapAround:

                    if param < maxParam:
                        param = param + curveMaxParam

                    maxParam = maxParam + curveMaxParam
                
                t = min(max((param - minParam) / (maxParam - minParam), 0.0), 1.0)

                return minAxis + (maxAxis - minAxis) * t

            return minAxis

        return 0.0 
Example #8
Source File: instanceAlongCurve.py    From instanceAlongCurve with MIT License 4 votes vote down vote up
def compute(self, plug, dataBlock):
        try:
            curveDataHandle = dataBlock.inputValue(instanceAlongCurveLocator.inputCurveAttr)
            curve = curveDataHandle.asNurbsCurveTransformed()

            updateTranslation = (plug == instanceAlongCurveLocator.outputTranslationAttr.compound)
            updateRotation = (plug == instanceAlongCurveLocator.outputRotationAttr.compound)
            updateScale = (plug == instanceAlongCurveLocator.outputScaleAttr.compound)

            if not curve.isNull():

                if updateTranslation or updateRotation or updateScale:
                    curveFn = OpenMaya.MFnNurbsCurve(curve)

                    instanceCount = self.getInstanceCountByMode()
                    distOffset = dataBlock.inputValue(instanceAlongCurveLocator.distOffsetAttr).asFloat()
                    curveLength = curveFn.length()

                    # Curve thresholds
                    curveStart = dataBlock.inputValue(instanceAlongCurveLocator.curveStartAttr).asFloat() * curveLength
                    curveEnd = dataBlock.inputValue(instanceAlongCurveLocator.curveEndAttr).asFloat() * curveLength

                    effectiveCurveLength = min(max(curveEnd - curveStart, 0.001), curveLength)
                    lengthIncrement = self.getIncrementByMode(instanceCount, effectiveCurveLength)

                    # Common data
                    inputTransformPlug = self.getInputTransformPlug()
                    inputTransformFn = self.getInputTransformFn()
                    
                    # Force update of transformation 
                    if OpenMaya.MPlug(self.thisMObject(), instanceAlongCurveLocator.inputTransformAttr).isConnected():
                        dataBlock.inputValue(inputTransformPlug).asMatrix()

                    # Manipulator data
                    curveAxisHandleArray = dataBlock.inputArrayValue(instanceAlongCurveLocator.curveAxisHandleAttr.compound)
                    axisHandlesSorted = getSortedCurveAxisArray(self.thisMObject(), curveAxisHandleArray, instanceCount)

                    if updateTranslation:
                        self.updateInstancePositions(curveFn, dataBlock, instanceCount, distOffset, curveStart, curveEnd, effectiveCurveLength, lengthIncrement, inputTransformPlug, inputTransformFn, axisHandlesSorted)

                    if updateRotation:
                        self.updateInstanceRotations(curveFn, dataBlock, instanceCount, distOffset, curveStart, curveEnd, effectiveCurveLength, lengthIncrement, inputTransformPlug, inputTransformFn, axisHandlesSorted)

                    if updateScale:
                        self.updateInstanceScale(curveFn, dataBlock, instanceCount, distOffset, curveStart, curveEnd, effectiveCurveLength, lengthIncrement)

        except:
            sys.stderr.write('Failed trying to compute locator. stack trace: \n')
            sys.stderr.write(traceback.format_exc())
            return OpenMaya.kUnknownParameter