This has been done a million times, but the versions I've seen have been done using mel and rather slow.
This one uses python and isloate's the selected object(s) to speed up the curve creation.
import maya.cmds as mc
def curveFromXform():
obj = mc.ls(sl=1)[0]
#try to get current modeling pane if possible to isloate select and speed xform aquisition
currentPanel = mc.getPanel(withFocus=1)
panelType = mc.getPanel(to=currentPanel)
visPanels = mc.getPanel(vis=1)
for each in visPanels:
if "modelPanel" in each and currentPanel != each:
print "Switching focus to " + each
mc.setFocus(each)
currentPanel = each
if len(mc.ls(sl=1)) > 0:
mc.isolateSelect(currentPanel,state=1)
elif "modelPanel" in each and currentPanel == each:
print "isolate panel " + each
if len(mc.ls(sl=1)) > 0:
mc.isolateSelect(currentPanel,state=1)
if mc.nodeType(obj) == "transform":
#get start and end frames from playback
start = mc.playbackOptions(q=1,min=1)
end = mc.playbackOptions(q=1,max=1)
curvePoints = []
#query pivot per frame and store in list
for i in range(start,end):
mc.currentTime(i)
pos = mc.xform(obj,q=1,ws=1,t=1)
curvePoints.append(pos)
#create curve from point data
mc.curve(n=obj + "_bakedPivot",p=curvePoints )
mc.isolateSelect(currentPanel,state=0)
mc.select(obj,r=1)