MaxScript Featured Example

Why I Love MaxScript

Why do I love MaxScript?

A lot of the 3D modelling work we do in Archviz can be tedious, frankly.

You’ve modelled 20 boxes, and forgotten to rename them as ‘wall01’ ‘wall02’ etc.

You’ve now renamed them all manually, but forgotten to give them a brick material, and then you’ve also forgotten to give them a 450cm by 450cm box UVW map modifier.

Ugh, you realise they now all need to be 240cm instead of 210cm high.

Onto the glazing, it would be nice to draw a rectangle, and then just click a button, adding a frame, the gasket, the cill and maybe even the glass itself, all with your favourite materials?

You’ve modelled each slab for a nice terrace outside, but it’s too regular and looks unnatural and ‘CG’. It would be great if each paving slab was tipped up slightly, and their respective UVW map offset and twisted using a unique UVW XForm modifier.

The nice outdoor chairs you’ve bought from http://www.designconnected.com; it would be great if they weren’t so regular too, and could be nudged and rotated slightly randomly.

This is why I love MaxScript. 

 

All of the above, MaxScript will eat for breakfast, and ask for second breakfast.

For sure, it’s daunting, as is I expect, learning Latin. I don’t profess to know a huge amount, not to the level of the professional scripts you’ll find on ScriptSpot.com. 

I know plenty enough. Any amount you learn, even if it’s just a snippet, will help you immeasureably.

20 boxes. 

Select them. 

Open up the MaxScript Listener window (pressing F11 should work), and in the lower whitespace box, type in the below and hit return. (If there’s any text already present in the listener, right-click and select ‘clear all’)

for obj in $selection do (obj.name = uniquename “wall”)

OK. What’s all this then? What’s an ‘obj’ and what does ‘for .. in .. do’ mean? 

Why $selection? What are the brackets for?

‘Uniquename’ you can probably guess. It’s obviously a ‘3ds Max script thing’, and if you assumed it’s a command for giving a Max object a unique name in the format ‘thing001’ or ‘thing027’ (if there’s already 26 things), you have guessed right.

The for .. in bit, not the easiest thing to explain if you are completely new to scripting or any form of computer programming. 

Here goes.

Imagine you have some coins in your pocket, you take each one out, give it a quick rub, and put it back, one by one. You are working through the coins in your pocket, and doing the same thing to each, before returning each one back into your pocket. 

This can be thought of as ‘iterating’ through objects in a list, the list being in this case, the loose change in your pocket. For a bit of further sophistication, every each coin you pull out of your pocket, you call it a quick temporary name, Daphne, coin, or indeed obj (is obj pronouncable?). Don’t overthink this temporary naming bit.

OK, let me try and write this giving the coins in your pocket a quick rub proceedure in a vague Mascript-y way.

for coin_that_I_just_got _out in my_pocket do (give_it_a_quick_rub)

I appreciate the grammar in the above is off, it would read better as from my_pocket and the brackets look superfluous, but this is MaxScript remember.
Bear in mind that coin_that_I_just_got _out in my_pocket could be a 10p piece, a 20p or if you’re lucky, a £2 piece, but it doesn’t matter – you give them a quick rub regardless, each and every one, and of course you put them back into your pocket, (although we didn’t need to include that in the above).

This is the essence of the MaxScript for.. in.. do..(stuff) routine. 

For each item in a list, work through the list and do the same task to each item in the list.

for coin_that_I_just_got _out in my_pocket do (give_it_a_quick_rub)

for obj in $selection do (obj.name = uniquename “wall”)

So, let’s look again at the proper MaxScript immediately above.

for obj in $selection do (stuff)

obj? Think back to calling each coin a name, Daphne or coin for example

for coin in $selection do (stuff to coin).

obj or coin, or indeed Daphne? Doesn’t matter. I just decided to use obj as it’s quick (less letters to type), and it’s kinda like ‘object’. 

Some people use i.
for i in $selection do (stuff to i)

Moving onto $selection.

The dollar sign, $ is very, very important in MaxScript. 

 

It allows MaxScript to grab hold of the objects in your scene.

$Box001
$Box001.height
$Camera001
$Camera001.fov
$Camera001.target.position
$Camera001.target.position.Z

$selection though? Everything you have currently selected! 

 

if you have 17 boxes currently selected, and then wanted them to all have the same height of 240cm, then type this and hit return:

for obj in $selection do (obj.height = 240)

Now this bit is important – MaxScript works using the system units, so please bear that in mind.

obj.height = 240

Why the equals sign? 

MaxScript is a 2 way street, by typing the above, you are changing the box’s height to 240. If was already 240cm in height, doesn’t matter, MaxScript will make its height 240cm again.

If you wanted to find out what a box’s current height is, say box017, then type the line shown below into the listener, don’t forget the $ sign, and hit return.

$Box017.height

The listener will report back, in blue type, box017’s current height value. Awesome.

Back to this.

for obj in $selection do (obj.name = uniquename “wall”) and specfically this bit,
(obj.name = uniquename “wall”).

This is the ‘do stuff’ bit.

 

Objects in 3ds Max have a name, right? $Box017’s name is, unsurprisingly “Box017”.

Let’s change it to Daphne!
$Box017.name = “Daphne”
Super.

$Box017.height = 240
– Unknown property: “height” in undefined
MAXScript callstack:
— thread data: threadID:21836
— ——————————————————
— [stack level: 0]
— In top-level

Wait, what? Where’s Box017 gone ?

No, Box017 hasn’t been deleted. Box017 now answers to the name Daphne (and why not?)

$Daphne.height = 240

This is important, if you change the name of a Max object, then going forwards, you’ll have to address it personally with its new name.

We’re nearly there.

 

for obj in $selection do (obj.name = uniquename “wall”)

uniquename as mentioned earlier is indeed a special MaxScript fragment of code or syntax that gives an object an individual-ised new name, starting with the prefix you supply immediately after. 

The ” ” are important. 

 

Numbers are 1, 2, 3.142 and words, letters, characters are “words”.

In MaxScript they are actually designated as strings. A string of characters.

Typing in 1 + 1 into the MaxScript listener, and hitting return will get you the answer 2

Typing 1.2 + 1.3 will get you the answer 2.5. Clever stuff.

Try typing in big + dog

Nope, Maxscript doesn’t like it.

Try this –

“big” + “dog”

The MaxScript listener is happy with this and answers “bigdog”, in blue.

Names, sentences are strings in MaxScript and you must include quotation marks when defining them. 

This is all getting a bit ‘computer programming-y’ and I wanted to keep it light, so let’s finish things up.

for obj in $selection do (obj.name = uniquename “wall”)

This hopefully is making sense now? 

 

Why the brackets though? 

Well, they’re actually not strictly necessary for the single ‘one-liner’ MaxScript example above. 

But what if wanted to do more than one thing to each thing in a list? 

Can you..?

 

You’ve modelled 20 boxes, and forgotten to rename them as ‘wall01’ ‘wall02’; their material should be the nice brick material you’ve already applied to another object in the scene, and they should all be 240cm in height.

Select the 20 boxes.

for obj in $selection do
(
obj.name = uniquename “wall”
obj.height = 240
obj.material = sceneMaterials[“brick”]
)

This has been a lot to get through, and there are some abstract and advanced concepts in all the above. 

But if you understood it, you pretty much understand a great deal of MaxScript.

 

Once you start wrting your own scripts, you’ll naturally want to save and re-use them. 

To do this, rather than use the MaxScript Listener, you would create your own scripts via the Scripting dropdown menu on the 3DS Max top menu, and chose ‘New Script’ to create an empty one. 

The syntax is exactly the same, and you can save and open scripts using File and Save as normal. 

To test or run your scripts, chose the Tools dropdown menu in the MAXScript window, and select Evaluate All. 

It’s handy to have the MaxScript Listener window open too, as this will give feedback, and error messages.

 

Error messages.

 

Don’t panic, you will have tons of error messages. 

Often it’s because of your spelling mistakes. 

Other times, it can be that you’re trying to change a property of an object that doesn’t exist, the property I mean. For example, a Box in 3ds Max has the properties length, width, height, position. But it doesn’t have the property ‘radius’. it’s not a circle after all.

So, $Box017.radius = 20 won’t work.

Of course, always save your scene before delving into MaxScript. Just in case.

I hope this has been a useful introduction to MaxScript. I picked something that you would actually use, iterating through objects and renaming them to something more appropriate.

I will be going into more detail over the coming months, and please feel free to ask for any MaxScript areas you’d like me to cover in the meantime. 

I’ll try my best to answer, maybe but don’t ask me about Matrix Transformations just yet.. Ugh

If the above has been useful, please feel to share using the icons below!

 
Facebook
Twitter
LinkedIn