/* This file downloaded from Highend3d.com '' '' Highend3d.com File Information: '' '' Script Name: Batch Animation Transfer '' Author: daagom '' Last Updated: Nov 02, 2006 '' Update/Change this file at: '' http://Highend3d.com/maya/downloads/mel_scripts/utility_external/copying/890.html '' '' Please do not alter any information above this line '' it is generated dynamically by Highend3d.com and will '' be changed automatically on any updates. */ /* '' Author: Jeremiah Grant '' Title: batchAnimTransfer.mel '' Date: 11/02/06 '' '' Description: Takes a folder of animations and transfers them onto '' any rig with the same controller names. The script will create a new '' folder called TransferedAnimations in the folder where the animations '' are located so you don't have to override your old animations. If new '' attributes or controls are added, the script will just ignore them. You '' can chose to transfer just a few files or the entire directory. '' '' You can email me at jeremiah@jeremiahgrant.com '' Website: www.jeremiahgrant.com */ global string $newRigFilePath; global string $animsDirPath; global string $selectedAnimsFiles[]; $selectedAnimsFiles = {""}; global proc batchAnimTransfer() { global string $newRigFilePath; global string $animsDirPath; if(`window -exists batchTransferAnimUI`) deleteUI batchTransferAnimUI; window -rtf 1 batchTransferAnimUI; columnLayout -adj 1; rowColumnLayout -nc 3 -cw 1 100 -cw 2 280 -cw 3 24; // Mocap File text -label "New Rig:" -align "right"; textField -fi $newRigFilePath fileRigTF; symbolButton -image "navButtonBrowse.xpm" -command ("fileBrowserDialog -m 0 -fc \"updateRigFilePath\" -an \"New_Rig\" -om \"Reference\"; textField -e -fi $newRigFilePath fileRigTF; "); // Reverse Rig File text -label "Animations Directory:" -align "right"; textField -fi $animsDirPath fileAnimsTF; symbolButton -image "navButtonBrowse.xpm" -command ("fileBrowserDialog -m 4 -fc \"updateAnimsDirPath\" -an \"Character Animations Directory\" -om \"Reference\"; textField -e -fi $animsDirPath fileAnimsTF; "); setParent ..; separator -h 10 -style none; rowColumnLayout -nc 4 -cw 1 10 -cw 2 129 -cw 3 129 -cw 4 130; // string $collection1 = `radioCollection`; separator -h 1 -style "none"; string $rb1 = `radioButton -label "Entire Directory"`; string $rb2 = `radioButton -label "Select Files"`; button -l "Select Files" -c "selectAnimFiles" -en 0 fileChooseButton; setParent..; separator -h 10 -style none; button -l "Transfer Animation" -c "transferAnim"; showWindow; radioButton -e -onc "button -e -en 0 fileChooseButton;" -ofc "button -e -en 1 fileChooseButton" -sl $rb1; } // Update the newRigFilePath variable global proc updateRigFilePath (string $path, string $type) { global string $newRigFilePath; $newRigFilePath = $path; } // Update the animsDirPath variable global proc updateAnimsDirPath (string $path, string $type) { global string $animsDirPath; $animsDirPath = $path; } // Select specific files global proc selectAnimFiles() { global string $animsDirPath; global string $selectedAnimsFiles[]; if(size($animsDirPath) < 0) error "Please select an animation directory"; if(`window -exists animFileSelectionUI`) deleteUI animFileSelectionUI; string $window = `window -wh 230 311 animFileSelectionUI`; columnLayout -adj 1; string $textList = `textScrollList -w 175 -numberOfRows 20 -ams 1 -aas 1 -shi 4 animFileScrollList`; button -l "Transfer Selected Files" -c ("$selectedAnimsFiles = `textScrollList -q -si "+$textList+"`; deleteUI animFileSelectionUI"); setParent..; showWindow; // Populate the list populateAnimFileList $textList; } // global proc populateAnimFileList(string $textList) { global string $animsDirPath; string $files[] = `getFileList -folder ($animsDirPath+"/") -fs "*.ma"`; string $sortFiles[] = sort($files); string $inFiles; // reset list textScrollList -e -ra $textList; // and repopulate it for($inFiles in $sortFiles) { textScrollList -e -append $inFiles $textList; } if($inFiles != "") textScrollList -e -sii 1 $textList; } // Main Animation Transfer script global proc transferAnim() { print "\n----------------------------------------\n"; print "+ \n"; print "+ Please Wait - Transfering \n"; print "+ Animations \n"; print "+ \n"; print "----------------------------------------\n"; $startTime = `timerX`; scriptEditorInfo -e -sw 1 -se 1 -si 1 -sr 1; // Global Vars global string $selectedAnimsFiles[]; global string $newRigFilePath; global string $animsDirPath; string $newAnimsDir = ($animsDirPath+"/TransferedAnimations/"); // Make a new animation if(!`filetest -d $newAnimsDir`) sysFile -makeDir $newAnimsDir; $errChk = `chdir $newAnimsDir`; if($errChk == -1) error "Couldn't create Directory"; for($sel in $selectedAnimsFiles) { $animFile = ($animsDirPath+"/"+$sel); string $basename = `basename $sel ".ma"`; setAttr -l false "time1.outTime"; file -f -open $animFile; $minTime = `playbackOptions -q -min`; $maxTime = `playbackOptions -q -max`; setAttr -l false "time1.outTime"; file -f -new; // Set clipping planes for convenience setAttr "perspShape.farClipPlane" 10000; setAttr "topShape.farClipPlane" 10000; setAttr "frontShape.farClipPlane" 10000; setAttr "sideShape.farClipPlane" 10000; // Import the new rig file file -import -ra 0 -options "v=0" $newRigFilePath; // Import the temp animation file file -import -ra 1 -rpr ("animFileTemp") -options "v=0" $animFile; copyAnim ("animFileTemp"); // Set playback to original file playbackOptions -min $minTime; playbackOptions -max $maxTime; $fileName = ("\""+$newAnimsDir+$basename+"_TR\""); eval("file -rn "+$fileName+"; file -s -type \"mayaAscii\";"); print ("Saved: "+$fileName+"\n"); file -new; } scriptEditorInfo -e -sw 0 -se 0 -si 0 -sr 0; $totalTime = `timerX -startTime $startTime`; print "\n----------------------------------------\n"; print "+ Animation Transfer Complete \n"; print ("+ Total Transfer took: "+$totalTime+" sec\n"); print "----------------------------------------\n"; } global proc copyAnim(string $prefix) { global string $newRigFilePath; string $allObj[] = `ls ("animFileTemp*")`; string $obj; for($obj in $allObj) { string $buff[], $attrBuff[]; string $file = `basenameEx $newRigFilePath`; string $objDest = `substitute "animFileTemp_" $obj ""`; if(!`objExists $objDest`) continue; string $allAttr[] = `listAttr -k $obj`; for($attr in $allAttr) { // Tokenize attr for obj.attr.att if attrBuff[1] has data, skip tokenize $attr "." $attrBuff; if(`size($attrBuff[1])` > 0) continue; else if(`keyframe -query -keyframeCount ($obj+"."+$attr)`) { $numKeyFrames = `keyframe -attribute $attr -query -keyframeCount $obj`; // Test to make sure the attributes are keyable string $keyableAttrsArray[] = `listAttr -k $objDest`; string $keyableAttrs = `stringArrayToString $keyableAttrsArray " "`; // Test to make sure the attributes are not locked string $unlockedAttrsArray[] = `listAttr -u $objDest`; string $unlockedAttrs = `stringArrayToString $unlockedAttrsArray " "`; $match = `match $attr $keyableAttrs`; $lmatch = `match $attr $unlockedAttrs`; if ($numKeyFrames > 0 && `size($match)` > 0 && `size($lmatch)` > 0 && `objExists ( $objDest + "." + $attr )` && `objExists ( $obj + "." + $attr )` ) { // print ($obj + "." + $attr + " : " + $objDest + "." + $attr + "\n"); if( catch( `eval ("copyKey -t \":\" -f \":\" -at \""+$attr+"\" " + $obj)` ) ) { print ("Can't copy from " + $obj + "." + $attr + " to " + $objDest + "." + $attr + "\n"); } if( catch( `eval ("pasteKey -option replaceCompletely -at \""+$attr+"\" " + $objDest)` ) ) { print ("Can't paste from " + $obj + "." + $attr + " to " + $objDest + "." + $attr + "\n"); } string $object = ($obj+"."+$attr); string $objectDest = ($objDest+"."+$attr); int $wt[] = `keyTangent -q -wt $object`; if($wt[0]) { $numKeys = `keyframe -query -keyframeCount $object`; $time = `findKeyframe -w first $object`; for ( $counter = 0; $counter < $numKeys ; $counter++ ) { float $inWeight[] = `keyTangent -time $time -q -iw $object`; float $outWeight[] = `keyTangent -time $time -q -ow $object`; keyTangent -e -wt 1 $objectDest; if( catch( `keyTangent -time $time -ow $outWeight[0] ($objDest+"_"+$attr)` )) print ("Error weighting " + $objDest+"_"+$attr + " out tangents" ); if( catch( `keyTangent -time $time -iw $inWeight[0] ($objDest+"_"+$attr)` )) print ("Error weighting " + $objDest+"_"+$attr + " in tangents" ); $time = `findKeyframe -w next $object`; } } } } } } $delete = `ls ("animFileTemp*")`; $grp = `group $delete`; delete $grp; } batchAnimTransfer;