Transforming Animation

A situation arose where an artist had animated an object in Maya. The object had keyframes applied directly to it, rather than being a skinned object. The problem was that the object and it’s animations were created at the wrong scale, and so when the animation was exported to the game engine, the effects of the animation were tiny. Increasing the scale of the animation in game would yield the corrected translation and scaling results, but of course the rotations went nuts. A second problem was that the animation had been carried out in the wrong axis. With the game being a 2 dimensional game, the sprite needed to be rotated -90° about the y-axis. Here is a brute force method for fixing animations that need transformations.

Fix The Base Transformations

  1. Create a locator, most likely one that is set at the origin, unless your animated asset is in a really strange place in the scene!
  2. Parent the animated object to the locator
  3. Translate, rotate and scale the locator so that the animated object has the correct orientation, position and scaling.

Create A New Animation Object

Create a duplicate of the object that was animated, matching the size of the now corrected original. Ensure that the scaling is set to [1,1,1]. Move the object to match the position and rotation of the original object.

Because I was working with flat sprites, I used a basic ‘get_dimensions’ script so that I could get the approximate size of the corrected object to use to create the new animation object:

def get_dimensions(x_form):
    bb = cmds.exactWorldBoundingBox(x_form)
    x = bb[3] - bb[0]
    y = bb[4] - bb[1]
    z = bb[5] - bb[2]
    return [x, y, z]

Create Animation Constraints

Next, you need to constrain the new animation object to the original object.

  • Select the original object, then the animation object
  • Click Constrain > Parent Options
  • Use the following settings:

  • Now create a scale constraint by selecting the original object and animation object again.
  • Click Constrain > Scale Options

If you play the animation on the timeline, the new animation object should follow the original object

Bake The Keyframes

  1. Set the timeline ranges to match the range of the keyframes.
  2. Click Edit > Keys > Bake Animation Options
  3. Bake with the following options.

 

You can delete the constraint nodes on the animation object. The keyframes should now be transferred to the animation object.

As mentioned, this is a brute force method. There is certainly scope for developing a tool that will perform relative transformations directly onto the keyframes using matrix maths. This would have the advantage of not creating all the extra in-between keyframes, and remove the need for creating a proxy animation object.

Addendum

The following script was found on cgsociety.org for transferring keyframes from objects that have been subgrouped. This could be a starting point for further work.

global proc bh_ungroupKeepScaledAnim()
{
string $sel[]=`ls -sl`;

for ($each in $sel)
{
string $parents[]= `listRelatives -p ($each)`;
float $parentScale=`getAttr ($parents[0]+".scaleX")`;
float $currentScale=`getAttr ($each+".scaleX")`;
float $newScale=$currentScale*$parentScale;
parent -w $each; //ungroup the object(s)

select -r $each;
// scale the translate keys
selectKey -add -k ($each+".translateX") ;
selectKey -add -k ($each+".translateY") ;
selectKey -add -k ($each+".translateZ") ;
scaleKey -scaleSpecifiedKeys 1 -timeScale 1 -timePivot 0 -floatScale 1 -floatPivot 0 -valueScale $parentScale -valuePivot 0;

// was the scale on the object keyframed?
string $scaleXKeys[]=`findKeyframe -curve -at "scaleX" $each`;
// no..so just change the object scale 
if (`size $scaleXKeys` ==0) {
setAttr ($each+".scaleX") $newScale ;
setAttr ($each+".scaleY") $newScale ;
setAttr ($each+".scaleZ") $newScale ;
}
else 
// yes.. so scale those keys too..
{ select -r $each;
selectKey -add -k ($each+".scaleX") ;
selectKey -add -k ($each+".scaleY") ;
selectKey -add -k ($each+".scaleZ") ;
scaleKey -scaleSpecifiedKeys 1 -timeScale 1 -timePivot 0 -floatScale 1 -floatPivot 0 -valueScale $parentScale -valuePivot 0; 
}

}
}
bh_ungroupKeepScaledAnim;
Advertisements

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 )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s