| Very introductory Maxscript tutorial 2 |
Dub. |
Hi,
We're going to step it up a level in this tutorial This one should take about 10 mins to complete.
Intro
We will be covering four main areas.
1: Variables and data types 2: Loops 3: Comparison Operators 4: If statements
Variables
You can think of variables in MXS (maxscript) as a temporary named storage place for an "object" of some sort. think of them like lockers at the swimming pool. there are a huge number of them available (unlimited) and they all have nothing in them and are unnamed. Once you put something in a locker (variable) - it might be a number, a piece of text or even a modifier - and give it a name it will always be in there until you replace it with something else.
To set a variable in maxscript you simply type the following:
variableName = variableValue
The variable name can be anything you like and the value can be almost anything as well. For now we will use a float which is a number with a decimal place
ourVariable = 34.5
if you enter that into the listener and execute it, it will return 34.5 now if you type an expression such as:
2 * ourVariable
Maxscript will return 69.0
The main data types in maxscript are the following:
Float - these are numbers will a decimal component to them ie 234.643 Integer - These numbers are whole numbers only ie 5 or 200 Boolean - true or false String - this is a piece of text. To set a string you need to enclose it in quote marks ie:
myStringVariable = "Maxscript sure is handy"
One last thing. You can convert different data types from one to another like this:
"35" as integer
the 35 is a string (because it has quote marks around it) but the "as integer" after it, turns it into an integer. Obviously "as string" and "as float" do as you would expect.
Loops
Loops are where maxscript becomes really powerful and useful. They enable maxscript to do repetitive tasks rather than you and the mouse.
Imagine you have built a scene with many objects. You have added a turbosmooth modifier to all the objects with an iteration value of 2. This is now causing you problems because the scene is too slow to navigate and you wish you had set the iterations to 1 with a rendertime setting of 2.
Here is the code:
for obj in $ do ( obj.modifiers[#turbosmooth].iterations = 1 obj.modifiers[#turbosmooth].useRenderIterations = true obj.modifiers[#turbosmooth].renderIterations = 2 )
We will now look at it line by line.
for obj in $ do - Our for loop is saying in plain English: "For the objects in our current selection, do the commands inside the following brackets."
( - We open the brackets obj.modifiers[#turbosmooth].iterations = 1 - we set the iterations to 1
obj.modifiers[#turbosmooth].useRenderIterations = true obj.modifiers[#turbosmooth].renderIterations = 2 - these lines should be obvious to you by now.
) - We close the brackets to end the for loop.
Max will keep looping through the code on every object in the selection until it has done them all.
You can test the code by making a bunch of objects all with turbosmooth on them. THEY MUST ALL HAVE A TURBOSMOOTH MODIFIER. if one object doesn't have a turbosmooth modifier, when MXS tries to talk to it, it won't find it and your script will fail with an error. This obviously isn't very satisfactory.
Comparison operators
We can solve this problem by adding an "If" statement to the code
But first of all we'll look at some comparison operators. if we have two items such as the numbers 5 and 2, we can compare them in different ways try typing the following into the listener:
5 == 2 -Is 5 equal to 2? -This returns False
5 != 2 -Is 5 NOT equal to 2? -This returns True
5 < 2 -Is 5 less than 2 -This returns False
5 > 2 -Is 5 greater than 2 -This returns True
There are more as well which you'll find useful in due course.
If Statements
The way if works is like this:
if (expression resulting in true or false) do ( code goes here )
Here is a sample which you should be able to read easily by now:
if (obj.modifiers[#turbosmooth] != undefined) do ( obj.modifiers[#turbosmooth].iterations = 1 obj.modifiers[#turbosmooth].useRenderIterations = true obj.modifiers[#turbosmooth].renderIterations = 2 )
That will check an object to make sure it has a turbosmooth modifier and then if it does, run through the code in the following brackets
Conclusion
Now when we put it all together, we get the following script:
for obj in $ do ( if (obj.modifiers[#turbosmooth] != undefined) do ( obj.modifiers[#turbosmooth].iterations = 1 obj.modifiers[#turbosmooth].useRenderIterations = true obj.modifiers[#turbosmooth].renderIterations = 2 ) )
It should happily run through every object in your current selection and if it has a turbosmooth modifier, set it to the values we specified.
Good luck! Any questions, ask here.
read 16306 times 11/21/2006 9:37:04 PM (last edit: 11/22/2006 1:30:35 PM)
|
paconavarro |
DUB this is great!!!
How about trying to persuade the MODs to get some kind of area for this tuts so the ones interested in MXS can ask and keep your effort organized and it does not gets lost in the middle of the posts
:)
---
http://www.exododw.com
read 16285 times 11/22/2006 12:14:10 AM (last edit: 11/22/2006 12:14:10 AM)
|
Weyu |
Link to the first one pleaseeeee! :D
__________________________________
Even the largest scene begins with a single mesh.
read 16278 times 11/22/2006 12:39:52 AM (last edit: 11/22/2006 12:39:52 AM)
|
lapenyeku |
wow, the for obj in $ thing is very cool! can you make these tutorials sticky? or atleast put their links in your sig?..
read 16268 times 11/22/2006 1:59:45 AM (last edit: 11/22/2006 1:59:45 AM)
|
-=LeadMagnet=- |
OOh OOh, *raises hand* I have a question.
Right, so if the variables are lockers at a swimming pool, that's fine for the floats and i also know that string floats, but won't the booleans and the integers sink?
 Do A Barrel Roll!!!
read 16264 times 11/22/2006 2:14:22 AM (last edit: 11/22/2006 2:14:22 AM)
|
Nik Clark |
Dub, if you create a Thread that contains links to these tutorials like an index, I'll make it a featured thread from which they can all be accessed.

read 16242 times 11/22/2006 3:23:25 AM (last edit: 11/22/2006 3:23:25 AM)
|
-=LeadMagnet=- |
i actually have a real question now though. Is there a way to use a different key besides numerical enter for running the code? 'Printscreen' for example?
The reason is that i'm using a laptop and the number pad shares keys with the regular keys. Toggled on and off using Fn+Ins. It's very awkward to need to switch the numpad on and off just to run the code.
 Do A Barrel Roll!!!
read 16237 times 11/22/2006 3:36:01 AM (last edit: 11/22/2006 3:36:01 AM)
|
urgaffel |
I'll be more than happy to host them if you print them out into html. This is invaluable. I can help you compile them into one big tutorial that you can mail to sites too, I know that Scriptspot is looking for scripting tuts, 3dtotal might give you a few texture cds for this etc etc.
Lead, you can hit Shift+Enter and it should work... Not sure :)
 All I remember was shaving... then *BLAM*
Environment art/gamesindustry blog thing
read 16220 times 11/22/2006 5:59:38 AM (last edit: 11/22/2006 6:04:38 AM)
|
-=LeadMagnet=- |
that works. Thanks.
 Do A Barrel Roll!!!
read 16212 times 11/22/2006 6:16:51 AM (last edit: 11/22/2006 6:16:51 AM)
|
horizon |
It works with regular enter here, whats the problem?
Two questions DUB 1. is there a difference between == and != at all? 2. why does the word undefined mean that there IS a certain modifier on?
read 16179 times 11/22/2006 1:19:51 PM (last edit: 11/22/2006 1:19:51 PM)
|
Dub. |
1: Yes there is a big difference!
== means "Is equal to" != means "Is NOT equal to"
2: So it follows that, if the turbosmooth modifier IS NOT undefined, then the script should perform the commands.
I suspect you might have missed reading carefully the comparison operators part. Have a look again and if it still doesn't make sense, I'll change it.
Rhys.
read 16174 times 11/22/2006 1:25:37 PM (last edit: 11/22/2006 1:25:37 PM)
|
horizon |
5 == 2 -Is 5 equal to 2? -This returns False
5 != 2 -Is 5 not equal to 2? -This returns True
ahh, missed that NOT in there
what about else function? or is that for the next one?
Great work DUB, greatly appreciated
read 16172 times 11/22/2006 1:27:46 PM (last edit: 11/22/2006 1:29:56 PM)
|
Dub. |
Fixed now :-)
I left the "else" out for now. I'm leaving a lot of stuff out to keep things really simple.
Fell free to add extra stuff yourself!
read 16165 times 11/22/2006 1:31:41 PM (last edit: 11/22/2006 1:31:41 PM)
|
horizon |
nah I know squat about maxscript, but for programming in general I at least know that with if comes else usually
read 16159 times 11/22/2006 1:35:45 PM (last edit: 11/22/2006 1:35:45 PM)
|
-=LeadMagnet=- |
"It works with regular enter here, whats the problem?" That only works for single lines and only when that line is the last line in the editor. To execute several lines of script at once you've got to select them and press numerical enter (or shift + enter). Pressing regular enter will delete the lines.
 Do A Barrel Roll!!!
read 16157 times 11/22/2006 1:39:55 PM (last edit: 11/22/2006 1:39:55 PM)
|