Before I make any scripts, I like to build out an interface, so here’s a quick lesson on the basics of interface building. I find it helps me map out what I need to code. So straight out here’s the basic block I use for all my starting interfaces.
try(DestroyDialog RL_Test)catch() Rollout RL_Test "The Test Dialog" ( button btn_ok "Ok" on btn_ok pressed do ( Destroydialog RL_Test ) ) CreateDialog RL_Test
So take this block of code, put it into a new Maxscript (maxscript ->new script) window and run it (Ctrl + E). You’ll get a window with an ‘Ok’ button, if you press it it’ll close the dialog. Easy.
Lets analyse it and understand what we’ve just made.
A try()catch() is a way of running some code that might not be valid, but won’t crash your script, in this instance we’re saying, lets try and see if we can Destroy a dialog (that’s close to me and you), and the dialog is called RL_Test, if we did this outside of a try/catch the code would fail if the the Dialog RL_Test hadn’t been created, and thus crash our script. It’s a bit of a pain in the arse to try find out if there is a dialog open with that name already (I’ll leave that for another tutorial), so the easiest thing is just to ‘try’ it. The Try/Catch functions have to come together, the catch is a useful way to find out what caused your try to fail. It can be used sometimes when you’re feeling lazy or simply cannot work out what is crashing your script, I’ll go more into the pro’s and con’s of using it on a separate tutorial.
Rollout RL_Test "The Test Dialog"
The next bit defines our rollout name and it’s caption, everything in the next bracketed section ( ) will be considered part of the Rollout. Certain things can live outside the scoop of a rollout, we’ll go into functions later on. You’ll notice we’re not defining any width or height parameters for our rollout, we could do but sometimes it’s just quicker and easier for maxscript to figure this all out for us.
button btn_ok "Ok"
So this but tells the script we want a button (funny that), we define our buttons name so we can reference it later and we tell the button it’s caption “Ok” again we’re not defining any sizes or positions for our button, maxscript will just position it at the top in the middle and make it a reasonable size. I always thought you needed to use the Visual Maxscript Listener to design your scripts, and this led to my old scripts being full of long-winded GUI code for exact positions. One of the most useful things that I wish I’d known about from day 1 is the across parameter. Add the following line before your ok button.
button btn_pressme "Press Me" across:2
Run your script and you’ll have two buttons next to each other, pretty useful for quickly laying out scripts. Sometimes you need to define exact positions but I try to avoid this as it’s fiddly to do.
on btn_ok pressed do
Next we define an event handler for what happens when we press the button, it reads pretty simply, so every time the button is left clicked (there are right click event handlers) the next section of code between the brackets ( ) will be run. In this instance we’re just telling our script to effectively finish by using the DestroyDialog function, with parameter of the dialog we want to close, it doesn’t know what dialog you want to close, it could be this one that is open or it could be an entirely different dialog.
Finally we create our dialog, this may seem a bit backwards to do this last, but you need to tell max everything about the rollout before you can create it.
So there’s the basics of creating a dialog for your script, I always find it useful to start with this setup as it’ll stop duplicate dialogs appear as you run the script multiple times, obviously you will want to give it your own name, but remember you’ve got 4 references to change.
So the practical bit, Create your own dialog with it’s own name, make 4 buttons, making a 2×2 grid. On pressing of a button it should display a messagebox with the name of the button….
hint… Messagebox “This is a message box”
See what you come up with.