Sunday, December 30, 2012

Some helpful scripts for Maya

Knowing basic scripting is always useful. Sometimes, I find myself searching how to do something in a 3D package just to realize the program doesn't have the feature. When those cases arrive, I know it's time for some coding.

My knowledge about scripting languages (basically MEL and Maxscript, but learning some Python now... ;)) is basic to intermediate. I don't know the code itself, but I know how to break apart an existing script, understand it and use it to make my own. It's easy for me to understand what some commands do so I can use them the way I want.

When working with Maya, there are some things that would made my life easier, so I decided to write some simple scripts to help me on my workflow. Below are some lines of code that you can grab and use as you like. I have tested these scripts in Maya 2010 (64-bit), so I don't know if they work on other versions.

Select children 

This script will select all the children controls starting from the one you have selected.So for example, if you want to select all the finger controls, just select the first control on each finger (that would be the control closest to the hand) and run the script.

For those of you familiar with 3ds max, this would be like double clicking a control.

string $sel[] = `ls -selection`;
string $namespace = `match ".*:"  $sel[0]`;
select -hi $sel;
string $selChild[] = `ls -s -sl`;
$selChild = `stringArrayRemoveDuplicates $selChild`;
select -r `listRelatives -p $selChild`;

This script will select only controls that are based on SHAPES, so if your rig have some Nulls or other node type as a control, the script won't work.

Playback selection

Sometimes, when working with several viewports at the same time, the playback lags. In order to reduce such lag, this script will enable/disable the playback on just the selected viewport.

Run the script to toggle between playback in ALL viewports or just in the one selected.

if (`playbackOptions -query -view` == "active") playbackOptions -view "all";
else playbackOptions -view "active";

Add keys without disrupting the curve

I got this line from Kiel Figgins at his site while searching for a way to add keys in the Curve Editor without disrupting the curve that I already had.

channelBox -e -exe "setKeyframe -i -at \"#A\" #N;" 1 $gChannelBoxName;

This is the link to Kiel's reference of this command: Link.

Incremental Save

There are a lot of scripts that do this, so I decided to do one too! Nothing special with it, just saves the file with an incremental number at the end each time the script runs, e.g. if the file name is "Test.mb", after the script it will be "Test_001.mb".

Due to the size of the script, I decided to create a .MEL file. To run the script, place the file in your script folder (C:\Users\username\Documents\maya\mayaVersion\scripts) and add a button in a shelf with the following line (you will need to restart Maya for the changes to take effect):

source "kronIncrementalSave.mel";

Link for Incremental save file: Here

The file has to have a name, it can't be "untitled" or else it will save the file as "unknown_001.mb". Also, for some reason that I'm still trying to debug, the first time you run the script you will have the following error:

Error: Invalid call to "substitute".  Check number and types of arguments expected by the procedure.

I don't know why is that, but if you run the script again it should work fine.

Anyways, feel free to try them and if you want to share them, do not forget to give proper credit. ;)


No comments: