abBatchFile

May 11th, 2009

A handy little script that allows you to run mel commands on one or many Maya scene files (.ma or .mb).  Once the specified script is run on a file, it can be saved (or not saved) in a variety of ways.

Getting Started

Note that this script has only been tested on Windows.  Some of the mel I've used is most likely Windows only (the file dialog stuff, and some of the file handling); I'm not sure how it will actually run on OSX or linux.  If you want to try it on either OS, go ahead; just make sure that the "Save" check box is unchecked, and prepare for disappointment.

Ok, with that bit of ugliness out of the way, (ahem)...

To run the script enter the following in the command line:

source abBatchFile.mel;abBatchFile();

You can add files to the current batch in one of two ways: click the "Add Directory To Batch" button to add all of the .ma and .mb files in a given folder to the file list, or click "Add File To Batch" to add a single file.  You can remove one or all files using the right click context menu.

Once you've got some files in the batch, you'll need to add some mel to the script field.  Make sure that any script that you enter is properly sourced and is sure to fail gracefully.

Once that's done you're ready to specify what the script should do with a batch file once processed.

Save Me, or Don't...

By default, files won't be saved once the script is run.  This can be useful; sometimes you might just need to glean some info from a file and do something with it (eg: a mel function that writes a render batch file for a given scene).  But if you do want to save the modified file, you can do that too.

To save a batch file after it has been processed, click the "Save" button.  If you enable the Save button and leave the other check boxes unchecked, the modified files (or Post Script Batchfiles, PSBs for short) will be saved with the same file name (over themselves).

If you want to increment the file name, you can do so by checking the "Incremental Save" box.  This will increment any numeric characters found at the end of the file name and immediately preceding the file suffix (eg: the 2 in myFile2.mb).  If there are no numeric characters at the end of the file name, the script will add them, starting with 1 and a default padding of 3.  If, after the file is incremented, there is found to be an existing file with the same name in the given directory, a snazzy suffix will be added: "_abBatchFile_Name_Overlap" (you can change this string in the abBFGlobals proc if you so desire).

If you want to save the modified fi..., wait, no, the PSBs, to a directory other than their source directory, you can enable the "Save To New Directory" check box.  Select the new save directory using the "Select Save Directory" button.

Run Batch, Run

Now you're ready to go.  To run the batch immediately, click the "Run Batch" button.  You'll get the option to cancel the run if the script finds any unsaved changes in the current scene.

Alternatively, you can save the batch to a .mel file, in case you want to run it from the prompt, run it again later, or just love and cherish it.  Click the "Save Batch" button, and save it somewhere in your Maya scriptPath.  Once saved, you can run it in the UI-less version of Maya (which is much faster than otherwise).

To do so, type:

mayaBatch -prompt

in the Windows command line.  Once Maya has loaded and you're presented with the "mel:" prompt, source your batch file (eg: "source mayaBatch.mel"), hit enter, and watch the ASCII fly.

One More Thing

Say you don't want to keep entering the same files into the UI every time you start a new session of Maya.  Well, you can do that.  Just run the following bit of mel in the Script Editor:

abBFSaveState();

Save the script it spits out (below the dashed line) and paste and run it anytime you want to get back to that batch setup.

One More Advanced Thing

As of version 1.1, batch scripts have access to the file save boolean $gSaveBatchFile.  Which means you can save batch files (or not save them) if they meet certain criteria specified by your batch script.

Note that each file in the batch will still default to the save settings specified in the UI.  So, if the "Save" check box is checked, $gSaveBatchFile will be reset to true at the beginning of each batch file operation.  If "Save" is unchecked and your batch script sets $gSaveBatchFile to true, the current save options ("Incremental Save", "Save To New Directory", etc) will be respected even though they're disabled in the script UI.

That's it.  Enjoy.


Updates

Version 1.0
May 11th, 2009 Fixed abBFSaveState() function to work with muli-line scripts.
Version 1.1
May 13th, 2009 Added the ability for batch scripts to access the file save boolean $gSaveBatchFile.
Version 1.2
May 14th, 2009 The batch script is now wrapped in a catchQuiet function, so if an error is raised when it's run on a scene, the batch will continue on to the next file without saving (regardless of the save settings in the UI); abBFSaveState function is no longer befuddled by nested escaped characters;  added and improved popUp menus for the script and file fields.
Version 1.3
July 5th, 2010 Added fix so that batches with comments in the script won't throw an error when run from the command line.
Version 1.31
October 3rd, 2012 Updated the script to work with Maya 2011 and above.  Stick to v. 1.3 if you're using an older version of Maya.

"abBatchFile" was posted on May 11th, 2009 under Mel Scripts.  It was created using Maya.

This entry was last updated on October 3rd, 2012.

Subscribe to the Comments Feed or Updates Feed for this entry.

Share |

comments

Dave Jones
February 8th, 2011 said:

Very cool script. Very helpful.

The only thing I think it needs is a way to add sub-folders automatically.

Oh, and your links all point to version 1.1

February 8th, 2011 said:

Thanks Dave.  I updated the links.

Agit
September 30th, 2012 said:

handy handy!! it seems to no longer work in maya 2012 though, // Error: line 65: Controls must have a layout.  No layout found in window : //

October 3rd, 2012 said:

Thanks Agit,

I just uploaded version 1.31, which should address the issues with recent versions of Maya.

James
September 20th, 2016 said:

Very nice stuff!

Everything works beautifully, but I'm getting an error when I try to use Save To New Directory.

// Error: line 21: An invalid path was specified. //

I'm using Maya 2016 on Windows 10.

Many thanks in advance!!


just say it






    

Related Posts