/* NAME: dbFileAutomator.mel VERSION: 1.1 CATEGORY: Utilities FUNCTION: A utility that allows you to run a sequence of MEL commands through a list of files, with the ability to save the MEL script for later use, as well as save and load the file list. Sometimes this can be done with batch mode, but there are some commands that won't run in batch, so if you need the Maya window open and need to run a number of scripts through a bunch of files, this will do the trick. USAGE: 1. Copy the dbFileAutomator.mel into your Maya Scripts directory (Usually found in "My Documents\maya\\scripts\"). 2. In the Maya Script Editor, type in "source dbFileAutomator.mel; dbFileAutomator();" REQUIRES: dbReloadFA.mel AUTHOR: David Bokser E-MAIL: me@davidbokser.com WEBSITE: www.davidbokser.com VERSION HISTORY: 1.10 - Aug 09, 2005 - Cleaned up a lot of the script so that it doesn't require the temp mel file in the temp dir, files don't need to be applied and saved before running, and the dbReloadFA.mel file is unneeded. Also added different save functionality so that you can now choose to overwrite, not save, or save with suffix. 1.00 - Aug 06, 2005 - Initial Release. */ /////////////////////////////////// // addFile(string, string) // // Function for adding file names to the // textScrollList in dbAutoHairSim() /////////////////////////////////// global proc int addFile( string $filename, string $fileType ) { textScrollList -e -append $filename fileNameTSL; return 1; } /////////////////////////////////// // dbFileAutomator() // // Creates a window that allows files // to be added to a textScrollList. The // files in the list are then used for // simulating hair and exporting into // MachStudio. /////////////////////////////////// global proc dbFileAutomator() { if(`window -q -ex faWin`) deleteUI -window faWin; string $directory = `faMakeDir`; string $lookDir = ($directory + "/"); string $window = `window -title "File Automator" -iconName "FileAutomator" -widthHeight 410 640 -menuBar true faWin`; menu -label "File" -tearOff false; menuItem -label "Save File List" -c ("fileBrowserDialog -m 1 -fc \"faSaveFileList\" -an \"Save\" -om \"SaveAs\";"); menuItem -label "Load File List" -c ("fileBrowserDialog -m 0 -fc \"faLoadFileList\" -an \"Load List\" -om \"Import\";"); menuItem -divider true; menuItem -label "Quit" -command ("deleteUI -window " + $window + "; if(`window -q -ex faAboutWindow`) {deleteUI -window faAboutWindow;}"); menu -label "Scripts" -tearOff false; string $scripts[] = `getFileList -folder $lookDir -filespec "*.mel"`; for ($script in $scripts) menuItem -label $script -c ("faLoadFileToSL(\"" + $lookDir + $script + "\", \"mel\")"); menu -label "Help" -helpMenu true; menuItem -label "About Application..." -c "faAboutWin"; scrollLayout -verticalScrollBarThickness 4; columnLayout; frameLayout -label "Script" -labelAlign "top" -cll true; columnLayout -adjustableColumn true; textFieldGrp -label "Function Name" -cw 1 90 nameTFG; scrollField -wordWrap true -w 372 -h 200 scriptField; rowColumnLayout -numberOfColumns 3 -cw 1 124 -cw 2 124 -cw 3 124; button -label "Apply and Save" -c ("faSaveFile(`textFieldGrp -q -tx nameTFG`, `scrollField -q -tx scriptField`, \"" + $directory + "\")"); button -label "Load" -c ("faLoadFile"); button -label "Clear" -c ("scrollField -e -text \"\" scriptField; textFieldGrp -e -tx \"\" nameTFG;"); setParent..; setParent..; setParent..; frameLayout -label "Automate" -labelAlign "top" -cll true; columnLayout -adjustableColumn true; paneLayout -h 200 -w 150; textScrollList -numberOfRows 8 -allowMultiSelection false -showIndexedItem 4 fileNameTSL; setParent..; columnLayout; rowColumnLayout -numberOfColumns 3 -cw 1 124 -cw 2 124 -cw 3 124; button -label "Add File" -c ("fileBrowserDialog -m 0 -fc \"addFile\" -ft \"mayaBinary\" -an \"Add File\" -om \"Import\";"); button -label "Remove File" -c ("textScrollList -e -ri (`textScrollList -q -si fileNameTSL`) fileNameTSL"); button -label "Clear All" -c ("textScrollList -e -ra fileNameTSL"); setParent..; frameLayout -label "Options" -labelAlign "top" -cll true -w 372 -cl 1 -borderStyle "etchedOut" -cc ("window -e -h 641 " + $window) -ec ("if(2 == `radioButtonGrp -q -sl saveRBG`){window -e -h 678 " + $window + ";} else{window -e -h 657 faWin;}") optionFrame; columnLayout optionLayout; radioButtonGrp -numberOfRadioButtons 3 -cw 1 80 -cw 2 110 -sl 3 -labelArray3 "Overwrite" "Save with Suffix" "Do not save" -on2 ("setParent optionLayout; textFieldGrp -label \"Suffix:\" -cw 1 60 -cw 2 150 suffixTFG; frameLayout -e -w 372 optionFrame; window -e -h 678 faWin;") -of2 ("deleteUI suffixTFG; frameLayout -e -w 372 optionFrame; window -e -h 657 faWin;") saveRBG; setParent ..; setParent ..; rowColumnLayout -nc 1 -cw 1 372; button -label "Do it" -command ("string $suffix = \"_FA\"; if(`textFieldGrp -q -ex suffixTFG`){ $suffix = `textFieldGrp -q -tx suffixTFG`;} else { string $suffix = \"\";} faDoIt($suffix);"); button -label "Close" -command ("deleteUI -window " + $window + "; if(`window -q -ex faAboutWindow`) {deleteUI -window faAboutWindow;}"); setParent ..; setParent..; setParent ..; setParent..; showWindow $window; window -e -h 640 -w 410 $window; } /////////////////////////////////// // faDoIt(string) // // Goes through the text scroll list // from the FileAutomator window and // opens each file, run the automation, // and saves out the file. /////////////////////////////////// global proc faDoIt(string $suffix) { global string $gMainProgressBar; // This is defined on maya startup int $i; int $numOfFiles = `textScrollList -q -numberOfItems fileNameTSL`; if ($numOfFiles == 0) { error "No files in list!"; } string $fileList[]; string $dir = `faMakeDir`; $dir = `substituteAllString $dir "/" "////"`; $dir = $dir + "////"; string $funcName = `textFieldGrp -q -tx nameTFG`; if ($funcName == "") { error "There is no function name!"; } $funcName = $funcName + ".mel"; string $fileName = ($dir + $funcName); progressBar -edit -beginProgress -isInterruptable true -status "Batching ..." -maxValue $numOfFiles $gMainProgressBar; for ($i = 0; $i < $numOfFiles; $i++) { textScrollList -e -sii ($i + 1) fileNameTSL; string $selectedTSL[] = `textScrollList -q -si fileNameTSL`; $fileList[$i] = $selectedTSL[0]; file -f -options "v=0;p=17" -typ "mayaBinary" -o $fileList[$i]; // THIS IS THE STUFF THAT GETS AUTOMATED // IN THE SCENE print "sourcing file"; eval ("source \"" + $fileName + "\";"); // END AUTOMATION STUFF if(`progressBar -query -isCancelled $gMainProgressBar`) break; progressBar -edit -step 1 $gMainProgressBar; if (1 == `radioButtonGrp -q -sl saveRBG`) { file -save; } else if (2 == `radioButtonGrp -q -sl saveRBG`) { if ($suffix == "") { warning "No suffix entered! Files will not be saved!\n"; } else { string $curFileLoc = `file -q -loc`; string $project = `match "^.*/" $curFileLoc`; string $curFilename = `match "[^/\\]*$" $curFileLoc`; $curFilename = `substitute ".mb" $curFilename ""`; setProject $project; file -rn ($curFilename + $suffix + ".mb"); file -s; } } else { warning "File not saved through File Automator\nIf you saved the file through the MEL script, disregard this warning\n"; } } progressBar -edit -endProgress $gMainProgressBar; } /////////////////////////////////// // faMakeDir() // // Function for making the "Automator" // directory in the same location as the // FileAutomator script. /////////////////////////////////// global proc string faMakeDir() { string $scriptDesc = `whatIs dbFileAutomator`; string $scriptPath = `substitute "Mel procedure found in: " $scriptDesc ""`; string $dir = `match "^.*/" $scriptPath`; sysFile -makeDir ($dir + "Automator"); return ($dir + "Automator"); } /////////////////////////////////// // faSaveFile($functionName, $function, $dir) // // Function for creating the file with the // automated commands. // // Takes in the function name, the function // itself, and the directory where to put // the file. /////////////////////////////////// global proc faSaveFile(string $functionName, string $function, string $dir) { if ($functionName != "") { $fileName = ($dir + "\\" + $functionName + ".mel"); $fileId=`fopen $fileName "w"`; string $allScript = ("global proc " + $functionName + "()\n{\n" + $function + "\n} //EOF\n"); $allScript = ($allScript + $functionName + ";"); fprint $fileId $allScript; fclose $fileId; } else error "There is no function name!"; } /////////////////////////////////// // faLoadFileToSL(string, string) // // Function for parsing the given file // and extracting the function name // and function code. /////////////////////////////////// global proc int faLoadFileToSL( string $filename, string $fileType ) { int $fileId=`fopen $filename "r"`; //get the first line of text string $nextLine = `fgetline $fileId`; string $functionName = `substitute "global proc " $nextLine ""`; $functionName = `substitute "\\(\\)" $functionName ""`; string $function = ""; string $EOF; textFieldGrp -e -tx $functionName nameTFG; fgetline $fileId; //while $nextline is not emtpy(end of file) do the following // while ( size( $nextLine ) > 0 ) { while ( $EOF != "EOF" ) { $nextLine = `fgetline $fileId`; $EOF = `match "EOF" $nextLine`; if ($EOF != "EOF") $function = $function + $nextLine; } scrollField -e -tx $function scriptField; //close file fclose $fileId; return 1; } /////////////////////////////////// // faLoadFile() // // Opens a file browser and loads the selected // file. /////////////////////////////////// global proc faLoadFile() { fileBrowserDialog -m 0 -fc "faLoadFileToSL" -ft "mel" -an "Load" -om "Load"; } /////////////////////////////////// // faSaveFileList() // // Saves the file list to a file /////////////////////////////////// global proc faSaveFileList( string $filename, string $fileType ) { $fileId=`fopen $filename "w"`; string $fileList = ""; int $i; int $numOfFiles = `textScrollList -q -numberOfItems fileNameTSL`; for ($i = 0; $i < $numOfFiles; $i++) { textScrollList -e -sii ($i + 1) fileNameTSL; string $selectedTSL[] = `textScrollList -q -si fileNameTSL`; if ($i == 0) $fileList = ($selectedTSL[0]); else $fileList = ($fileList + "\n" + $selectedTSL[0]); } fwrite $fileId ( $fileList ); fclose $fileId; } /////////////////////////////////// // faLoadFileList() // // Loads a file list to the Automator window /////////////////////////////////// global proc faLoadFileList( string $filename, string $fileType ) { int $fileId=`fopen $filename "r"`; string $nextLine = `fgetline $fileId`; while ( size( $nextLine ) > 0 ) { string $line = `match "^[^(\r\n)]*" $nextLine`; textScrollList -e -append $line fileNameTSL; $nextLine = `fgetline $fileId`; } } /////////////////////////////////// // faAboutWin() // // Opens the about window /////////////////////////////////// global proc faAboutWin() { if(`window -q -ex faAboutWindow`) deleteUI -window faAboutWindow; // Make a new window // string $aboutWin = `window -title "About" -iconName "About" -widthHeight 330 370 faAboutWindow`; columnLayout -adjustableColumn true; scrollField -wordWrap true -w 230 -h 300 -ed 0 -tx ("File Automator v 1.1\n\n" + "To run, type in the mel commands " + "you want to apply to your files. Click " + "\"Apply and Save\". Then, add in the " + "files you want to automate by clicking " + "\"Add Files\" in the \"Automate\" window.\n\n" + "The script will run through your files " + "and apply the code. It will also save the " + "code for future reference and will show " + "up in the \"Scripts\" menu on the menu bar\n\n" + "Enjoy!\n\n" + "This script was brought to you " + "by David Bokser. For comments " + "and suggestions " + "e-mail me@davidbokser.com\n\n" + "Copyright © David Bokser 2005\n") aboutField; button -label "Visit Website" -c "faOpenBrowser"; button -label "Close" -command ("deleteUI -window " + $aboutWin); setParent ..; showWindow $aboutWin; // Resize the main window // window -edit -widthHeight 330 370 $aboutWin; } /////////////////////////////////// // faOpenBrowser() // // Opens the browser window the the // davidbokser.com website for // shameless self-promotion. /////////////////////////////////// global proc faOpenBrowser() { if(`window -q -ex faBrowserWin`) deleteUI -window faBrowserWin; string $browser; window faBrowserWin; columnLayout; $browser = `webBrowser -width 800 -height 600 -url "www.davidbokser.com"`; showWindow faBrowserWin; }