Components To Joints Script


Today I wrote a quick script in preparation for my facial rig I’m starting. It creates joints at the selected components, whether they’re faces, edges, or verts. It will create a joint at the center of each edge or face if a face is selected. If verts are selected, it creates joints at those vertex positions.

It’s pretty straightforward, and would be way easier to do if component selection was more consistent in the way it returned the selection. The problem is, when you select sequential components, it will return them as something like “pCube1.vtx[1:4]” but if you select non-sequential components, it will return them in a list like “[pCube1.vtx[1], pCube1.vtx[3]]”

Obviously, the second version would be way more preferable for iteration as I could just make a joint at the world position of each of the objects in the selection… But the first version requires some annoying string formatting and such… You’ll see it in the script.

import maya.cmds as cmds

def componentToJoint():
    if cmds.objectType([0]) != "mesh":
    # return the selection as a list
    selList = getSelection()
    print selList
    componentType = selList[0][selList[0].index(".") + 1:selList[0].index("[")]
    componentCenters = []
    # if you selected a face or edge, make our joints at those component's centers
    if componentType == "f" or componentType == "e":
        for c in selList:
            p = cmds.xform(c, q=1, t=1, ws=1)
            # find the average of all our x,y,z points. That's our center
            componentCenters.append([sum(p[0::3]) / len(p[0::3]),
                                     sum(p[1::3]) / len(p[1::3]),
                                     sum(p[2::3]) / len(p[2::3])])
            for loc in componentCenters:
                cmds.joint(n="joint#", p=loc, rad=.25)

    # else make a joint at the location of each vertex
        for c in selList:
            # make a joint at the position of each selected vertex
            cmds.joint(n="joint#", p=cmds.pointPosition(c), rad=.25)

def getSelection():
    components =
    selList = []
    objName = components[0][0:components[0].index(".")]
    for c in components:
        if ":" not in c:
            print c
            startComponent = int(c[c.index("[") + 1: c.index(":")])
            endComponent = int(c[c.index(":") + 1:c.index("]")])
            componentType = c[c.index(".") + 1:c.index("[")]
            while startComponent <= endComponent:
                selList.append(objName + "." + componentType + "[" + str(startComponent) + "]")
                startComponent += 1

    return selList


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s