proc miApplyRenderGlobalsPresets(string $node, string $preset)
{
	switch ($preset)
	{
		case "Preview":
		break;

		case "Production":
		break;

		case "FinalFrameEXR":
			setAttr ($node + ".imageFormat") 51;
			setAttr ($node + ".imfkey") -type "string" "exr";
			setAttr ($node + ".imfPluginKey") -type "string" "exr";
		break;
	}
}

proc miApplyMentalRayOptionsPresets(string $node, string $preset)
{
	switch ($preset)
	{
		case "Preview":
			setAttr ($node + ".filter") 0;	// box
			setAttr ($node + ".filterWidth") 1.0;
			setAttr ($node + ".filterHeight") 1.0;
 			setAttr ($node + ".miRenderUsing") 0;
			setAttr ($node + ".miSamplesQualityR") 0.01;
			setAttr ($node + ".miSamplesQualityG") 0.01;
			setAttr ($node + ".miSamplesQualityB") 0.01;
			setAttr ($node + ".miSamplesQualityA") 0.01;
			setAttr ($node + ".miSamplesMin") 0.5;
			setAttr ($node + ".miSamplesMax") 100;

		break;

		case "Production":
			setAttr ($node + ".filter") 2;	// gauss
			setAttr ($node + ".filterWidth") 0.6666666667;
			setAttr ($node + ".filterHeight") 0.6666666667;
			setAttr ($node + ".maxReflectionRays") 4;
			setAttr ($node + ".maxRefractionRays") 6;
			setAttr ($node + ".maxRayDepth") 10;
			setAttr ($node + ".maxShadowRayDepth") 5;
			setAttr ($node + ".miRenderUsing") 0;
			setAttr ($node + ".miSamplesQualityR") 0.6;
			setAttr ($node + ".miSamplesQualityG") 0.6;
			setAttr ($node + ".miSamplesQualityB") 0.6;
			setAttr ($node + ".miSamplesQualityA") 0.6;
			setAttr ($node + ".miSamplesMin") 1;
			setAttr ($node + ".miSamplesMax") 100;

		break;

		case "FinalFrameEXR":
			setAttr ($node + ".filter") 2;	// gauss
			setAttr ($node + ".filterWidth") 0.6666666667;
			setAttr ($node + ".filterHeight") 0.6666666667;
			setAttr ($node + ".maxReflectionRays") 4;
			setAttr ($node + ".maxRefractionRays") 6;
			setAttr ($node + ".maxRayDepth") 10;
			setAttr ($node + ".maxShadowRayDepth") 5;
			setAttr ($node + ".shadowMethod") 3;
			setAttr ($node + ".miRenderUsing") 0;
			setAttr ($node + ".miSamplesQualityR") 1.5;
			setAttr ($node + ".miSamplesQualityG") 1.5;
			setAttr ($node + ".miSamplesQualityB") 1.5;
			setAttr ($node + ".miSamplesQualityA") 1.5;
			setAttr ($node + ".miSamplesMin") 1;
			setAttr ($node + ".miSamplesMax") 250;
            
		break;
	}
}

proc miApplyMentalRayGlobalsPresets(string $node, string $preset)
{
	switch ($preset)
	{
		case "Preview":
		break;

		case "Production":
		break;

		case "FinalFrameEXR":
			setAttr ($node + ".imageCompression") 5;
			setAttr ($node + ".compressionQuality") 0;
		break;
	}
}

proc miApplyMentalRayFramebufferPresets(string $node, string $preset)
{
	switch ($preset)
	{
		case "Preview":
		break;

		case "Production":
		break;

		case "FinalFrameEXR":
			setAttr ($node + ".datatype") 16;
		break;
	}
}

proc miApplyPresetToNode(string $node, string $preset)
{
	string $nType = `nodeType $node`;
	switch ($nType)
	{
		case "renderGlobals":
			miApplyRenderGlobalsPresets($node, $preset);
		break;

		case "mentalrayOptions":
			miApplyMentalRayOptionsPresets($node, $preset);
		break;

		case "mentalrayGlobals":
			miApplyMentalRayGlobalsPresets($node, $preset);
		break;

		case "mentalrayFramebuffer":
			miApplyMentalRayFramebufferPresets($node, $preset);
		break;
	}
}

proc string[] miApplyMentalRayOptionsPresetsCustom(string $preset)
{
	string $cmds[];
	switch ($preset)
	{
		case "Preview":
			$cmds[size($cmds)] = "global int $gMiStringOptUnifiedSamplingIndex;";
			$cmds[size($cmds)] = "setAttr (\".stringOptions[\" + $gMiStringOptUnifiedSamplingIndex + \"].value\") -type \"string\" \"true\";";
			$cmds[size($cmds)] = "global int $gMiStringOptSamplesQualityIndex;";
			$cmds[size($cmds)] = "setAttr (\".stringOptions[\" + $gMiStringOptSamplesQualityIndex + \"].value\") -type \"string\" \"0.01 0.01 0.01 0.01\";";
			$cmds[size($cmds)] = "global int $gMiStringOptSamplesMinIndex;";
			$cmds[size($cmds)] = "setAttr (\".stringOptions[\" + $gMiStringOptSamplesMinIndex + \"].value\") -type \"string\" \"0.5\";";
			$cmds[size($cmds)] = "global int $gMiStringOptSamplesMaxIndex;";
			$cmds[size($cmds)] = "setAttr (\".stringOptions[\" + $gMiStringOptSamplesMaxIndex + \"].value\") -type \"string\" \"100\";";
		break;

		case "Production":
			$cmds[size($cmds)] = "global int $gMiStringOptUnifiedSamplingIndex;";
			$cmds[size($cmds)] = "setAttr (\".stringOptions[\" + $gMiStringOptUnifiedSamplingIndex + \"].value\") -type \"string\" \"true\";";
			$cmds[size($cmds)] = "global int $gMiStringOptSamplesQualityIndex;";
			$cmds[size($cmds)] = "setAttr (\".stringOptions[\" + $gMiStringOptSamplesQualityIndex + \"].value\") -type \"string\" \"0.6 0.6 0.6 0.6\";";
			$cmds[size($cmds)] = "global int $gMiStringOptSamplesMinIndex;";
			$cmds[size($cmds)] = "setAttr (\".stringOptions[\" + $gMiStringOptSamplesMinIndex + \"].value\") -type \"string\" \"1\";";
			$cmds[size($cmds)] = "global int $gMiStringOptSamplesMaxIndex;";
			$cmds[size($cmds)] = "setAttr (\".stringOptions[\" + $gMiStringOptSamplesMaxIndex + \"].value\") -type \"string\" \"100\";";
		break;

		case "FinalFrameEXR":
			$cmds[size($cmds)] = "global int $gMiStringOptUnifiedSamplingIndex;";
			$cmds[size($cmds)] = "setAttr (\".stringOptions[\" + $gMiStringOptUnifiedSamplingIndex + \"].value\") -type \"string\" \"true\";";
			$cmds[size($cmds)] = "global int $gMiStringOptSamplesQualityIndex;";
			$cmds[size($cmds)] = "setAttr (\".stringOptions[\" + $gMiStringOptSamplesQualityIndex + \"].value\") -type \"string\" \"1.5 1.5 1.5 1.5\";";
			$cmds[size($cmds)] = "global int $gMiStringOptSamplesMinIndex;";
			$cmds[size($cmds)] = "setAttr (\".stringOptions[\" + $gMiStringOptSamplesMinIndex + \"].value\") -type \"string\" \"1\";";
			$cmds[size($cmds)] = "global int $gMiStringOptSamplesMaxIndex;";
			$cmds[size($cmds)] = "setAttr (\".stringOptions[\" + $gMiStringOptSamplesMaxIndex + \"].value\") -type \"string\" \"250\";";
		break;
	}

	return $cmds;
}

global proc string[] miApplyPresetToNodeCustom(string $node, string $preset)
{
	string $cmds[];
	string $nType = `nodeType $node`;
	switch ($nType)
	{
		case "renderGlobals":
		break;

		case "mentalrayOptions":
			$cmds = miApplyMentalRayOptionsPresetsCustom($preset);
		break;

		case "mentalrayGlobals":
		break;

		case "mentalrayFramebuffer":
		break;
	}
	return $cmds;
}

//
// Procedure to generate the default presets for the render globals window
// if they have not already been created. Presets are stored in the user prefs
// and are managed using the nodePreset MEL command.
//
// This method is called as part of plugin initialization from MayaPlugin.cpp.
//
global proc miCreateDefaultPresets()
{
	// Sanity check
	if (!`renderer -exists mentalRay`) return;

	// ************************ MAYA-20369 PART 1 of 2 *************************
	// Workaround for dirtying the scene during plugin load. MAYA-23559
	// adds an assertion for plugins that leave the scene dirty after
	// load. To fix this properly, Mental Ray would need to be able to
	// create its own default nodes. There is no API for this yet.
	// But it has been logged as MAYA-23578. This workaround can be removed
	// when MAYA-23578 is fixed. This is a hack :( But one we hope to clean up
	// soon!
	int $sceneDirty = `file -q -modified`;
	// ************************ END MAYA-20369 PART 1 of 2 *********************

	miCreateDefaultNodes();

	// Make sure tab utils are loaded to prevent errors from callbacks
	// if they happen before the render globals UI is created
	source createMentalRayTabsUtils;

	// Preset data
	string $PresetNames[] =
	{
		"Preview",
		"Production",
		"FinalFrameEXR"
	};

	// Get globals nodes and create the presets
	string $globalsNodes[] = `renderer -q -globalsNodes "mentalRay"`;
	for ($preset in $PresetNames)
	{
		// Skip existing presets
		if (`nodePreset -exists $globalsNodes[0] $preset`) continue;

		int $i;
		for ($i=0; $i<size($globalsNodes); $i++)
		{
			string $node = $globalsNodes[$i];
			if (`size $node` > 0 && !catchQuiet(`nodeType $node`))
			{
				string $nType = `nodeType $node`;

				// Not all presets need to store data for all globals nodes
				if ($nType == "resolution" ||
					($preset != "FinalFrameEXR" &&
					 $nType  != "mentalrayOptions" &&
					 $nType  != "mentalrayGlobals"))
				{
					continue;
				}

				// Set up attribute list for node for preset
				string $presetAttrs = "";
				int $useCustom = false;
				switch ($nType)
				{
					case "renderGlobals":
						$presetAttrs = "imageFormat imfPluginKey imfkey";
					break;

					case "mentalrayOptions":
						$presetAttrs = ("filter filterWidth filterHeight filter " +
							"filterWidth filterHeight maxReflectionRays maxRefractionRays " +
							"maxRayDepth maxShadowRayDepth filter filterWidth filterHeight " +
							"maxReflectionRays maxRefractionRays maxRayDepth " +
							"maxShadowRayDepth shadowMethod sampleLock " +
							"miRenderUsing miSamplesQualityR miSamplesQualityG miSamplesQualityB " + 
							"miSamplesQualityA miSamplesMin miSamplesMax");
						$useCustom = true;
					break;

					case "mentalrayGlobals":
						$presetAttrs = "caching";
						if ($preset == "FinalFrameEXR")
						{
							$presetAttrs += " compressionQuality imageCompression";
						}
					break;

					case "mentalrayFramebuffer":
						$presetAttrs = "datatype";
					break;
				}

				// Create a new instance of the node type of the current global
				// node. This will give it default values. Then we can modify
				// the defaults and store the result as a preset.
				string $newNode;
				if (!catchQuiet($newNode = `createNode -skipSelect $nType`))
				{
					// Alter the static attributes on the new node according to
					// the preset, nodePreset will save them.
					miApplyPresetToNode($newNode, $preset);

					// Build nodePreset command using preset attributes and the
					// custom flag.
					string $cmd = "nodePreset";
					$cmd += (" -attributes \"" + $presetAttrs + "\"");
					if ($useCustom)
					{
						$cmd += (" -custom \"" + encodeString("miApplyPresetToNodeCustom(\"#nodeName\", \"#presetName\")") + "\"");
					}
					$cmd += (" -save " + $newNode + " \"" + $preset + "\"");
					eval($cmd);

					// Delete the newly created node.
					delete $newNode;
				}
			}
		}
	}

	// ************************ MAYA-20369 PART 2 of 2 *************************
	// If the scene was clean before we entered miCreateDefaulPresets and it is
	// dirty now, set it to clean. The creation of the default nodes and/or
	// setting the presets would have marked the scene dirty.
	if ( !$sceneDirty) {
		file -modified 0;
	}
	// ************************ END MAYA-20369 PART 2 of 2 *********************
}
