Creating an animation curve by code using cubic-bezier values

Creating an animation curve by code using cubic-bezier values

Anonymous
Not applicable
928 Views
2 Replies
Message 1 of 3

Creating an animation curve by code using cubic-bezier values

Anonymous
Not applicable

Hi all,

 

I'd love some help on this subject. I have some animation curves that i'd like to recreate in Maya. The problem is they're in a cubic bezier fashion like so...

 

http://cubic-bezier.com/#.17,.25,.67,1

 

Does anyone have any suggestions how I can covert this style of animation curve to Maya's (angle/weight) via Mel/Python?

 

Any assistance would be greatly appreciated.

 

Thanks.

 

0 Likes
929 Views
2 Replies
Replies (2)
Message 2 of 3

cheng_xi_li
Autodesk Support
Autodesk Support

Hi,

 

I think you may want to check the document of MFnAnimCurve. It explains how cubic 2d Bezier is working in Maya.

 

Yours,

Li

 

0 Likes
Message 3 of 3

Anonymous
Not applicable

This is probably too late, but for future references, here's the code I wrote for converting bezier curve to anim curve:

 

import maya.api.OpenMaya as om2
import maya.api.OpenMayaAnim as oma
import maya.cmds as mc


def bezierCrvToAnimCrv(crv):
    cvs = mc.ls(crv + '.cv[*]', fl=True)
    cv_ids = [int(x.split('[')[-1][:-1]) for x in cvs]

    animCrv = mc.createNode('animCurveTU')
    for keyIndex, i in enumerate(range(0, len(cvs), 3)):
        # create keys
        x = mc.getAttr(cvs[i] + '.xValue')
        y = mc.getAttr(cvs[i] + '.yValue')
        mc.setKeyframe(animCrv, time=x, value=y)
        mc.keyTangent(animCrv, edit=True, weightedTangents=True)
        mc.keyTangent(animCrv, e=True, itt='auto', ott='auto')

    for keyIndex, i in enumerate(range(0, len(cvs), 3)):
        # set tangents
        inWeight = 0
        outWeight = 0
        in_x, in_y = (0, 0)
        out_x, out_y = (0, 0)
        if i == 0:  # first cv
            out_x, out_y, outWeight = getWeightAndTangent(cvs[i], cvs[i + 1])
        elif i == cv_ids[-1]:  # last cv
            in_x, in_y, inWeight = getWeightAndTangent(cvs[i - 1], cvs[i])
        else:  # middle cvs
            in_x, in_y, inWeight = getWeightAndTangent(cvs[i], cvs[i - 1])
            out_x, out_y, outWeight = getWeightAndTangent(cvs[i], cvs[i + 1])

        # set tangents
        setWeightAndTangent(animCrv=animCrv, index=keyIndex,
                            x=in_x, y=in_y, weight=inWeight, isInTangent=True)
        setWeightAndTangent(animCrv=animCrv, index=keyIndex,
                            x=out_x, y=out_y, weight=outWeight, isInTangent=False)

    return animCrv


def getWeightAndTangent(pnt1, pnt2):
    pos1 = mc.xform(pnt1, q=1, ws=1, t=1)
    pos2 = mc.xform(pnt2, q=1, ws=1, t=1)
    vec = om2.MVector(pos2) - om2.MVector(pos1)

    return vec.x, vec.y, vec.length()


def setWeightAndTangent(animCrv, index, x, y, weight, isInTangent):
    sel = om2.MSelectionList()
    sel.add(animCrv)
    node = sel.getDependNode(0)
    animCrvFn = oma.MFnAnimCurve(node)
    animCrvFn.setTangent(index, x, y, isInTangent)
    animCrvFn.setWeight(index, weight, isInTangent)
0 Likes