Search code examples
pythonmaya

How can I create a Maya Nurbs curve to match a joint chain?


I have a joint chain that twists and turns -- all rotation axes are used. I'm trying to create a Nurbs curve from this that matches the path of the joints. Ideally I could specify the density of the CVs and the degree of the curve as well. Not having much luck with curve creation options.

Any help would be appreciated! I'm working mainly in Python.


Solution

  • The cheap solution is to loop through the bones, getting their positions, then create a degree-1 (linear) curve from them.

    def joint_positions(j):
        pos = [cmds.xform(j,q=True, t=True, ws=True)]
        children = cmds.listRelatives(j, c=True) or []
        for child in children:
            pos.extend(joint_positions(child))
        return pos
    joints = joint_positions('joint1') 
    cmds.curve(d = 1, p=joints)
    

    That gives you a curve with one knot at every joint. You can then use some combination of the fitBSpline command to resample it into a smoother form, and/or the rebuild command to sample it more regularly. You can experiment with those interactively using the UI to find good values for your application then bake them into a script when you know what you're looking for. There's not a single, perfect, mathematically pure answer in degrees higher than 1 so you're basically eyeballing what looks best for your application.