Loading...
27 June, 2012#

3ds Max Maxscript Help: Snippets

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)
18 May, 2012#

3ds Max plugins

Always good to keep your scripts and plugins seperate from the main install IF YOU CAN (max can be a lot of trouble with this, other applications are set up purposely in a much nicer way)

In the plugin.ini file, add a path for your custom scripts, under
[Directories]

The plugin.ini file used to be located in the install directory, I believe it has been moved to the user folder in later releases. ( I use 2010 at work and tend to use Softimage at home so I’m not 100%)
C:\Program Files\Autodesk\3ds Max 2010\plugin.ini

[Directories]
sr_Scripts=C:\Users\admin\Dropbox\Workgroups\3dsMax_Workgroup
6 April, 2012#

Quick tip: 3dsmax Maths in a Spinner

You can do basic maths functions inside spinners! For example, to add 3.5 to a spinner value, select the value then type, ‘r3.5′

Or for more useful stuff, pressing CTRL+N bring up a calculator!