Locators on a Curve


import maya.cmds as cmds

#################
### Variables ###
#################

##Get the number of locators from user
requestedLocators = cmds.promptDialog(
                title='Locators on a Curve',
                message='Enter Number of Locators:',
                button=['OK', 'Cancel'],
                defaultButton='OK',
                cancelButton='Cancel',
                dismissString='Cancel')

if requestedLocators == 'OK':
    result = cmds.promptDialog(query=1, text=1)

selected = cmds.ls(sl=1)
print (selected)
curveName = cmds.ls (sl=1)

#################
### Functions ###
#################

##extract one curve from the selection

def extractNurbsCurves():
  curveShapeName= (cmds.ls(dag=1, sl=1, type="nurbsCurve"))
  cmds.select (curveShapeName[0])
  cmds.pickWalk (d="up")
  curveName = cmds.ls (sl=1)

def curveCheck():
  if len(selected)==0:
    pickCurveWindow()
  else:
    curveShapeName= (cmds.ls(dag=1, sl=1, type="nurbsCurve"))
    cmds.select (curveShapeName)
    cmds.pickWalk (d="up")
    curveName = cmds.ls (sl=1)
    print ("Placing Locators on "+curveName[0])
    upVector()

def pickCurveWindow():
  print("*window1*")
  window = cmds.window( tlb=1, title="Locators on a Curve", widthHeight=(200, 200))
  cmds.columnLayout (adjustableColumn=1)
  cmds.button( label='Select curve and press me', command=("buttonPush1()") )
  cmds.button( label='Close', command=('cmds.deleteUI(\"' + window + '\", window=True)') )
  cmds.showWindow( window )

def buttonPush1():
  print("*button1Push*")
  curveShapeName= (cmds.ls(dag=1, sl=1, type="nurbsCurve"))
  cmds.select (curveShapeName)
  cmds.pickWalk (d="up")
  curveName = cmds.ls (sl=1)
  print ("Placing Locators on "+curveName[0])
  upVector()
#placeLocators()
#except IndexError:
#print ("*Try harder*")


#Creates the UpVector Locator
def upVector():
  curveName= cmds.ls(sl=1)
  #Get the curve's middle control vertex's position
  curveX = cmds.getAttr (curveName[0]+".translateX")
  curveY = cmds.getAttr (curveName[0]+".translateY")
  curveZ = cmds.getAttr (curveName[0]+".translateZ")
  
  curveShapeName= (cmds.ls(dag=1, sl=1, type="nurbsCurve"))
  cmds.select (curveShapeName[0])
  #number of Spans on curve
  curveSpans=cmds.getAttr(curveShapeName[0]+".spans")
  middleControlVertex= (int((curveSpans)/2+1))
  print("spans: "+str(curveSpans))
  print ("Target Control Vertex: "+str(middleControlVertex))
  
  pointX = cmds.getAttr (curveShapeName[0]+".controlPoints["+str(middleControlVertex)+"].xValue")
  pointY = cmds.getAttr (curveShapeName[0]+".controlPoints["+str(middleControlVertex)+"].yValue")
  pointZ = cmds.getAttr (curveShapeName[0]+".controlPoints["+str(middleControlVertex)+"].zValue")
  
  totalX=(curveX+pointX)
  totalY=(curveY+pointY)
  totalZ=(curveZ+pointZ)
  
  print(curveX,curveY,curveZ)
  print(pointX,pointY,pointZ)
  print("total:")
  print (totalX,totalY,totalZ)
  
  #Make the UpVector locator and move it to the curve's middle vertex
  cmds.spaceLocator (name=(curveName[0]+"_UpVector"), position=(0,0,0))
  cmds.move (totalX, totalY, totalZ, (curveName[0]+"_UpVector"))
  cmds.color ((curveName[0]+"_UpVector"), ud=7)
  cmds.setAttr ((curveName[0]+"_UpVector")+".localScaleX", 1.5)
  cmds.setAttr ((curveName[0]+"_UpVector")+".localScaleY", 1.5)
  cmds.setAttr ((curveName[0]+"_UpVector")+".localScaleZ", 1.5)
  cmds.select (curveName[0])
  placeLocators()

def placeLocators():
  curveName = cmds.ls (sl=1)
  for i in xrange (int(result)):
    ##create Locators
    cmds.spaceLocator (name=(curveName[0]+"_Locator_"+str(i+1)),p=(0, 0, 0))
    locator = (str(curveName[0]+"_Locator_"+str(i+1)))
    print (" ")
    print (locator)
    #upVector = cmds.ls(dag=1, sl=1, type="nurbsCurve")
    cmds.pathAnimation (locator, c=(str(curveName[0])),name=(str(locator)+"_Path"), fractionMode=1, startU=0, endU=1, follow=1, followAxis="x", upAxis="y", worldUpType="object", worldUpObject=(curveName[0]+"_UpVector"))
    currentPath = ((locator[0])+"_Path")

    ##Break default connections by listing connections, then breaking said connections
    uValueConnections = (cmds.listConnections (curveName[0]+"_Locator_"+(str(i+1))+"_Path.uValue"))
    print ("Break connection: "+uValueConnections[0])
    cmds.disconnectAttr (uValueConnections[0]+".output", curveName[0]+"_Locator_"+(str(i+1))+"_Path.uValue")
  
    ##Move locator approriately along curve.
    print ("Placing at: "+str(i)+"/"+str(int(result)-1))
    cmds.setAttr (curveName[0]+"_Locator_"+(str(i+1))+"_Path.uValue", (i/(float(result)-1)))

      
curveCheck()

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s