/*
Copyright (C) 2010  Ari Sachter-Zeltzer

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 3o30, Boston, MA  02111-1307, USA.

ari@shadowplaystudio.com
*/

(
	/*
	
	This sample script is designed to illustrate how to procedurally cache TP systems. For the purposes of this script
	we will assume that this is intended for use in slave mode ( max is being run with the 3dsmaxcmd.exe command ).	
	This will allow you to cache TP in an unattended fashion and not use a 3dsmax workstation license. This will, however,
	require a TP Simulation license.
	
	Here's the command to use this script with 3dsmaxcmd.exe:
	
	3dsmaxcmd.exe -script <this script> -v:5 <the file you want to cache>
	
	*3dsmaxcmd.exe is located in the root of your max directory
	
	---	
	If you want to run this script in workstation mode for testing, you should comment out the last line 'quitMax #noPrompt'	
	*/
	
	if maxFileName != "" then
	(
		/*
		if our scene has a name - ie, its not an unsaved scene, proceed...
		*/
		
		-- find all TP systems in the scene and collect them
		tpSystems = for o in objects where ( classOf o == Thinking ) collect o
		
		for curTP in tpSystems do
		(
			/*
			iterate through all TP systems in the scene. Record caches for the master dynamic sets if they do not
			have a cache filename assigned
			*/
			
			curMD = curTP.masterDynamic -- set the current master dynamic set into a local variable
				
			if curMD.recordFile == "" then
			(
				/*
				if there is no cache file assigned, we can assume it is safe to cache out this master dynamic
				*/
				
				cachePath = maxFilePath
				-- cachePath = @"\\server\path\to\your\cache" -- uncomment and adjust this line to place the cache somewhere on your network
				
				-- create our new cache filename. We'll name it after the TP system and save it in the same place as the scene file.
				newCacheFileName = cachePath + curTP.name + "_masterDynamic.tps"
				
				/*
				record the cache. Here is the maxScript command to call.
				<integer>CacheRecord filename <boolean>rendermode <boolean>redrawviews
				
				note we are recording with the rendermode and redrawviews options as false
				*/
				curMD.CacheRecord newCacheFileName false false
				curMD.Play = 0		
			)
			else
			(
				/*
				if a filename is present, we are going to skip caching this set. 
				If we wanted to add extra checking to see if the file actually existed, we could... but we're not going to now
				*/
			)
			
		)
		
		-- now that our work is done, lets quit max before it decides to render some frames...
		quitMax #noPrompt
	)
	else
	(
		/*
		the scene does not have a name, and is thus not a saved scene...
		*/
		
		format "This script should really be used on a scene that is saved...\n"
	)
)