//-------------------------------------------------------------------- //FileName: setCamera.mel //Purpose: Contains mel functions for setting a camera. // //Usage: Load in this file. Next, In the expression editor, create a new expression, and give it a //descriptive name, such as "cameraExpression". In the edit box at the bottom //of the expression window, call the setCamera as many times as you want to //specify where and when you need it. //Example: // setCamera ("zoomoutCamera", "mainCamera", 1, 219); // setCamera ("closeupCamera", "mainCamera", 220, 440); // //This would set the camera "mainCamera" to the same position and orientation //as "zoomoutCamera" during frames 1 - 219, and to the same position and orientation //as "closeupCamera" during frames 220 - 440. //Note: If you have more than one camera, instead of nameing them //camera1, camera2, camera3, ou must name them cameraA, cameraB, cameraC, //do to a "feature" of Maya that the cameraShape names get the number //at the end of any camera appended to the end of their name. //--------------------------------------------------------------------- //---------------------------------------------------------------------- //FunctionName: setCamera //Programmer: Tom Happ //Purpose: Sets the main scene camera to mimic another camera for a certain //interval of time. //---------------------------------------------------------------------- global proc setCamera (string $originalCamera, string $mimicCamera, int $startFrame, int $endFrame) { //These global variables make sure that we don't connect the same two //objects twice in a row. global string $lastOriginal; global string $lastMimic; float $frame = `currentTime -q`; //Get the current frame //First check to see whether we're in the proper interveral. if (($frame >= $startFrame) && ($frame <= $endFrame)) { //Next check that we haven't already made the connection between these two cameras //so that we don't repeat the instructions for each frame between the start and end. //Note that if I used "copyAttributes" instead of "connectAttributes", this check //would mess up the code. if ( ( strcmp($originalCamera,$lastOriginal) != 0) || (strcmp($mimicCamera,$lastMimic) != 0)) { print("\nSwitching cameras from camera " + $lastOriginal + " to camera " + $originalCamera + ".\n"); //Connect all the attributes. connectAttributes ($originalCamera, $mimicCamera); //Copy the attributes for the camera shapes as well. connectAttributes ( ($originalCamera + "Shape"), ($mimicCamera + "Shape")); $lastOriginal = $originalCamera; $lastMimic = $mimicCamera; }//End of inner if }//End of outer if }//End of setCamera //---------------------------------------------------------------------- //Function Name: connectAttributes //Programmer: Tom Happ //Purpose: Connects the attributes from one object to another. Does not //copy attributes which don't exist in the target object. //---------------------------------------------------------------------- global proc connectAttributes (string $sourceObject, string $targetObject) { float $frame = `currentTime -q`; //Note that the following statement works because MEL can read strings //as if they were code. Just be careful since $targetCamera could contain //anything! //select $targetObject; //Select the target object. $targetAttributes = `listAttr -r -k $targetObject`; for ($attributeName in $targetAttributes) { $alreadyConnected = `isConnected ($sourceObject + "." + $attributeName) ($targetObject + "." + $attributeName)`; if ($alreadyConnected == 0) { //Uncomment the line below for debugging. //print ("\nAttributeName: " + $attributeName); //The '-force' parameter tells it to break old connections; //if I didn't put in force, the target object would be "stuck" //after the first time this function was called. connectAttr -force ($sourceObject + "." + $attributeName) ($targetObject + "." + $attributeName); } }//End of for }//End of connectAttributes.