Camera Switching with MEL

In this tutorial we are going to devise a way to toggle through the available cameras, making the current panel the camera view, just by pressing a hotkey. I will be using this scene to test our script

Typically the "0" (zero) key is not associated as a hotkey so we are going to create a script that is invoked every time the "0" key is pressed.

To perform such an action manually in Maya we do the following;

1) Select a panel
2) Select the camera we wish to look through
3) In the panel menus select: Panels > Look through selected

While this is fairly simple, it is time consuming when you are doing a lot of camera work and especially when you are relying on visual cues to select your camera.

So how does Maya perform the above tasks in MEL?

Lets have a look at the MEL script that the script editor spits out when we perform the above actions.

select -r camera1 ;

Hmmm... thats not very impressive is it? Yet you will find that this is all Maya that is prepared to share with you for this particular action.

So out come the thumb screws! Lets make Maya spill the beans.

In the script editor select: Script > Echo all commands

Now lets see what Maya yields

select -r camera1 ;
ikSelectionChanged("MayaWindow|mainKeysMenu|menuItem1011|ikFKStateItem");
statusLineUpdateInputField;
buildPanelItemsNow modelPanel4 MayaWindow|mayaMainWindowForm|formLayout3|viewPanes|modelPanel4|Panels;
buildPanelItemsNow modelPanel4 MayaWindow|mayaMainWindowForm|formLayout3|viewPanes|modelPanel4|Panels;
lookThroughSelected modelPanel4;
editMenuUpdate MayaWindow|mainEditMenu;

This is the absolute explicit Maya feedback, but most of what we see is useless information (which is half the reason why we normally have the "Echo all commands" turned off).

But at least we can see here the pertinent command: lookThroughSelected modelPanel4;

so a simplified version of the above would be

select -r camera1 ;
lookThroughSelected modelPanel4;

Easy!

Luckily for us there is a MEL command that will take us most of the way here. It is called the lookThru command. We can use it to both query Maya as to which camera we are currently looking through, and secondly to make the current panel look through a specified camera.

(1) To find out which camera we are currently looking through we can execute lookThru in query mode

lookThru -q

and we can store the camera name with

$currentCamera = `lookThru -q`;

(2) Next we need to find out a list of the cameras in the scene and where our active camera lies on this list and then choose the next camera in the list. To do this we can use the listCameras command. The listCameras -p will list only perspective cameras, and -o will list orthographic cameras only.

string $allCameras[];
$allCameras = `listCameras`; // list all cameras in the scene

We now create a marker variable that records the position on the list after the active camera.

int $nextCamera;

With this we can now run through the list of cameras and find out where the active camera lies.

for($pos=0;$pos<size($allCameras);$pos++)
{
if($currentCamera == $allCameras[$pos]) $nextCamera = $pos+1; //Found it!! so choose the next one in line
}

The last check is to make sure we don't run off the end of the list, if we do, then we jump back to the top of the list.

if($nextCamera>=size($allCameras)) $nextCamera = 0;

Finally we can change the camera to look through the new camera with the simple command

lookThru $allCameras[$nextCamera];

So now our entire code is thus;

$currentCamera = `lookThru -q`;
string $allCameras[];
$allCameras = `listCameras`;
int $nextCamera;
for($pos=0;$pos<size($allCameras);$pos++)
.....{
.....if($currentCamera == $allCameras[$pos]) $nextCamera = $pos+1;
.....}
if($nextCamera>=size($allCameras)) $nextCamera = 0;
lookThru $allCameras[$nextCamera];

If we wanted to toggle backwards we would make the following modifications;

$currentCamera = `lookThru -q`;
string $allCameras[];
$allCameras = `listCameras`;
int $prevCamera;
for($pos=0;$pos<size($allCameras);$pos++)
.....{
.....if($currentCamera == $allCameras[$pos]) $prevCamera = $pos-1;
.....}
if($prevCamera<=0) $prevCamera = size($allCameras)-1;
lookThru $allCameras[$prevCamera];

Setting up Hotkeys

With our code now working nicely, lets assign each to the hot keys "9" and "0" which aren't being used for my current preferences. To set a hotkey click;

Window > Settings/Preferences > Hotkeys

Click New

For the Name type ToggleCameraForward

Choose the "user" category

and type the forward code into the command box

Then click "Accept"

This creates a new command organized under the user category.

Repeat this for the ToggleCameraBackward script.


With our commands stored, we can now assign a hotkey to each. To assign a hotkey, type the keypress you wish to associate with a command and select that command in the commands list and then click Assign.

A good practice however is to always first click Query to make sure that the keypress you have chosen is not already associate with an important part of your current hotkey setup.

If the keypress is unavailable, you can try variations on the keypress, such as the addition of a Ctrl or Alt in combination with your desired keypress. You can also use Function keys.


So thats it. I hope this makes using maya just that little bit easier.