PDA

View Full Version : Python Locator Creation


saedAbdelhafez
04-27-2009, 10:13 AM
I wrote this script that the user can set 2 locators and enter the number of locators between them ... the locators betwen the two points should be in the same line between the main locators and the distance between each one of the locators should be equal .. i know there is lots of ways i can do this using xform and curves but i want to solvet based on the mathmatical equation between 2 points in a 3d space ,, i just wanna know why its not working !! here is the script

import maya.cmds as cmds
from math import sqrt
InputWindow = cmds.window(t="Input Range",wh=(100,100))
cmds.columnLayout()
cmds.button(l="Create the Root locator",c="Root()")
cmds.button(l="Create the Back locator",c="Back()")
cmds.text(l="Enter Number Of Joints")
intFieldK = cmds.intField( editable=True,min=0)
cmds.button(l="Evaluate",c="Evel()")
cmds.showWindow(InputWindow)
#########################################
def Root():
global rootLocator
rootLocator=cmds.spaceLocator(n="root")
cmds.move(0,0,0,rootLocator[0])
return rootLocator
def Back():
global backLocator
backLocator=cmds.spaceLocator(n="back")
cmds.move(0,7,0,backLocator[0])
return backLocator
def Evel():
##########################################
cmds.select("root")
##########################################
rootTx=cmds.getAttr(rootLocator[0]+".tx")
rootTy=cmds.getAttr(rootLocator[0]+".ty")
rootTz=cmds.getAttr(rootLocator[0]+".tz")
rootT=(rootTx,rootTy,rootTz)
##########################################
cmds.select("back")
##########################################
backTx=cmds.getAttr(backLocator[0]+".tx")
backTy=cmds.getAttr(backLocator[0]+".ty")
backTz=cmds.getAttr(backLocator[0]+".tz")
backT=(backTx,backTy,backTz)
print "backT = ",backT
###########################################
distance=sqrt(((backTx-rootTx)**2)+((backTy-rootTx)**2)+((backTz-rootTx)**2))
###########################################
global jointsNumber

jointsNumber = cmds.intField(intFieldK,q=True,v=True)

###########################################
offset=distance/jointsNumber
displacementX=0
displacementY=0
displacementZ=0


for i in range(0,jointsNumber,1):
displacementX=displacementX+offset
displacementY=displacementY+offset
displacementZ=displacementZ+offset

X=rootTx+(backTx-rootTx)/displacementX
Y=rootTy+(backTy-rootTy)/displacementY
Z=rootTz+(backTz-rootTz)/displacementZ

LoL=cmds.spaceLocator(n="backN%d"%i)

#cmds.move(X,0,0,"backN%d"%i,r=True)
#cmds.move(0,Y,0,"backN%d"%i,r=True)
#cmds.move(0,0,Z,"backN%d"%i,r=True)
cmds.setAttr(LoL[0]+".translate",X,Y,Z)

BackX = cmds.getAttr(LoL[0]+".tx")
BackY = cmds.getAttr(LoL[0]+".ty")
BackZ = cmds.getAttr(LoL[0]+".tz")

RyanT
04-30-2009, 11:43 PM
This should do the trick:

import maya.cmds as cmds
from math import sqrt
import maya.OpenMaya as OpenMaya

# Globals
rootLocator = ""
backLocator = ""

InputWindow = cmds.window(t="Input Range",wh=(100,100))
cmds.columnLayout()

cmds.button(l="Create the Root locator",c="Root()")
cmds.button(l="Create the Back locator",c="Back()")

cmds.text(l="Enter Number Of Joints")
intFieldK = cmds.intField( editable=True,min=1)

cmds.button(l="Evaluate",c="Evel()")

cmds.showWindow(InputWindow)


#########################################
def Root():
global rootLocator
rootLocator=cmds.spaceLocator(n="root")
cmds.move(0,0,0,rootLocator[0])
return rootLocator
def Back():
global backLocator
backLocator=cmds.spaceLocator(n="back")
cmds.move(0,7,0,backLocator[0])
return backLocator
def Evel():

# UI Options
jointsNumber = cmds.intField(intFieldK,q=True,v=True)
avgDist = 1.0 / (jointsNumber + 1)

# Start/End points
startPoint = cmds.xform('root', q=True, ws=True, rp=True)
endPoint = cmds.xform('back', q=True, ws=True, rp=True)

# Use maya API vector class
mVectorStart = OpenMaya.MVector(startPoint[0], startPoint[1], startPoint[2])
mVectorEnd = OpenMaya.MVector(endPoint[0], endPoint[1], endPoint[2])
mVectorResult = mVectorEnd - mVectorStart

# Create a locator per joint number
distMultiplier = avgDist
for i in range(jointsNumber):
newPoint = mVectorResult * distMultiplier
finalPoint = mVectorStart + newPoint
distMultiplier = distMultiplier + avgDist

locator=cmds.spaceLocator(n="newloc#")
cmds.move(finalPoint.x, finalPoint.y, finalPoint.z, locator[0], rpr=True)

CGTalk Moderation
04-30-2009, 11:43 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.