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, pCube1.vtx]”
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(cmds.ls(sl=1)) != "mesh": return # return the selection as a list selList = getSelection() print selList componentType = selList[selList.index(".") + 1:selList.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.select(cl=1) cmds.joint(n="joint#", p=loc, rad=.25) # else make a joint at the location of each vertex else: for c in selList: cmds.select(cl=1) # make a joint at the position of each selected vertex cmds.joint(n="joint#", p=cmds.pointPosition(c), rad=.25) cmds.select(cl=1) def getSelection(): components = cmds.ls(sl=1) selList =  objName = components[0:components.index(".")] for c in components: if ":" not in c: selList.append(c) else: 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