maya tool: get speed from animated object live

i’ve wrote 2 handy tools in maya for getting a speed value from animated Objects.  it works on everything, which have a transform node. Locators, Geo etc…

this script add an extra attribute call Speed. its updates live, even when change keyframes. its very hand if you have to animate your force field based on speed /animation of other Objects. you can simply refer this attribude inside other expression.

this second script is pretty much the same, except it display the speed live in Maya’s HUB display. (inside a viewport)

when you activate the script again it will turn off the HUD display.

link to script

link to script 2

Python script:

import maya.cmds as cmds
import math

Psel = cmds.ls( selection=True )
try:

nodeT = cmds.nodeType( Psel[0] )
print nodeT
if nodeT == ‘transform’:
checkAttr = cmds.objExists(Psel[0]+’.speed’)
print checkAttr
if checkAttr == True:
listexpr = cmds.listConnections(Psel[0]+’.speed’)
if listexpr == None :
ex1 = ‘float $lastPosX = getAttr -t (frame-1) '+Psel[0]+'.tx;’
ex2 = ‘float $lastPosY = getAttr -t (frame-1) '+Psel[0]+'.ty;’
ex3 = ‘float $lastPosZ = getAttr -t (frame-1) '+Psel[0]+'.tz;’
ex4 = Psel[0]+’.speed = abs (mag (<<‘+Psel[0]+’.translateX,’+Psel[0]+’.translateY,’+Psel[0]+’.translateZ>>)- mag (<<$lastPosX,$lastPosY,$lastPosZ>>) );’
exAll = ex1+ex2+ex3+ex4
cmds.expression( o=Psel[0], s=exAll)
else:
cmds.delete(listexpr[0])
ex1 = ‘float $lastPosX = getAttr -t (frame-1) '+Psel[0]+'.tx;’
ex2 = ‘float $lastPosY = getAttr -t (frame-1) '+Psel[0]+'.ty;’
ex3 = ‘float $lastPosZ = getAttr -t (frame-1) '+Psel[0]+'.tz;’
ex4 = Psel[0]+’.speed = abs (mag (<<‘+Psel[0]+’.translateX,’+Psel[0]+’.translateY,’+Psel[0]+’.translateZ>>)- mag (<<$lastPosX,$lastPosY,$lastPosZ>>) );’
exAll = ex1+ex2+ex3+ex4
cmds.expression( o=Psel[0], s=exAll)

else:
print “test”
cmds.addAttr( Psel[0],longName=’speed’ )
ex1 = ‘float $lastPosX = getAttr -t (frame-1) '+Psel[0]+'.tx;’
ex2 = ‘float $lastPosY = getAttr -t (frame-1) '+Psel[0]+'.ty;’
ex3 = ‘float $lastPosZ = getAttr -t (frame-1) '+Psel[0]+'.tz;’
ex4 = Psel[0]+’.speed = abs (mag (<<‘+Psel[0]+’.translateX,’+Psel[0]+’.translateY,’+Psel[0]+’.translateZ>>)- mag (<<$lastPosX,$lastPosY,$lastPosZ>>) );’
exAll = ex1+ex2+ex3+ex4
print exAll
cmds.expression( o=Psel[0], s=exAll)

else:
print ‘—> select transform !’

except TypeError :
print ‘—> select transform !’

 

 

 Script 2:


import maya.cmds as cmds
import math

def getSpeed(*args):

res = 0
Psel = cmds.ls( selection=True )
try:

nodeT = cmds.nodeType( Psel[0] )
print nodeT
if nodeT == ‘transform’:
checkAttr = cmds.objExists(Psel[0]+’.speed’)
print checkAttr
if checkAttr == True:
listexpr = cmds.listConnections(Psel[0]+’.speed’)
if listexpr == None :
ex1 = ‘float $lastPosX = getAttr -t (frame-1) '+Psel[0]+'.tx;’
ex2 = ‘float $lastPosY = getAttr -t (frame-1) '+Psel[0]+'.ty;’
ex3 = ‘float $lastPosZ = getAttr -t (frame-1) '+Psel[0]+'.tz;’
ex4 = Psel[0]+’.speed = abs (mag (<<‘+Psel[0]+’.translateXA,’+Psel[0]+’.translateY,’+Psel[0]+’.translateZ>>)- mag (<<$lastPosX,$lastPosY,$lastPosZ>>) );’
exAll = ex1+ex2+ex3+ex4
cmds.expression( o=Psel[0], s=exAll)
else:
cmds.delete(listexpr[0])
ex1 = ‘float $lastPosX = getAttr -t (frame-1) '+Psel[0]+'.tx;’
ex2 = ‘float $lastPosY = getAttr -t (frame-1) '+Psel[0]+'.ty;’
ex3 = ‘float $lastPosZ = getAttr -t (frame-1) '+Psel[0]+'.tz;’
ex4 = Psel[0]+’.speed = abs (mag (<<‘+Psel[0]+’.translateX,’+Psel[0]+’.translateY,’+Psel[0]+’.translateZ>>)- mag (<<$lastPosX,$lastPosY,$lastPosZ>>) );’
exAll = ex1+ex2+ex3+ex4
cmds.expression( o=Psel[0], s=exAll)

else:
print “test”
cmds.addAttr( Psel[0],longName=’speed’ )
ex1 = ‘float $lastPosX = getAttr -t (frame-1) '+Psel[0]+'.tx;’
ex2 = ‘float $lastPosY = getAttr -t (frame-1) '+Psel[0]+'.ty;’
ex3 = ‘float $lastPosZ = getAttr -t (frame-1) '+Psel[0]+'.tz;’
ex4 = Psel[0]+’.speed = abs (mag (<<‘+Psel[0]+’.translateX,’+Psel[0]+’.translateY,’+Psel[0]+’.translateZ>>)- mag (<<$lastPosX,$lastPosY,$lastPosZ>>) );’
exAll = ex1+ex2+ex3+ex4
print exAll
cmds.expression( o=Psel[0], s=exAll)

else:
print ‘—> select transform !’

except TypeError :
print ‘—> select transform !’
res = cmds.getAttr(Psel[0]+’.speed’)
return res
try:
cmds.headsUpDisplay( ‘voxelsHUD’, section=1, block=0, blockSize=’small’, label=’Speed’, command=getSpeed, event=’timeChanged’, nodeChanges=’attributeChange’ )
except RuntimeError:
cmds.headsUpDisplay(‘voxelsHUD’, remove=True)

 

Advertisements

2 thoughts on “maya tool: get speed from animated object live

  1. Thank you!
    But I am getting an error:
    “# Error: IndexError: file line 10: list index out of range”

    Any solutions?

Leave a Reply

Fill in your details below or click an icon to log in:

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