// Copyright (C) 1997-2004 Alias Systems Corp.
// 
// The information in this file is provided for the exclusive use of the
// licensees of Alias.  Such users have the right to use, modify,
// and incorporate this code into other products for purposes authorized
// by the Alias license agreement, without fee.
// 
// ALIAS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
// EVENT SHALL ALIAS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
// DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

//
//  Alias Script File
//  MODIFY THIS AT YOUR OWN RISK
//
//  Date:  	March 2004
//
// Description: 
//		Procedure for creating and attaching a diskCache
//		node to the fluidShape passed in.  Fluid disk caches
//		come in two varieties: Playback caches (with an
//		"mcfp" extension) and Initial State caches (with an
//		"mcfi" extension).  Furthermore, all diskCaches have
//		a ".cacheName" attribute which must eventually be set
//		with a string value representing the path to the physical
//		file containing the cache data for your fluid.  
//		
//		Is is the value of $cacheTypeOrFilePath that
//		determines which type of fluid diskCache is created
//		by this procedure.  The value passed in can be
//		either a cacheType (like "mcfi" or "mcfp") or a path
//		to the cache data file (like
//		"C:/myScene.mb_fluidShape1.mcfi").  (It is up to the
//		caller to ensure that the data in the specified
//		cache file is compatible with the specified fluid's
//		resolution.)  If a path is specified, the new
//		diskCache's .cacheName attribute will be updated
//		with the location of the cache data file.
//		Otherwise, it is assumed that the .cacheName
//		attribute will be set properly elsewhere.
//
//		Some examples: 
//			
//			// Create a new Playback cache and attach it to fluidShape1.
//			// There is no data file yet for this cache.
//			// 
//			createAndAttachFluidDiskCache( "fluidShape1", "mcfp" );
//			
//			// Create a new Initial State cache and attach it to fluidShape1.
//			// There is no data file yet for this cache.  (Note that
//			// it doesn't make a difference whether the "." is specified
//			// before the file extension/cache type or not.)
//			// 
//			createAndAttachFluidDiskCache( "fluidShape1", ".mcfi" );
//			
//			// Create a new Initial State cache and attach it to fluidShape1.
//			// The fluid's Initial State data comes from the absolute path
//			// specified.
//			// 
//			createAndAttachFluidDiskCache( "fluidShape1", "C:/bug.mb_fluid1_fred.mcfi" );
//			
//			// Create a new Playback cache and attach it to fluidShape1.
//			// The fluid's Playback data comes from the project-relative
//			// path specified.
//			// 
//			createAndAttachFluidDiskCache( "fluidShape1", "data/bug.mb_fluid1_fred.mcfp" );
//
//
proc debugPrint( string $printThis ) {
	// print( " DEBUG: " + $printThis + "\n" );
}


global proc string createAndAttachFluidDiskCache( string $fluid, 
												  string $cacheTypeOrFilePath )
{
	// Find out what cacheType we're working with: mcfi or mcfp?
	//
	string $nameBits[];
	int    $isFilePath = false;
	
	int $numTokens = tokenize( $cacheTypeOrFilePath, ".", $nameBits );
	if( $numTokens > 1 ) {
		$isFilePath = true;
	}
	string $extension = $nameBits[$numTokens - 1];

	// Based on extension/cacheType, we know which fluid attribute to
	// connect it to.
	//
	string $fluidAttr;
	string $nodeNamePrefix;

	if( $extension == "mcfi" ) {
		$fluidAttr = ".diskCacheIC";
		$nodeNamePrefix = "initialState_";
		debugPrint( "createAndAttachFluidDiskCache: mcfi -- " + $cacheTypeOrFilePath );
	} else if( $extension == "mcfp" ) {
		$fluidAttr = ".diskCache";
		$nodeNamePrefix = "cache_";
		debugPrint( "createAndAttachFluidDiskCache: mcfp -- " + $cacheTypeOrFilePath );
	} else {
		debugPrint( "createAndAttachFluidDiskCache: wrong type" );
		if( $isFilePath ) {
			error( "Cache file " + $cacheTypeOrFilePath + " does not have a valid extension for " +
				   "a fluid cache file.  Valid extensions \'mcfp\' or \'mcfi\'." );
		} else {
			error( "Invalid cache type specified.  Valid types are \'mcfp\' and \'mcfi\'." );
		}
		return "";
	}

	// Find a unique name, create the cache node, setup its attributes
	// and connect it to the fluid.
	//
	string $cacheName = uniqueCacheName($fluid, ("." + $extension));
	string $diskCache = `createNode -n ($nodeNamePrefix+$fluid) diskCache`; 
	setAttr -type "string" ($diskCache + ".hiddenCacheName") $cacheName;
	setAttr -type "string" ($diskCache + ".cacheType") $extension;

	debugPrint( "$cacheName = " + $cacheName );
	debugPrint( "$diskCache = " + $diskCache );
	debugPrint( "$extension = " + $extension );
	debugPrint( "$isFilePath = " + $isFilePath );
	debugPrint( "$fluidAttr = " + $fluidAttr );

	// Only set this attribute value if we got a real filename passed
	// in as our cache's data file.
	//
	if( $isFilePath ) {
		setAttr -type "string" ($diskCache + ".cacheName") $cacheTypeOrFilePath;
	}

	// Connect the diskCache node to the appropriate attribute on the 
	// fluid shape.  (The attribute depends on the cacheType.)
	//
	connectAttr ($diskCache + ".diskCache") ($fluid + $fluidAttr );

	return $diskCache;
}
