Doing complicated things in lots of files in Max (solution)

Here is a solution we came up with a while ago, but someone on Polycount had a related question so I figured I may as well detail it, since I am really happy with it.

Doing the same thing over a bunch of Max files is not a big deal. If you don’t have a way to do it already, you can download Paul Neale’s Batch It Max. It is an extremely useful tool if you don’t have something similar already.

What we needed to solve was, we had to open dozens of large files (80+ megs, most of which was animation data), load animations (this is using PuppetShop so they are all stored on sets of controllers, not externally), then export them. After a handful of files, Max (both 8 and 2008) would run out of memory and crash, no matter how much garbage collection or memory management we tried to do.

So we wrote a script that did the following:
You’d choose a directory to load, whether you load its subdirectories, or you could load files from a text file, whatever- this is simple and I will put it to the reader how they want to get a list of files to batch. You then choose a script that you will run on each file. I’ll give an example script below since it needs some explanation. When you ‘execute’, you copy the script into the Scripts/Startup directory, and use a DOSCommand to load the max file with the ‘-silent’ option… this will disable any ‘Missing External File’ dialog nonsense. So it will look like:

DOSCommand ("\"C:\\Program Files\\Autodesk\\3ds Max 2008\\3dsmax.exe\" " + File + " -silent")

Now, in most cases this will be all you need to do. However, your script will try to run as soon as it is loaded. If it references other scripts that are loaded AFTER the batch script, the script will crash, because Max has not evaluated them yet. For more information, see “Startup Scripts” in MAXScript help. It will explain what is going on behind the scenes when Max loads. Also see [w]Macro Installation (MAXScript)[/w] on the Wiki.

So, the batch script. A skeleton script will explain itself:


fn batchFn =
(
	--if you want, set up some logging... I'd recommend it
	for o in objects do
	(
		someStruct.doSomething o
	)
	--close the log, save the file, whatever
	quitMAX #noPrompt
)

callbacks.addScript #filePostOpen "batchFn()"

loadMaxFile (maxFilePath + maxFileName) quiet:true

So when you load the file, you create a callback that will fire when you load a new file. You then REload the file, which will fire the function off. The difference is, this time, all your scripts will be loaded, and you won’t get dependency problems (such as if the someStruct .ms file was evaluated after my startup script… when I reload the file, it will have been evaluated).

I hope that helps someone.

I should add, this is a pretty tried and true process. We’ve been doing this for many months and have transferred over all animation onto new/replacement rigs a handful of times using it. Writing the batch scripts normally takes a few hours to get right. The downside is you can’t start a new version of Max or the batch will run, though I suppose you can add an ‘if not keyboard.escPressed’ to determine whether to add the callback/reload the file or not to get around this. But this is batching, so you should be doing it overnight/on another machine.

I’ve ran into the same problem of Max just getting messed up when trying to batch process a lot of files (we have a process for batch exporting all meshes from ~1000 max files, each max file outputting multiple meshes etc).

At some point Max just started to export out garbage, and restarting Max always fixed the problem. However, Max does always take a while to start up…

So basically I made a script which exports meshes in a chunk of 20 Max files per session. It initially makes a list of all *.max files to be exported, dumps that to a file, then starts going thru the list. First export Max files 1 to 20. Then quit max. Then export Max files from 21 to 40. Etc… Pretty straight forward, there was just some extra steps to make to make sure the system recovers from erros, like the status of the export que when you happen to run into a .max file that just crashes Max when trying to export it… :slight_smile:

Rob btw, all the MSX code formatting quotes on this site seem really hard to read - it seems like the background for the formatting whould be very bright instead of the current dark gray?

SamiV.