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)


 
First page  Go to the previous page   [01]  [02]  Go to the next page  Last page
 
Very introductory Maxscript tutorial 5
show user profile  Dub.
Hi All,

Intro

In this tut we will be covering 4 things. Arrays, Error Checking, Messagebox prompts, and user interfaces.
This is the longest tut yet and will take about 1/2 hour to complete.
We left our random rotate script last time with two fatal flaws. 1, It crashes if there is no object selected and 2, there is also no way to specify the range of random rotation.

Arrays

First of all, I'd like to introduce a new concept - Arrays. If you're new to programming in general this will be a relatively meaningless term.
Put most simply, an array is just a list of "things". Almost anything that can be saved as a variable can be saved into an array. Lets make a quick array to get an idea about how they work.
First of all, we need to create an empty array with a name. Fire up the listener and input and execute the following:

ourFirstArray = #()

ourFirstArray is our arbitrary name we have chosen, # is the symbol for arrays in maxscript, and the () encloses the contents of our array which is nothing in this case. Thus we have just created an empty array.
Now we want to put something in our array. Maxscript has a useful little function called "append" which does just that. Usage is as follows:

append ourFirstArray 1

now type:

append ourFirstArray 3

now, type:

ourFirstArray

Maxscript will show us the contents of our array: #(1,3)

We can keep adding to the array as much as we like. But how do we retrieve a certain value from it? Each item in the list is numbered from 1 upwards. So to retrieve the second item in our array we need to ask for item 2 from ourFirstArray we do this by typing:

ourFirstArray[2]

Maxscript will return the integer 3 that we saved into the array earlier.

One last thing that you will find very useful with arrays is the property .count - if we type the following:

ourFirstArray.count

Maxscript will return 2 being the number of items in the array.

Error Checking

Open up the script we finished with last tut. it should look like this:

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
)
)

Now we have been referring to the current selection by typing $.
Another way we can refer to the current selection is by typing the word "selection" This is actually an array of objects. Create a bunch of random objects in a scene. A bunch of teapots will do fine. Select them all. Now type:

selection.count

Maxscript will return the number of teapots that are selected! Try typing:

selection[1]

Maxscript will return the first teapot in the selection list. Now deselect the teapots so that nothing is selected and type:

selection.count

Maxscript returns 0. This is obviously useful to us. Lets now add an if clause to our script so that it will only run the script if more than 0 items are currently selected. Add the following code:

MacroScript Random_Rotate category:"Rhys Tools" buttonText:"Random Rotate" tooltip:"Random Rotate"
(
for obj in $ do -- Loop over currently selected objects
(
if (selection.count > 0) then -- if the current selection has more than 0 items, then do...
(
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
)
)
)

Before we run it, lets add a warning to the user so that if they have no items selected, they will realise why nothing is happening when they run the script.

Messageboxes

We will use an else after our if/then statement. Also we'll invoke the messagebox function to display an error message to the user. Add the following lines:

MacroScript Random_Rotate category:"Rhys Tools" buttonText:"Random Rotate" tooltip:"Random Rotate"
(
for obj in $ do -- Loop over currently selected objects
(
if (selection.count > 0) then -- if the current selection has more than 0 items, then do...
(
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
)
else
(
messagebox "You must have at least one object selected!"
)

)
)

Now run the script. As it has the same macroscript name as last time, it should overwrite the previous version of the script and you can immediately use the menu item or button you made to kick off the new script. Make sure nothing is selected in the viewport and run it. Hopefully an error prompt should appear telling you that you need to have at least one item selected!
Good work. We have solved the first flaw with the script.




read 20266 times
11/27/2006 2:02:00 PM (last edit: 11/27/2006 2:02:00 PM)
show user profile  Dub.
User Interfaces

Obviously to solve the second problem, we're going to have to give the user some way to enter in the range of values that he/she wants the random rotations to fall between.

Open the Visual Maxscript Editor - Maxscript>visual Maxscript editor...
It's very simple to use the editor to create an interface. Add elements from down the bottom and position them in the workspace to your liking. Here is the results of my interface:


Now, save your interface as a .ms file (make sure you don't save it as a .vms file which is the default.)
Open the file in max as a maxscript and lets take a look. This is what mine looks like.

rollout RandomRotateRollout "Random Rotate" width:162 height:122
(
groupBox grp1 "Random Rotation Range" pos:[10,12] width:142 height:99
button btn1 "Randomly Rotate!" pos:[19,81] width:125 height:22
spinner spn1 "Minimum value " pos:[22,34] width:122 height:16 range:[-360.0,360.0,0.0]
spinner spn2 "Maximum value" pos:[23,56] width:122 height:16 range:[-360.0,360.0,0.0]
)

It contains 4 elements. A groupbox to keep things tidy, A button to run the script and two spinners to select the random constraints.
So how do we get the interface to run our script when we push the button?

Inside the rollout function, we can add the following code:

rollout RandomRotateRollout "Random Rotate" width:162 height:122
(
groupBox grp1 "Random Rotation Range" pos:[10,12] width:142 height:99
button btn1 "Randomly Rotate!" pos:[19,81] width:125 height:22
spinner spn1 "Minimum value " pos:[22,34] width:122 height:16 range:[-360.0,360.0,0.0]
spinner spn2 "Maximum value" pos:[23,56] width:122 height:16 range:[-360.0,360.0,0.0]

on btn1 pressed do -- when btn1 is pressed do...
(
for obj in $ do -- Loop over currently selected objects
(
if (selection.count > 0) then -- if the current selection has more than 0 items, then do...
(
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
)
else
(
messagebox "You must have at least one object selected!"
)
)
)

)

You'll see that I have just taken the contents of our macroscript and pasted it inside the "on btn1 pressed do" brackets.
That takes care of the button press! Now we just have to set things up so that the values of the two spinners are used as the random values instead of the hard coded +/- 3.
We can do this by switching the hard coded values with: spn1.value and spn2.value
make the changes.

Now we have two more little things we need to do. We need to tell max to display the rollout when our script starts running. we do this by adding the following after the rollout code:

createDialog RandomRotateRollout 162 166

And we also need to wrap the whole thing with our macroscript function. Our final script should look like this:

MacroScript Random_Rotate category:"Rhys Tools" buttonText:"Random Rotate" tooltip:"Random Rotate"
(
rollout RandomRotateRollout "Random Rotate" width:162 height:122
(
groupBox grp1 "Random Rotation Range" pos:[10,12] width:142 height:99
button btn1 "Randomly Rotate!" pos:[19,81] width:125 height:22
spinner spn1 "Minimum value " pos:[22,34] width:122 height:16 range:[-360.0,360.0,0.0]
spinner spn2 "Maximum value" pos:[23,56] width:122 height:16 range:[-360.0,360.0,0.0]

on btn1 pressed do -- when btn1 is pressed do...
(
for obj in $ do -- Loop over currently selected objects
(
if (selection.count > 0) then -- if the current selection has more than 0 items, then do...
(
randXrot = random spn1.value spn2.value -- create a random X rotation value and store as a variable
randYrot = random spn1.value spn2.value -- create a random Y rotation value and store as a variable
randZrot = random spn1.value spn2.value -- 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
)
else
(
messagebox "You must have at least one object selected!"
)
)
)
)
createDialog RandomRotateRollout 162 166
)

Run the script. If you have no errors, it should just return a number in the listener. Now test it out on a bunch of objects!

Conclusion and Homework

Hopefully by now, you should be able to create basic interfaces to interact with your scripts.
As an exercise, try adding separate range controls for X rotation, Y rotation and Z Rotation. Post your solution in this thread if you can!


read 20264 times
11/27/2006 2:02:20 PM (last edit: 11/27/2006 2:02:20 PM)
show user profile  Undersky
Dub, I must say this is fantastic work you're doing with these tutorials. When I have time (probably sometime during the christmas holidays) I'm going to sit down with these. Thanks!


Fjant.se
Demo reel, 2010
read 20239 times
11/27/2006 4:00:11 PM (last edit: 11/27/2006 4:00:11 PM)
show user profile  Nik Clark
Great stuff Dub.

I just want to add, that my preference is to build the script without the visual editor, then when the functionality is complete I use the visual tool to fix the layout. Others may prefer to start with the visual editor, of course.



Click here to send me an emailClick here to visit my websiteClick here to visit my photo gallery on Flickr

read 20233 times
11/27/2006 4:06:46 PM (last edit: 11/27/2006 4:06:46 PM)
show user profile  Dub.
yeah I debated on the best way to do it!

I make them by hand myself. However after talking to some other scripters, we decided to that the visual editor was a little less daunting for a beginner.

It'll be good to have your tut run over creating an interface by hand.


read 20229 times
11/27/2006 4:09:48 PM (last edit: 11/27/2006 4:10:11 PM)
show user profile  paconavarro
You rock!!!

Can I translate'em to spanish? many people might be interested in the tuts.

Cheers

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

read 20197 times
11/27/2006 11:50:19 PM (last edit: 11/27/2006 11:50:19 PM)
show user profile  Dub.
Sure.
Go right ahead. Just stick my name and email address on the bottom.


read 20191 times
11/28/2006 12:23:02 AM (last edit: 11/28/2006 12:23:02 AM)
show user profile  nemo
very cool tuts!
i want to write a little script that selects all non-quad faces in an epoly. lets see how far ill get now

edit: ..ok obviously this very example can be found in the maxscript helpfiles
:)

cheers
nemo out



read 20152 times
11/29/2006 1:47:16 PM (last edit: 11/29/2006 2:18:18 PM)
show user profile  Boomer
Dub,

Love it. Any possibility you could send me this series as a html file?

Cheers,



  Mathew Kaustinen
 http://www.boomerlabs.com

read 20128 times
11/29/2006 3:58:19 PM (last edit: 11/29/2006 3:58:19 PM)
show user profile  Dub.
I don't have them as one file sorry Matt, However Bobbyboy made a PDF in another thread containing all the tuts up to now.


read 20124 times
11/29/2006 4:08:23 PM (last edit: 11/29/2006 4:08:36 PM)
show user profile  Bobbyboy
Hey, here is the link
Click Me

Hope it helps :)
I still haven't gotten around to finishing off these tutorials yet


http://img.photobucket.com/albums/v322/bob9909/xmassig.jpg


read 20105 times
11/30/2006 7:38:50 AM (last edit: 11/30/2006 7:38:50 AM)
show user profile  CosmicMotions
My

)
else
(
messagebox "you must have at least one object selected!"
)

Does not work... ANything relating to me bing in max9 makes this function not work...

the error says = --no"map" function or undifined...

Normal?
Thanks
Cosmic.
read 19436 times
3/5/2007 4:33:03 PM (last edit: 3/5/2007 4:33:03 PM)
show user profile  Dub.
Can you post your entire script? There is nothing wrong with the snippet you posted.


read 19426 times
3/5/2007 5:14:28 PM (last edit: 3/5/2007 5:14:28 PM)
show user profile  CosmicMotions
Sure! Thank you for any explination you might have... Sometimes i have trouble testing certian things. is there some tricks to testing your script that help you?


macroScript Random_Rotate category:"Garys Tools" buttonText:"Random Rotate" tooltip:"Random Rotate"
(
rollout RandomRotateRollout "Random Rotate" width:162 height:149
(
groupBox grp1 "Random Rotation Range" pos:[10,12] width:142 height:99
button btn1 "Exicute Rotate!" pos:[19,81] width:125 height:22
spinner spn1 "Minimum " pos:[22,34] width:122 height:16 range:[-360.0,360.0,0.0]
spinner spn2 "Maximum" pos:[22,56] width:122 height:16 range:[-360.0,360.0,0.0]

on btn1 pressed do
(
for obj in $ do
(
if(selection.count > 0)then
(
randXrot = random spn1.value spn2.value
randYrot = random spn1.value spn2.value
randZrot = random spn1.value spn2.value
rot_obj = eulerangles randXrot randYrot randZrot
rotate obj rot_obj
)
else
(
messagebox "You must have at least one object selected!"
)
)
)
)
createDialog RandomRotateRollout 162 166
)

read 19403 times
3/6/2007 10:05:17 AM (last edit: 3/6/2007 10:07:04 AM)
show user profile  noob-saibot
Like this, the rollout will open

rollout RandomRotateRollout "Random Rotate" width:162 height:149
(
groupBox grp1 "Random Rotation Range" pos:[10,12] width:142 height:99
button btn1 "Exicute Rotate!" pos:[19,81] width:125 height:22
spinner spn1 "Minimum " pos:[22,34] width:122 height:16 range:[-360.0,360.0,0.0]
spinner spn2 "Maximum" pos:[22,56] width:122 height:16 range:[-360.0,360.0,0.0]

on btn1 pressed do
(
for obj in $ do
(
if(selection.count > 0)then
(
randXrot = random spn1.value spn2.value
randYrot = random spn1.value spn2.value
randZrot = random spn1.value spn2.value
rot_obj = eulerangles randXrot randYrot randZrot
rotate obj rot_obj
)
else
(
messagebox "You must have at least one object selected!"
)
)
)
)
createDialog RandomRotateRollout 162 166

read 19374 times
3/6/2007 11:27:07 AM (last edit: 3/6/2007 11:27:54 AM)
 
First page  Go to the previous page   [01]  [02]  Go to the next page  Last page
 
#Maxforums IRC
Open chat window


Support Maxforums.org