##
##

import maya.cmds as cmds

##Create the locators for the distance mesure node for the arm and parent then appropriately.

cmds.spaceLocator(n='arm_dist_start')
cmds.spaceLocator(n='arm_dist_end')
cmds.spaceLocator(n='hand_loc')

point=cmds.pointConstraint('up_arm','arm_dist_start')
cmds.delete(point)

cmds.parent('arm_Handle','hand_loc')
cmds.parent('up_arm','arm_dist_start')
cmds.parent('arm_dist_end','hand_loc')



##Create arm_dist node

cmds.createNode('distanceDimShape')

myArmDist=cmds.rename('distanceDimension1',
                      'arm_dist')

cmds.connectAttr('arm_dist_startShape.worldPosition',
                  myArmDist+'.startPoint')

cmds.connectAttr('arm_dist_endShape.worldPosition',
                  myArmDist+'.endPoint')

##Create up_arm_to_elbow_dist node

cmds.createNode('distanceDimShape')
myDistUpToElbow=cmds.rename('distanceDimension1', 'up_arm_to_elbow_dist')

cmds.connectAttr('arm_dist_startShape.worldPosition',
                  myDistUpToElbow+'.startPoint')

cmds.connectAttr('elbowShape.worldPosition',
                  myDistUpToElbow+'.endPoint')

##Create elbow_to_hand_dist node

cmds.createNode('distanceDimShape')
myDistElbowToHand=cmds.rename('distanceDimension1',
                              'elbow_to_hand_dist')

cmds.connectAttr('elbowShape.worldPosition',
                  myDistElbowToHand+'.startPoint')

cmds.connectAttr('hand_locShape.worldPosition',
                  myDistElbowToHand+'.endPoint')

##Create variables

upArmLength  =    0.0
lowArmLength =    0.0
fullLength   =    0.0
driver       =    'arm_distShape.distance'

##Set variables

upArmLength  = cmds.getAttr('low_arm.tx')
lowArmLength = cmds.getAttr('hand.tx')
fullLength   = (upArmLength + lowArmLength)

##Create setDrivenKey commands (normal state)

cmds.setDrivenKeyframe('low_arm.tx',
                        cd=driver,
                        dv=fullLength,
                        v=upArmLength)

cmds.setDrivenKeyframe('hand.tx',
                        cd=driver,
                        dv=fullLength,
                        v=lowArmLength)

##2 times of length

cmds.setDrivenKeyframe('low_arm.tx',
                        cd=driver,
                        dv=(fullLength*2),
                        v=(upArmLength*2))

cmds.setDrivenKeyframe('hand.tx',
                        cd=driver,
                        dv=(fullLength*2),
                        v=(lowArmLength*2))

##When length is 0(Default)

cmds.setDrivenKeyframe('low_arm.tx',
                        cd=driver,
                        dv=0,
                        v=upArmLength)

cmds.setDrivenKeyframe('hand.tx',
                        cd=driver,
                        dv=0,
                        v=lowArmLength)

##Set postInfinit curves to linear

cmds.selectKey('low_arm',add=True,k=True, at='translateX')
cmds.selectKey('hand',   add=True,k=True, at='translateX')

cmds.setInfinity('low_arm', poi='linear')
cmds.setInfinity('hand',    poi='linear')

##Create the blend nodes and make the proper connection to pin the elbow

cmds.blendTwoAttr('low_arm.tx',
                   at0='low_arm_translateX.output',
                   at1='up_arm_to_elbow_dist.distance',
                   n='up_arm_choice')

cmds.blendTwoAttr('hand.tx',
                   at0='hand_translateX.output',
                   at1='elbow_to_hand_dist.distance',
                   n='low_arm_choice')

cmds.select('elbow')

cmds.addAttr( longName='Pin', attributeType='float', max=1.0, min=0.0, dv=0.0, k=True)

cmds.connectAttr('elbow.Pin','up_arm_choice.attributesBlender',f=True)
cmds.connectAttr('elbow.Pin','low_arm_choice.attributesBlender',f=True)

##
##