Lesson 1: The first script….

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.

try(DestroyDialog RL_Test)catch()

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.

CreateDialog RL_Test

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.

Advertisements

About davewortley

Somewhere between an artist and a programmer, I like technical things, but being creative. I love problem solving and coming up with elaborate solutions. This blog is where I shall share ideas, tips, tutorials and anything that amuses me.
This entry was posted in Lessons, MaxScript. Bookmark the permalink.

11 Responses to Lesson 1: The first script….

  1. Pete says:

    Great tutorial, my first dialog! One problem I encountered, however, was the message box. Your code Messagebox “This is a message box” was throwing an error, and after some research I found out it needs to be messageBox instead of Messagebox. Might want to change this for beginners so they don’t get confused.

    Like

  2. Great Tutorial for someone who’s trying to learn scripting, but is all Arts and no computer smarts

    Like

  3. onut says:

    This is great for me! I always wanted to ease my work with scripts.
    Please tell us how the 4 button dialog script should be, because I can’t do it…

    Like

  4. onut says:

    excellent! now it works.
    I had problems with the across parameter.
    thank you!

    Like

  5. Jon Seagull says:

    Nice tutorial. You may want to add a footnote about using the Visual MAXScript editor to lay out more complex UIs.

    Like

    • davewortley says:

      The Visual Maxscript Editor is a bit of a pig to work with, I think it’s a lot better to learn how to do it using the auto-formatting of using ‘across’ and ‘offset’. The Visual MaxScript Editor makes some rather messy code some times and doesn’t support every parameters, for example you can’t put text in an edittext box using it, and the alignment of spinners with captions is often wrong when you run the script, and also sometimes when things get complex it’s difficult to move the right UI item as you can’t select it (annoying bug).

      It’s definitely useful for some designs of UI but I’ll cover it in another script and cover why it’s evil and sometimes deletes your code.

      Like

  6. kidcamaleao says:

    Thank’s man, very useful.

    Like

  7. Witono says:

    I have had phobia of max script for years until I see your blog today! Thank you!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s