MEL: Copy Animation with an offset

Written by Chris

Maya 2016/2018

This script allows you to copy an animation, and paste to any other or multiple objects. You also have a choice to add an offset to the animation and even add some randomization.

How it works

  • select one animated object
  • run the script
  • add desired offset or randomization
  • deselect the animated object
  • select one of multiple objects
  • paste the animation
/*
author Christian Pulla
TRANSFER ANIMATION TOOL WITH RANDOM FEATURE
============================================================
how this works to copy animation keys
    select a animated object
    run script
    select all other objects that will be the target
    deselect the source object
        option - offset
        option -randomization value (0 means no randomization)
    "Paste animation"
============================================================
*/


//make sure something is selected
string $selection[] =`ls -sl`;

if( size($selection) != 1 )
{
    //add warning that tells user to select something
    warning "You must select one animated source object";
}
else
{
    //copy all keys of the selected object
    copyKey -time ":";

    //close old window with same name
    if (`window -ex win_pasteTime`)
    {
      //win_pasteTime is the name of the window
      deleteUI -window win_pasteTime;  
    }
    
    //create the window title, width, backgorund color
    window -t "Transfer Animation Tool" -wh 500 100 -bgc 0.3 0.3 0.3 win_pasteTime;
    
    //set up the layout
    //allow the window to resize
    columnLayout -adj true;
    //text that shows the source -label -editable -text (first selected object) -name
    textFieldGrp -l "Source Object" -editable false -tx $selection[0] tx_scr;
    //variable that adds frame offset
    intFieldGrp -l "Frame Offset Amount" -v1 0 int_offset;
    //variable that adds randomization
    floatFieldGrp -l "Random Seed" -v1 0 fl_rand;
    
        
    //adding cmd button: -label -call command -background color name
    button -l "Paste Animation" -c "pasteTheseKeys" -bgc 0.2 0.2 0.5 btn1;
    
    //adding close button: -lable -command -background color name
    button -l "Close" -c "deleteUI -window win_pasteTime;" -bgc 0.4 0.4 0.4 btn2;
    
    //show window called ...
    showWindow win_pasteTime;

}//end else



//PASTE KEYS PROCEDURE
global proc pasteTheseKeys()
{
    //find what's the offset from the integer field; -query -v1 (name of the value) from variable named
    int $offsetValue = `intFieldGrp -q -v1 int_offset`;
    //add randomization, query seed
    float $randValue = `floatFieldGrp -q -v1 fl_rand`;
    //store the keys
    string $selectionObj_PasteKeys[] = `ls -sl`;
         
    //paste the animation in selected objects
    for($objQuantity = 0; $objQuantity < size($selectionObj_PasteKeys); $objQuantity++ )
    {
             //query current time for offset
             int $ct = `currentTime -q`;
             //current time + offset
             int $t = $ct + $offsetValue;
             
             //if there is no randomization
             if($randValue == 0){
                  //set current time
                 currentTime $t;
                 
                 //select a key to be copied
                 select $selectionObj_PasteKeys[$objQuantity];
                 pasteKey -t $t -option merge -copies 1 -connect 0 -timeOffset 1 -floatOffset 0 -valueOffset 0;
                                 
        }
        else
        { //use randomization
                 float $randomizeTime = rand($t, $randValue);
                  //set current time
                 currentTime $t;
                 
                 //select a key to be copied
                 select $selectionObj_PasteKeys[$objQuantity];
                 pasteKey -t $randomizeTime -option merge -copies 1 -connect 0 -timeOffset 1 -floatOffset 0 -valueOffset 0;  
        }//end else of if rand 
    }//end for loop
}//end global proc