Friday 24 February 2023

python way of making the bones based on cvs

from cgtalk forum poster - Darksuit

CGTalk | Auto-create joint chain along spline? (cgsociety.org)

just in case - 

changing the orientation on the joints should be easy enough look to the script for

this line

cmds.joint(currentJoint,e=True,zso=True,oj="xyz")

change the oj=“xyz” to the orientation you need… more than likely you are looking for “xzy”.

The import maya.cmds as cmds line imports the MEL script commands to python. There are some differences, but there is a lot of good documentation, and a number of references out there.

as for reorienting the joints. change the following line i=1 in the last block to i=0 that will start the orientation at the base joint instead of the second joint. A lot of times I leave the base joint aligned to world for my own uses.




 import maya.cmds as cmds


# array of selected objects

sel = cmds.ls( selection=True )


# name of the curve to convert

lastSelect = int(len(sel)) -1

curveSel = sel[lastSelect]


# print lastSelect

#print curveSel


#find the number of CVs (Degree + Spans)

numCVs = int(cmds.getAttr (curveSel+'.degree'))+ (cmds.getAttr (curveSel+'.spans'))

#print numCVs


# selection must be clear

cmds.select(clear=True)


#For each CV in the Curve create a joint.


i=0

while 1:

    if not ( i < numCVs ):

        break

    cvPosition = (cmds.getAttr (curveSel+'.cv['+(str(i))+']'))

    if (i == 0):

    cmds.joint(n=curveSel+'_jointRoot',p = cvPosition[0])

    if (i == (numCVs-1)):

    cmds.joint(n=curveSel+'_jointEnd', p = cvPosition[0])

    if(i>0)and(i != numCVs-1):

    cmds.joint(n=curveSel+'_joint_'+(str(i)),p = cvPosition[0])

    i+=1

 

# reorient the joints    

i=1

while 1:

if not (i < numCVs-1):

break

#find the name of the current Joint

currentJoint = (curveSel+'_joint_'+(str(i)))

cmds.joint(currentJoint,e=True,zso=True,oj="xyz")

i+=1