Recursive Script critique

Hello All!

As I have a background from an art school, and therefore enjoy the abuse of critiques, I thought I would throw this little bit of script I’ve been working on and ask for suggestions to make it better/faster/cooler.

I’m kinda new to a lot of maxscript, so it seems to take me days to learn the really easy stuff, and when I do it usually is slapped together to meet some deadline, so I apologize if this is something the majority of you know. I had a difficult enough time with it that I thought I’d save other newer folks the headache.

Anyway, I needed a way to batch through our animations to save them onto a new rig, then export them in the proper format. The problem were that there were multiple sub-folders in the source directory, and I had to save them all to appropriate sub-folders in the destination directory, plus the Source and Dest directories would have to be user definable

Of course, max provides a good recursive script in the MXS reference, but I couldn’t find anything on how to save to sub-folders in a different directory.

After trying a whole slew of different things (including parsing the paths and rebuilding them, trying the configpath function, and ripping apart other batch scripts online) I hit upon the relatively simple idea of using the substituteString function, and with a bit more fiddling around, this is what I came up with:


sourceDir = "C:\\project\\art\\Char\\anims\\Source"
destDir = "C:\\project\\art\\Char\\anims\\Dest\\"

dir_Array = getdirectories ( sourceDir)

for d in dir_array do
	join dir_Array (getdirectories (d+"\\*"))

files = #()
	for f in dir_array do
	   join files (getfiles (f+"*.max"))

	For f in files do
	( loadmaxfile f quiet:true
		fileName = getfilenamefile f
		oldPath = getfilenamepath f
		newPath = substitutestring oldpath sourceDir destDir
		makeDir newPath
		savemaxfile (newpath + filename + ".max")
		
	)

the source and dest variables would end up being defined by text fields in the UI, plus the option to run other scripts/operations when the file opens.

Like I said before, I know this script’s been done to death, and every studio is bound to have some solution, but it’s the first time I’ve done it. What do you guys think?

Well, as long as it gets the job done there’s not too much one can say, but –

It doesn’t look as if anything is actually recursive in here. You’re manually traversing to the next level in each directory tree, and if you want to add another branch, you’ll have to hand-code it in.

Recursive functions usually call themselves, that’s why they are recursive.

eg:

fn recursiveGetDirectories root pattern =
(
local dirs = #()
local a = GetDirectories ( root + pattern )

join dirs a

for d in a do
(
	join dirs ( recursiveGetDirectories d pattern )
)
	
dirs

)

print ( recursiveGetDirectories @“C:\Riot Games” “*” )

Edit note: fixed to avoid “\” in results…

FYI, unless they’ve gone in and rewritten it lately the ‘recursive’ example in the MXS docs is not recursive. The function in that script never calls itself.

Man you guys are OVER THINKING things!

files = (DotNetClass “System.IO.Directory”).GetFiles @“C:\cygwin” “*.bvh” (DotNetClass “System.IO.SearchOption”).AllDirectories
–files is a regular MXS array!

[QUOTE=Rob Galanakis;6244]Man you guys are OVER THINKING things!

files = (DotNetClass “System.IO.Directory”).GetFiles @“C:\cygwin” “*.bvh” (DotNetClass “System.IO.SearchOption”).AllDirectories
–files is a regular MXS array![/QUOTE]

.Net + MXS for the win!:nod:

And given the relative performance differences between MXS and .NET Rob’s solution is probably much faster than a recursive MXS function.