MaxScript Beginner - 04 Basics, files

MaxScript Beginner - 04 Basics, file handling

creating a filepath, concatenating / adding two strings

this method of just adding strings together can often bite you in production. It is better to use some methods that check that the filepath is more valid and you do not accidentally add more slashes. But first here is a classic String concatination.

--to get ONE "\" in the end you need to write "\\"
TheFirstString = "c:\\temp"
TheSecondString = "myFile.max"

--Add them together

newString = ( TheFirstString + TheSecondString )

--result: newString = "c:\tempmyFile.max"
--NOT a VERY good filePath...
--remember to add the \\ when needed..

newString = ( TheFirstString + "\\" + TheSecondString )
--result: newString = "c:\temp\myFile.max"

A safer way to add two paths

Appends the second argument filename to the first argument path, adding backslash if necessary

-- pathConfig.appendPath <path1> <path2> 

pathConfig.appendPath "c:\\temp" "test.txt"
"c:\temp\test.txt"
pathConfig.appendPath "c:\\temp\\" "test"
"c:\temp\test"

The pathconfig struct contains A lot of good methods to work with filepaths

read more about the pathconfig struct here

Working with filePaths & strings: Filterstring

-- pathConfig.appendPath <path1> <path2> 

MyPath = "c:\\dir1\\dir2\\dir3\\test_Diffuse.png"
MyPathArray = filterstring MyPath "\\"

for s in MyPathArray do ( print s )

--Get the name of the Last Folder
MyPathArray[4]

-- This always takes the Last dir
-- Even if it is a long path
MyPathArray[MyPathArray.count - 1]

How to find files from disk

--gets all files in a folder
TheFiles = getfiles "c:\\temp\\*"
for f in TheFiles do (print f )

--gets all png files in a folder
TheFiles = getfiles "c:\\temp\\*.png"
for f in TheFiles do (print f )

Load or Merge a max file

--To reset max from maxscript is very useful when
--Batching alot of files, #noPrompts forces max to 
--Just reset without warnings...
resetMaxFile #noPrompt


--if you want to load a maxfile from disk
loadMaxFile "c:\\temp\\myFile.max"

--if you want to merge.. there is many options that you can use
--the #select option selects the merged objects so that you can
--store the selection in a variable
mergeMaxfile "c:\\temp\\myFile.max" #select
StoreSelection = selection as array --store the merged objects

--for example if you know the name of some objects in the scene
--you merge from you can add an array and only merge them
mergeMaxfile "c:\\temp\\myFile.max" #("box01","box02","box03") #select

Update texture paths in many maxfiles

This is a script that updates all texturepaths in an array of maxfiles

/*
IMPORTANT to know... this ONLY works on maxfiles 
SAVED with 3dsmax 2010 or newer.. Older maxfiles 
will just return an empty array...

solution is to open an old maxfile in a new version 
of max, and save it from the newer max
*/

--This is a small function that updates the texture path
fn UpdateTexturePath filename newTexturePath = (
  --Get all fileAssets in a maxfile and store in an array
  fileAssets = getMAXFileAssetMetadata filename

  for fa in fileAssets do ( 

--for every fa get just the filename and filetype..
  fname = (getfilenamefile fa.filename + getfilenametype fa.filename)

--Update the old texturePath with newTexturePath + old filename
  fa.filename = newTexturePath + fname
  )

--this write the information back to the file
setMAXFileAssetMetadata filename fileAssets	
)


--This is an example when I update the texturePaths 
--on all models in the modelbank

CarDirs = getDirectories "\\\\TheNet\\share-01\\ModelBank\\Cars\\*"
TheNewTextureRoot = "x:\\myTextures\\"

for c = 1 to carDirs.count do (
	carDir = carDirs[c]
	maxFiles = getFiles ("*.max")
	for mxfile in maxfiles do (UpdateTexturePath mxfile TheNewTextureRoot )
	print carDir
)