def create_neck ():

    if mc.objExists( Controllers.HEAD ):
        print 'HEAD ALREADY EXISTS'
        return

    if mc.objExists ( Controllers.NECK ):
        print 'NECK ROOT DRV exists'

    else:
        create_spine ()
    
    distance_x = mc.getAttr ( SpineJoint.HEAD + '.translateX' ) / 4

    _, __, ___, ik_crv, neck_group = rd.create_ikspline( 5, distance_x, 'neck', SpineJoint.NECK, Controllers.GLOBAL )

    mc.parent ( neck_group, Controllers.GLOBAL )

    # Create head
    mc.select ( clear = True )
    head_drv = mc.joint ( name = 'head_DRV' )
    rd.get_position ( head_drv, SpineJoint.HEAD, 0, 1, 1 )

    head_ctl = rd.create_ctrl( 'circle', Controllers.HEAD, 10, Colors.BLUE, option_attr = True, obj_pos = head_drv )

    orient_gp = rd.parent_group( head_ctl, Controllers.HEAD + '_orient_GRP' )
    head_gp = rd.parent_group ( orient_gp, head_ctl + '_GRP' )
    mc.parent ( head_gp, Controllers.GLOBAL )
    mc.parent ( head_drv, head_ctl )

    orient_loc = mc.spaceLocator( name = 'head_orient_pos_LOC' )
    rd.get_position ( orient_loc, 'root_JNT', position = False )

    mc.parent ( orient_loc, Controllers.GLOBAL )

    mc.pointConstraint ( 'neck_CTL_pos_LOC', head_gp )
    mc.orientConstraint ( 'neck_CTL', 'chest_CTL', orient_loc, orient_gp )

    # Skin spline curve
    mc.skinCluster ( DriveJoints.NECK, head_drv, ik_crv, toSelectedBones = True )[0]
def create_spine ():

    # Check if the spine rig already exists
    if mc.objExists( Controllers.ROOT ):
        print 'SPINE ALREADY exists'
        return

    # Check if the base elements exists
    if mc.objExists( Controllers.GLOBAL ):
        print "MAIN ROL exists"

    else:
        create_general()
    
    # Get distance from root to chest
    distance_x = mc.getAttr ( SpineJoint.CHEST + '.translateX' ) / 6

    # Create IK skeleton
    # 7 bones from root to chest
    _, drv_bones, ik_handle, ik_crv, spine_group = rd.create_ikspline( 7, distance_x, 'spine', SpineJoint.ROOT, Controllers.GLOBAL )
    
    # Create root_LOC
    root_loc = mc.spaceLocator( name = 'root_LOC' )[0]
    rd.get_position( root_loc, SpineJoint.ROOT )
    root_group = rd.parent_group( root_loc, root_loc.replace ( 'LOC', 'LOC_GRP' ) )
    mc.parent ( root_group, Controllers.GLOBAL )
    
    # Create root controller
    root_ctl = rd.create_ctrl ( 'cube', Controllers.ROOT, size = 20, color = Colors.BLUE, option_attr = True, obj_pos = SpineJoint.ROOT )
    rd.add_attr( root_ctl, 'Auto_center_pivot', attr_type = 'bool' )

    auto_center_gp = rd.parent_group ( root_ctl, root_ctl + '_auto_center_GRP' )
    root_ctl_gp = rd.parent_group ( auto_center_gp, root_ctl + '_GRP' )
    mc.parent ( root_ctl_gp, Controllers.GLOBAL )
    mc.parent ( spine_group, root_ctl_gp )
    
    # Create hip controller and DRV joint
    hip_ctl = rd.create_ctrl ( 'circle', Controllers.HIP, size = 10, color = Colors.BLUE, obj_pos = SpineJoint.ROOT )

    hip_drv = mc.rename ( mc.duplicate ( drv_bones[0] )[0], 'hip_DRV' )
    rd.get_position ( hip_drv, SpineJoint.ROOT )
    mc.parent ( hip_ctl, root_ctl )
    mc.parent ( hip_drv, hip_ctl )
    
    mdn = mc.createNode ( 'multiplyDivide', name = 'root_auto_center_on_off')
    mc.connectAttr ( mdn + '.outputX', auto_center_gp + '.translateX' )
    mc.connectAttr ( mdn + '.outputY', auto_center_gp + '.translateY' )
    mc.connectAttr ( mdn + '.outputZ', auto_center_gp + '.translateZ' )
    
    # Create spine controllers
    fk_spine_01_ctl = rd.create_ctrl ( 'circle', Controllers.SPINE_FK.replace ( 'CTL', '01_CTL' ), size = 10, color = Colors.BLUE, obj_pos = drv_bones[1] )

    fk_spine_01_gp = rd.parent_group ( fk_spine_01_ctl, fk_spine_01_ctl + '_GRP' )

    fk_spine_02_ctl = rd.create_ctrl ( 'circle', Controllers.SPINE_FK.replace ( 'CTL', '02_CTL' ), size = 10, color = Colors.BLUE, obj_pos = drv_bones[3] )

    fk_spine_02_gp = rd.parent_group ( fk_spine_02_ctl, fk_spine_02_ctl + '_GRP' )
    mc.parent ( fk_spine_02_gp, fk_spine_01_ctl )
    mc.parent ( fk_spine_01_gp, root_ctl )
    
    # Create chest controller
    chest_ctl = rd.create_ctrl ( 'cube', Controllers.CHEST, size = 20, color = Colors.BLUE, option_attr = True, obj_pos = SpineJoint.CHEST )
    chest_gp = rd.parent_group ( chest_ctl, chest_ctl + '_GRP' )

    chest_drv = mc.duplicate ( drv_bones[0] )[0]
    chest_drv = mc.rename ( chest_drv, 'chest_DRV' )
    DriveJoints.CHEST = chest_drv
    rd.get_position ( chest_drv, SpineJoint.CHEST )

    mc.parent ( chest_drv, chest_ctl )
    mc.parent ( chest_gp, fk_spine_02_ctl )
    
    chest_loc = mc.spaceLocator( name = chest_ctl.replace ( 'CTL', 'pos_CTL_LOC' ))
    rd.get_position( chest_loc, SpineJoint.CHEST )
    mc.parent ( chest_loc, chest_ctl )
    
    # Skin spline curve
    mc.skinCluster ( chest_drv, hip_drv, ik_crv, toSelectedBones = True )[0]

    # Twist system
    mc.setAttr ( ik_handle + '.dTwistControlEnable', 1 )
    mc.setAttr ( ik_handle + '.dWorldUpType', 4 )
    mc.connectAttr ( hip_ctl + '.worldMatrix[0]', ik_handle + '.dWorldUpMatrix' )
    mc.connectAttr ( chest_ctl + '.worldMatrix[0]', ik_handle + '.dWorldUpMatrixEnd' )
    
    # Create neck_root DRV
    neck_root_drv = mc.duplicate ( drv_bones[0] )[0]
    neck_root_drv = mc.rename ( neck_root_drv, 'neck_root_DRV' )
    DriveJoints.NECK = neck_root_drv
    rd.get_position ( neck_root_drv, SpineJoint.NECK )
    mc.parent ( neck_root_drv, chest_ctl )

    neck_ctl = rd.create_ctrl( 'circle', Controllers.NECK, 5, Colors.BLUE, obj_pos = neck_root_drv )
    neck_gp = rd.parent_group( neck_ctl, neck_ctl + '_GRP' )

    mc.parent ( neck_gp, chest_ctl )

    p_con = mc.pointConstraint ( SpineJoint.HEAD, SpineJoint.NECK, neck_gp )
    mc.delete ( p_con )

    neck_loc = mc.spaceLocator( name = neck_ctl + '_pos_LOC' )
    rd.get_position( neck_loc, SpineJoint.HEAD )
    mc.parent ( neck_loc, neck_ctl )