Forum Groups
  All forums
    Help & Feedback
      Questions
      Work in progress
      Finished Art
      Non-Max related
    Community
      Offtopic
      News
    Hardware
    Photography


Featured Threads
  Speedmodeling 2011 - Holidays Edition
(117 replies)
  Maxforums Kiva Group
(57 replies)
  The Life of a Sea Turtle - Tutorial by mrgrotey
(121 replies)
  So what are you working on? =)
(2620 replies)
  Want free webspace?
(20 replies)
  spam alert!!!
(3918 replies)
  Maxforums member photo gallery index
(113 replies)
  Maxforums Member Tutorials
(86 replies)
  three cheers to maxforums...
(185 replies)
  The Grotorial Index Page
(57 replies)
  101 Things you didnt know in Max...
(185 replies)
  A Face tutorial from MDB101 :D
(95 replies)
  Free textures thread
(77 replies)
  Maxforums.org Members Gallery
(486 replies)
  SON OF POST YOURSELF
(626 replies)
  Dub's Maxscript Tutorial Index
(119 replies)
  The Best Art of Maxforums [56k warning]
(109 replies)


Very introductory Maxscript tutorial 4
show user profile  Dub.
Intro

In this tut we'll be covering 4 things. Rotations, Comments, Saving and running scripts
and making macroscripts.

Hopefully you still have your max file with the randomised fence posts from tut 3. Open it up.

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.


read 17577 times
11/25/2006 7:32:08 PM (last edit: 11/25/2006 10:36:02 PM)
show user profile  paconavarro
WOHOO!!!! Tnks again!

--- http://www.exododw.com

read 17570 times
11/25/2006 7:42:49 PM (last edit: 11/25/2006 7:42:49 PM)
show user profile  timpa
Dub you are a a legend.


Let me know if you want some PHP code to let you add these quickly and easily onto your site.

You need this to be permanent :)


read 17561 times
11/25/2006 10:12:47 PM (last edit: 11/25/2006 10:12:47 PM)
show user profile  horizon
Ok the next one I leave for tomorrow, I wandered in maxscript experimenting for too long and now am too tired for nex tut

Got a question though, I'll take a risk at slowly getting annoying hehe:

I wrote this;

var = 0
for obj in $ do
(
var = var + 5
obj.height = var
)

and ran it on 20 boxes i made with array tool, named Box01 to Box20
So it did what I wanted, making box01 5 units heigh, box02 10 units etc...
But...
it skipped Box14 and did a loop on it as a very last object, so it's height was 100

Why is that?



read 17477 times
11/27/2006 3:48:22 PM (last edit: 11/27/2006 3:48:22 PM)
show user profile  Dub.
Thats because the selection array isn't necesarily in order of name.

if you type:

selection

you'll probably see that Box14 is at the end

If you want things in a specific order you'll have to make your own array with the objects in the right order.

then you'd just go:

for obj in myOwnArray do
(
)

I'll leave it to you to figure out how to fill myOwnArray with the objects in the right order >:-)

If you have trouble I can suggest a couple of ways to do it.

Rhys.


read 17470 times
11/27/2006 3:59:10 PM (last edit: 11/27/2006 4:04:05 PM)
#Maxforums IRC
Open chat window


Support Maxforums.org