Maxscript Editor
From now on, we won't be running our main scripts from the listener. The listener is still useful for testing snippets of code before you incorporate it into your main script. Also you'll need to look at the listener to see stuff returned from the maxscript parser.
Click Maxscript>New Maxscript
Hopefully a blank page should present itself. We will work in here from now on.
Save your script. I suggest making two folders in your scripts folder. One for your random snippets of code that you've written for one-off tasks and the other for finished release quality scripts with proper error checking, documentation and interfaces etc.
Last time we finished with a script to randomly set the positions of the fenceposts. Now we want to modify the script so that we can randomly set their rotations.
Rotations
Rotations are slightly odd in Max in that we need to first create a "rotation object" that we will store as a variable. Then we apply our rotation object to our object (a fencepost in this case!)
As you possibly already know, in Max, there are 3 different types of rotations that you can use. Euler angles, Quaternions and Angleaxis. As you get more advanced, you will want to work with the different types in different scenarios but for now, we will use euler angles.
Eulerangles are defined in the following way: rot_obj = eulerangles x y z
create a cube somewhere in your scene and type the following into the listener:
cubeRotationObject = eulerangles 0 20 0
Maxscript should return: (eulerAngles 0 20 0)
Now we have our rotation object stored in our variable; cubeRotationObject.
All we need to do now is apply the rotation object to our object. Select the cube and type the following:
rotate $ cubeRotationObject
The cube should rotate 20 degrees around the Y axis.
Back to our script. Hopefully you should be ahead of me now with a clear idea how to modify the script to randomly rotate the posts. In case you're lost, here is the code: (this time, type it into our blank new maxscript file)
for obj in $ do
(
randXrot = random -3.0 3.0
randYrot = random -3.0 3.0
randZrot = random -3.0 3.0
rot_obj = eulerangles randXrot randYrot randZrot
rotate obj rot_obj
)
Select all the fence posts (or if you don't have the file from tut3, create an array of fence posts and select them all) and run the maxscript by hitting CTRL+E (evaluate) All the posts should now have random heights, positions and rotations giving a much more organic look than what we started with.
Comments
Once your scripts grow beyond a few lines long, comments start to get really important so that you don't forget what parts of your code do what. Also if you need to give your code to anyone else, good comments will make things much easier for them. A comment is text notes inside your script that the Maxscript parser knows to ignore. You can make a comment by typing "--" Anything after the -- will be interpreted as a comment and is not executed. If you need to write more than one line, you'll need to put -- at the beginning of each line. Now go through and comment your random rotation script. You'll notice that the comments show as green.
for obj in $ do -- Loop over currently selected objects
(
randXrot = random -3.0 3.0 -- create a random X rotation value and store as a variable
randYrot = random -3.0 3.0 -- create a random Y rotation value and store as a variable
randZrot = random -3.0 3.0 -- create a random Z rotation value and store as a variable
rot_obj = eulerangles randXrot randYrot randZrot -- Build our rotation object and store
rotate obj rot_obj -- Apply the rotation to the current obj
)
Macroscripts
I'm now going to show you how to make your script into a macroscript that you can bind to a shortcutkey or set as a menu option or button inside max.
There is a function that we will be using strangely enough called macroscript. Here is a template:
MacroScript Script_Name category:"Category Name" buttonText:"Name of Button or Menu Item" tooltip:"Tooltip Name"
(
your script code goes here
)
Obviously you will need to substitute names of your choosing into it.
Here is what my final script looks like:
MacroScript Random_Rotate category:"Rhys Tools" buttonText:"Random Rotate" tooltip:"Random Rotate"
(
for obj in $ do -- Loop over currently selected objects
(
randXrot = random -3.0 3.0 -- create a random X rotation value and store as a variable
randYrot = random -3.0 3.0 -- create a random Y rotation value and store as a variable
randZrot = random -3.0 3.0 -- create a random Z rotation value and store as a variable
rot_obj = eulerangles randXrot randYrot randZrot -- Build our rotation object and store
rotate obj rot_obj -- Apply the rotation to the current obj
)
)
Save it and evaluate it (CTRL+E)
Maxscript will appear to do nothing and a number will be returned in the listener. This is good.
Now open Customize>Customize User Interface and under the category dropdown, there should be one labeled "Rhys Tools" (or whatever you decided to call yours) You can now make this a button or as I usually do, create a "Rhys Tools" menu bar item and add it there.
now whenever you click the item, it will randomise the rotations of whatever objects you have selected within +/- 3 degrees.
Conclusion
You may have realised that there are two fatal flaws. The first is that the script will crash if you have nothing selected as $ will be undefined. Secondly what if you want different values than the +/- 3 degrees hard coded into the script? We will deal with these things in the next tut.