Python: How to select curve CV's?

This is my spline spine script so far. Can someone help me figure out how to select the curve CV’s? I am a beginner so if you could also explain what you did, I would appreciate it

import maya.cmds as mc

#find selected objects
selected = cmds.ls(sl=True)

#create empty list for new joints
jList = []

#clears the locator selection - you can put this into loop if you don't want joints to be parented to each other
mc.select(cl=True)

#for locators in selection, run the following code
for i, s in enumerate(selected):

    #create variable for the position of the locators
    pos = mc.xform(s, q=True, t=True, ws=True)

    #create joints and position them on top of locators
    j = mc.joint(radius=0.15, p=pos)

    #rename joints
    jj= mc.rename(j, "SpineIK_" + str(i))

    #append current joint to jList
    jList.append(jj)

#This line changes orientation of each joints to xzy (excluding the end joint)
mc.joint((jList), edit=True, oj="xzy", secondaryAxisOrient="xup", ch=True)

#This variable stores the last joint in the chain
Lastj = mc.ls(sl=True)

#This line zeroes out the orientation of the last joint so that it matches the orientation of the chain
mc.setAttr(Lastj[0] + ".jointOrientX", 0)
mc.setAttr(Lastj[0] + ".jointOrientY", 0)
mc.setAttr(Lastj[0] + ".jointOrientZ", 0)

#This line creates an IKSpline from first joint to the last, with 2 curve spans

IK= mc.ikHandle(sj=jList[0], ee=Lastj[0], sol= 'ikSplineSolver', ns=2)

#this line selects the curve
SpineCurve = mc.select(IK[2])

    

Well the cvs of a curve can be accessed as attributes of the curve itself. So cmds.select(“curve1.cv[0]”) selects the first cv - it’s base 0. And the number of CVs a curve has is equal to the number of spans + the degree of the curve. So if span = 2 and degree = 3, there are 5 CVs. Remember base 0 though, so you could select them all with cmds.select(“curve1.cv[0:4]”)

Also, do you mind if I ask why the script makes new joints based off of a locator position, instead of working on selected existing joints? I’m curious, since I’ve made all of my rigging tools for pre-existing skeletons.

Thank you so much man! This is exactly what I needed.

As for your question, I don’t really know… Seemed like a good idea at a time. Is there some potential issue with locators that I am not seeing? ( I am a beginner so most of the time I have no idea what I’m doing)

No issue that I’m aware of. I guess I just personally find it easier to make the full skeleton prior to fixing it with expressions and ik handles and stuff. Anyway, good luck!

Hey man.

Thought I’d ask…

How can I tell python to copy the value from curve arclength and paste it into multiply divide input2x node? (I’ve already connected arc length to input1X and now need the same value to be shown in input 2x):

Relevant script part:

#This line creates curveInfoNode and Multiply Divide Node
SCI = mc.arclen('Spine', ch=True)
mc.rename(SCI, 'SpineCurveInfo')
mc.createNode('multiplyDivide', n='Spine_MDN')
#This line connects the acrLength value to MD Node's input1x
mc.connectAttr("SpineCurveInfo.arcLength", 'Spine_MDN'+'.input1X')
mc.setAttr('Spine_MDN.operation', 2)

With setAttr’s bro, getAttr. Also, have you ruled out using mel for this thing? Just about every line is using maya.cmds. Could save yourself some typing. :cool:


mc.connectAttr("SpineCurveInfo.arcLength", 'Spine_MDN'+'.input1X')

is connecting the arclen to the input1X, you’d just make it


mc.connectAttr("SpineCurveInfo.arcLength", 'Spine_MDN'+'.input2X')

To duplicate it to 2x

Okay guys thanks to your help I almost finished the script…

As a finishing touch, how would I go about parenting the joints I generated to the original spine joints(locators)?

So far I’ve got this:

#This line parents the IKSpline Joints to the original spine joints
for s in selected:
    mc.parentConstraint(jList, s)

It’s not working for obvious reasons: It parents each original spine joint to all the SplineIK joints. How do I tell python that I want to parent Constraint original joint 1 to spineIK joint 1, joint 2 to Spine IK joint2 and so on?
In other words, how do I tell Python to do this: (jList is the SplineIK joints, and /selected/ are original spine joints)

mc.parentConstraint(jList[0], selected[0])
mc.parentConstraint(jList[1], selected[1])
mc.parentConstraint(jList[2], selected[2])    
mc.parentConstraint(jList[3], selected[3])
mc.parentConstraint(jList[4], selected[4])    
mc.parentConstraint(jList[5], selected[5])

for i in xrange(len((selected)):
    mc.parentConstraint(jList[i], selected[i])

Or

 

for joint, source in zip(jlist, selected):
     cmds.parentConstraint(joint, source)


It’s a very, very good idea to use long names (ls -l) for everything: that way you can sort them using plain old’ python list sorts and be sure you get the hierarchy you expect :slight_smile:

Thanks guys! Exactly what I needed