Here is a list of practical examples for the syntax of scripting in 3ds Max with it’s internal language, maxscript. This isn’t a tutorial and HOWTO script, but more of a reference of how to format commands.
I’ll continue to update this post as I go on as it’s a resource for me as much as anyone else, let me know if there are any useful additions!
Note also that the indenting isn’t necessary but I think it’s a good habit (and I mostly use Python which requires it)
Arrays
/* Create an array */ oFruitArray = #("Apple","Orange") /* Append to an array */ append oFruitArray "Banana" /* Loop through an array */ for oFruit in oFruitArray do ( print oFruit ) /* Arrays start from 1 in maxscript */ print oFruitArray[1] -- returns "Apple" |
Strings
/* Substring - cut characters from a string */ oString = "String" substring oString 2 3 /* returns "tri" */ -- It's often useful to remove extensions oFile = maxFileName oFileNoExt = substring maxFileName 1 (oFile.count-4) |
Scene
/* Scene Path (read only)*/ oFilePath = maxFilePath /* Scene Filename (read only) */ oFileName = maxFileName /* Render Ouput File Name */ rendOutputFilename = "C:/Renders/Render.exr" |
Objects
/* lists all the properties of the object, very useful */ showProperies $ /* Selected objects */ $ /* Check the type/class of an object */ classof $ /* Change radius of selected spheres */ $.radius = 10 /* Selection FOR loop */ for oObj in $ do ( print oObj ) /* Another method */ for obj in selection do ( print oObj ) |
Modifiers
/* Accessing noise controller on a modifier */ $.Bend.BendAngle.controller.seed = 541274 -- Create an empty array oDisplaceExists = #() -- For loop through selected objects for oObj in $ do ( -- Get the top modifier oMod = oObj.modifiers[1] -- Get the class of the modifier oModClass = classof oMod if (oModClass == VRayDisplacementMod) then ( /* Append to an array */ append oDisplaceExists oObj print (oObj.name + " doesn't have a vray disp") ) else ( print (oObj.name + " has a vray disp") ) ) -- For loop for those with a vray disp modifier on top for oArrayObj in oDisplaceExists do ( print oArrayObj.name ) |
Interaction
/* Message box to give user some info */ messageBox "Asset name field is empty! Not saving.." |
Macroscript loader
Useful to load at startup, points at an updateable script
macroScript AnalogSave category:"Lemongood" toolTip:"LemonSave" ( filein "\\\\Server\\share\\Workgroups\\3dsMax_Workgroup\\MACROS\\LemonSave_v1.01.mcr" ) |
Misc
/* Random */ random 1 99999 /* Create a box at every position of selected objects */ for oObj in $ do ( oPosition = oObj.pos oCreateBox = Box pos: oPosition ) /* try catch to pick up errors */ oName = "Hello" try ( oName = (oJobIndex as integer) ) catch ( ) /* Loop through all the RPManager controls */ for i=1 to 90 do ( oNum = i as string ooP = (oNum + ": ") vals = rpmdata.RMGetValues i 1 valsstring = vals as string print (ooP + valsstring) ) |
System access
In windows…
/* Get user or computer name */ sysInfo.username sysInfo.computername /* Get local time and date */ localTime -- returns "13/07/2012 14:33:11" /* Recursively create directories*/ makedir ("c:/whatever")) /* Create a text file and write a line to it */ oFilePath = "C:/Hello.txt" oTextFile = createFile oFilePath /* Open the file for editting */ oOpenTextFile = openFile oFilePath mode:"at" /* Write a line to the text file*/ oWriteLine = "This is a line in a text file" format "%\n" oWriteLine to:oOpenTextFile /* Close the file */ close oOpenTextFile close oTextFile /* Open that text file and read the line */ oOpenTextFile = openFile oFilePath mode:"r" oReadLine = readLine oOpenTextFile close(oOpenTextFile) |
