Stretchy spine

Python script for making things like stretchy spines etc. Create a cv curve, select it and run the script and specify the number of joints you want. Voila.

##
## ChainToIK
## -------------
## 14 sep 2009
## Jan-Erik "Jonne" Östman (jonne@jonne.net)
##
## www.jonne.net
##
## Description:
## ------------
## PYTHON script for creating things that require joints that stretch. 
## For instance a stretchy spine.
##
##
## Instructions:
## -------------
## Create a cv curve, select it and run the script. Simple as that. After creation 
## select cv points on the curve and drag them to see the effect
##
## -------------------------------------------------------------------------



import maya.cmds as mc


##--------------------------------------
## Creates number of specified joints
## along the selected curve
##--------------------------------------
def gb_CurveToJointChain():

	## Define starting values
	numJnts = 10


	## Promptdialogue
	result = mc.promptDialog(
				title = 'Number of joints',
				message = 'Enter numer:',
				button = ['OK', 'Cancel'],
				defaultButton = 'OK',
				cancelButton = 'Cancel',
				dismissString = 'Cancel')

	if result == 'OK':
		text = mc.promptDialog(query=True, text=True)
		numJnts = int(text)

		#errorchecking to aviod conversion error
		try:
			numJnts = float(text)
			numJnts = int(text)
		except ValueError:
			numJnts = 10
			print "Using default value of 10"


	sel = mc.ls(sl=1)
	mc.select(cl=1)

	## Errorchecking
	if len(sel) > 0:
		crv = sel[0]
		cont = 1
	else:
		print "nothing selected"
		cont = 0

	# Create an empty list
	returnlist = ""
	returnList =  []

	if cont == 1:

		returnList.append(crv)
		maxVal = mc.getAttr(crv + ".maxValue")
		offset = maxVal/numJnts

		#startvalue, range to loop through, increment/modifier
		for i in range(0,(numJnts+1),1):
			uOff = i * offset
			pntOnCrv = mc.pointOnCurve(crv, pr=uOff, p=1)
			jnt = mc.joint(p=(pntOnCrv[0],pntOnCrv[1],pntOnCrv[2]))

			# add to list
			returnList.append(jnt)

	return returnList




##--------------------------------------
## Makes created joints into a stretchy
## spine system
##--------------------------------------
def gb_ChainToIK():

	## assign previous definition to a variable
	curveInput = gb_CurveToJointChain()
	crv = curveInput[0]

	# Create empty list for joints
	joints = []

	# loop through created joints and add them to the joints list
	for i in range(1, len(curveInput),1):
		joints.append(curveInput[i])

	# Get value for how much to offset each joint
	numJnts = (len(joints)-1)
	maxVal = mc.getAttr(crv + ".maxValue")
	offset = maxVal/numJnts

	# Loop through the joints.
	# Create a pointOnCurve node for each joint and connect that to
	# the position on the curve so that the curve controls the jointposition
	j = 0
	for jnt in joints:
		param = offset * j
		node = mc.createNode("pointOnCurveInfo", n=jnt + "crvInfo")
		loc = mc.spaceLocator(n=jnt + "NUL")
		mc.connectAttr(crv + ".local", node + ".inputCurve", f=1)
		mc.setAttr(node + ".parameter", param)
		mc.connectAttr((node + ".position"), (loc[0] + ".translate"))

		#Position the joints in local- instead of worldspace
		mc.pointConstraint(loc[0], jnt)

		j += 1

	# Joint orientation
	mc.ikHandle(ee=curveInput[j], sj=curveInput[1], ccv=0, c=crv, sol="ikSplineSolver")

## Execute
gb_ChainToIK()
I wish to do something Great and Wonderful, but I must start by doing the little things like they were Great and Wonderful”
~ Albert Einstein