Controlling Depth of Field (DoF) in Maya - Automation via MEL

So now that we have looked at how easy DoF is to connect using 2 and 3 node cameras let's write a script that automatically does this for us. This script will work as long as any node (lookAt, camera, locator) of the camera group is selected.

Like with most of my scripts this will be a global procedure. I normally use my full initials for my scripts, use what ever you like.

global proc ewc_connectDoF()
{

 

Create and array out of the current selection. Take the first index in the selection and query the node type and store it in the $type variable.

	// Create selection and query node type
	string $sel[] = `ls -selection`;
	string $type = nodeType($sel[0]);
	string $lookAt, $camera[];

 

Interesting note I want to make with some commands in Maya. You will notice that on the node type query that there is no back quotes (`) used. Some commands, like nodeType, can be used with two different syntax structures. The structure you pick will determine if back quotes are needed. Above I used the bracket delimiter style which encompasses the data (in this case a string) in parentheses. This method does NOT require the use of back quotes.

However if I was to use it without parentheses (the delimiter is now the space between the command and the data) then the command will require back quotes.

	string $type = `nodeType $sel[0]`;

 

Just a little something I wanted to demonstrate so I could validate my nerd status.

The next step is to evaluate the node type. If it is NOT a 'lookAt' node then we continue on to evaluate the selection to see if it is either the camera or locator(s).

	 // Test node type
	if ($type != "lookAt")
	{

 

If the selection is either the camera or the locator(s) we will need to query the parent node which should be the 'lookAt' node.

		string $parent = firstParentOf($sel[0]);

 

If the string is not empty query the node type of the parent object. If it is empty the selection is invalid because either a normal camera is selected (which has no 'lookAt' node) or a completely unrelated object all together. We then cancel the whole procedure with a return.

		// Test that there is a parent else cancel
		if ($parent != "")
			$type = nodeType($parent);
		else
			return;

 

If the parent is a 'lookAt' node we need to get the camera shape. This is done with the listRelatives command. All children are queried with the -ad (all descendents) flag and we isolate the node type as 'camera' with the -type flag. Finally store the name of the queried parent in the $lookAt variable.

		// If the parent is a lookAt get camera else cancel
		 if ($type == "lookAt")
 		{
 			$camera = `listRelatives -ad -type "camera" $parent`;
	 		$lookAt = $parent;
 		} else {return;}

 

Earlier we declared $camera as a string array. This is because the data type returned by listRelatives is always a string array.

We return up to the else-statement (associated with the first if-statement). If the original node type queried was a 'lookAt' node then store the name of the selected object into $lookAt and query the camera shape.

	} else {
		// The $type is a lookAt node
		$lookAt = $sel[0];
 		$camera = `listRelatives -ad -type "camera" $lookAt`;
 	}

 

In the event that DoF was NOT enable, enable it.

	// Enable DOF
	setAttr ($camera[0] + ".depthOfField") on;

 

Now connect the 'distanceBetween' attribute to the 'focusDistance' attribute. Finish with the closing curly bracket to properly end the procedure.

	// Connect .distanceBetween to .focusDistance
	connectAttr -force ($lookAt + ".distanceBetween") ($camera[0] + ".focusDistance");
}

 

The complete script:

global proc ewc_connectDoF()
{
	// Create selection and query node type
	string $sel[] = `ls -selection`;
	string $type = nodeType($sel[0]);
	string $lookAt, $camera[];

	// Test node type
	if ($type != "lookAt")
	{
		string $parent = firstParentOf($sel[0]);

		// Test that there is a parent else cancel
		if ($parent != "")
			$type = nodeType($parent);
		else
			return;

		// If the parent is a lookAt get camera else cancel
		if ($type == "lookAt")
		{
			$camera = `listRelatives -ad -type "camera" $parent`;
			$lookAt = $parent;
		} else {return;}
	} else {
		// The $type is a lookAt node
		$lookAt = $sel[0];
		$camera = `listRelatives -ad -type "camera" $lookAt`;
	}

	// Enable DOF
	setAttr ($camera[0] + ".depthOfField") on;

	// Connect .distanceBetween to .focusDistance
	connectAttr -force ($lookAt + ".distanceBetween") ($camera[0] + ".focusDistance");
}

 

Source the script and experimenting by creating a 2 or 3 node camera and running the procedure name.

ewc_connectDoF();

 

Now that the script is written you have a very fast and effective way to create a interactive connection with the DoF attributes of your cameras.

Work smarter, not harder...

- Ed

 

<< < 0 1 2 3 4 > >>

 

Download completed script


References:

fStop (aperture) and shutter speed/angle

Adjust depth of field