import maya.cmds as mc
'''
Motion Trail to Curve
Instructions: select animated object(s), and run script…
'''
#Find selected transform nodes
sel = mc.ls(sl=True, type='transform')
#Get playback start time
start = mc.playbackOptions(q=True, min=True)
#Get playback end time
end = mc.playbackOptions(q=True, max=True)
#For Loop - iterate on each selected object
for each in sel:
#Create geometry - used later for a snapshot trail
geo = mc.polyCube()[0]
#Point Constrain the geometry to the selected object
mc.pointConstraint(each, geo)
#Select the geo we created and create an animated snapshot
mc.select(geo, r=True)
#Create variable "snap_node" that stores the snapshot group
snap_node = mc.snapshot(n=each + '_snap', i=1, st=start, et=end, u='animCurve')
#Find parented objects stored in snapshot group
trail_geo = mc.listRelatives(snap_node[0], c=True)
#Get the amount of objects parented to snapshot group
size = len(trail_geo)
#Create Curve
cv_node = mc.curve(d=1, p=[(0, 0, 0), (0, 0, 1)])
#Rebuild the curve to create the new curve we will align to our motion trail
cv = mc.rebuildCurve(cv_node, ch=False, rpo=True, rt=0, end=0, s=size-1, d=1)
#Select all points on the rebuilt curve
mc.select((cv[0] + '.cv[0:*]'), r=True)
#Store component selection in variable "pts"
pts = mc.ls(sl=True, fl=True)
#For Loop - iterate using the amount of snapshat geo stored in snapshot group
for i in range(size):
#Select each snapshot geo
mc.select(trail_geo[i], r=True)
#Connect a cluster to each snapshot mesh
cl = mc.cluster()
#Rename the snapshot clusters
mc.rename(cl[1], (each + '_' + str(i) + '_cl'))
#Clear selection
mc.select(cl=True)
#For Loop - iterate using the amount of snapshat geo stored in snapshot group
for i in range(size):
#Create variable for snapshot clusters
cl_geo = each + '_' + str(i) + '_cl'
#Select each curve point and create a cluster
mc.select(pts[i], r=True)
cl_pt_node = mc.cluster()
#Rename curve point clusters
cl_pt = mc.rename(cl_pt_node[1], (each + '_' + str(i) + '_pt_cl'))
#Now, align curve clusters to snapshot clusters!
mc.delete(mc.pointConstraint(cl_geo, cl_pt))
#Remove initial geometry used for snapshot, and remove snapshot group
mc.delete(geo, snap_node[0])
#Remove history on curve to bake the cluster deformations
mc.delete(cv, ch=True)
#Finally, make the curve smooth
mc.rebuildCurve(cv, ch=False, rpo=True, rt=0, end=0, kcp=True, kep=True, d=3)