Script Bug - Position Drifting


#1

I am writing a Python script that bakes the position of an object (student object) to the position of an animated object (teacher object) object using ‘Snap3PointsTo3Points’ to eventually be used to help convert vertex-animations to object based keyframes.

The ‘Teacher’ object is animated and the ‘Student’ object is snapped to the position of the teacher and keyframed on every frame to “bake” the animation. This seems to work perfectly on every individual frame, but when played back the student object seems to drift from its keyed position (even though the Channel Box values are the same as when it was baked).

I have created a dumbed-down example using an excerpt of the script to illustrate the issue. This has really had me stumped the last day or so so any insight is really appreciated.

Screen Recording

# After executing this script, the student object should play the exact same animation
# as the teacher object with a keyframe on every frame.
# This seems to work every frame, but if applied over multiple frames there is drift.

import maya.cmds as cmds

# move forward one frame
cmds.currentTime( cmds.currentTime( query=True )+1, edit=True )

# clear selection
cmds.select( clear=True )
# select 3 verticies from student object (object that will be repositioned)
cmds.select( 'object_student' + '.vtx[1]', add=False )
cmds.select( 'object_student' + '.vtx[2]', add=True )
cmds.select( 'object_student' + '.vtx[3]', add=True )
# select 3 corresponding verticies from teacher object (object with the desired position)
cmds.select( 'object_teacher' + '.vtx[1]', add=True )
cmds.select( 'object_teacher' + '.vtx[2]', add=True )
cmds.select( 'object_teacher' + '.vtx[3]', add=True )
# snap allign - 3 points to 3 points
cmds.Snap3PointsTo3Points( moveParent=0 )
            
# key position, rotation, and scale of student object
cmds.setKeyframe( 'object_student' )

cmds.select ( 'object_student', add=False )