#######pshipkov's
import maya.cmds as mc
import maya.OpenMaya as om
def lookAt(base, target, up=[0,-1,0]):
# check if the "base" object has parent
parent = mc.listRelatives(base, pa=True, p=True)
if parent != None: parent = parent[0]
base = mc.getAttr(base+".worldMatrix")
target= mc.getAttr(target+".worldMatrix")
# build transformation matrix
z = om.MVector(target[12]-base[12], target[13]-base[13], target[14]-base[14])
z.normalize()
x = z ^ om.MVector(-up[0], -up[1], -up[2])
x.normalize()
y = x ^ z
y.normalize()
m = om.MMatrix()
l = [x.x, x.y, x.z, 0, y.x, y.y, y.z, 0, z.x, z.y, z.z, 0, 0, 0, 0, 1]
om.MScriptUtil.createMatrixFromList(l, m)
if parent:
# transform the matrix in the local space of the parent object
m2 = om.MMatrix()
l = mc.getAttr(parent+".worldMatrix")
om.MScriptUtil.createMatrixFromList(l, m2)
m *= m2.inverse()
# retrieve the desired rotation for "base" to aim at "target", in degrees
r = om.MTransformationMatrix(m).eulerRotation() * 57.2958
return r[0], r[1], r[2]
#####
def aimFuturPosition(inNode, offset):
offsetTime=offset
tmpLoc = mc.spaceLocator(n=inNode+'_tmpLocator')
mc.currentTime((mc.currentTime(query=True))+offsetTime)
tmpLoc_translate = mc.xform( inNode, query=True, t=True, worldSpace=True)
mc.xform( tmpLoc[0], worldSpace=True, t=(tmpLoc_translate))
mc.currentTime((mc.currentTime(query=True))-offsetTime)
matrixRotation=lookAt( tmpLoc[0], inNode)
print matrixRotation
mc.xform(inNode, worldSpace=True, ro=(matrixRotation))
mc.select(inNode)
mc.filterCurve (inNode)
mc.delete(tmpLoc)
mc.currentTime ((mc.findKeyframe(timeSlider=True, which='next')),edit=True)
inNode=mc.ls(sl=True)
aimFuturPosition(inNode[0], 2)