Friday, February 20, 2009

Tip: Organizing Your Scripts & Adding Quick Links

Difficulty: Beginner - Intermediate

This is not a tutorial on scripting, but rather some helpful tips to speed up the process.

Here are a few helpful tips when creating Scripts in any Program.

1. Find a good editor!

I can't emphasize this point enough! When you pick up a good editor (like MEL Studio for Maya, found for free at www.digimation.com), you will cut down the amount of time you spend debugging. Note the emphasis on the word "good". You are looking for an editor that will recognize & color code different parts of your script. It is especially nice when one of those colors represents incomplete parts of your script, because a lot of errors have to do with incorrect syntax (such as forgetting to end with a semi-colon, or forgetting to put something in quotes).

Having a program that can help you recognize these errors quickly will really help you in writing them.

2. Add Headers to your file.

In Fig. 1, the green words are an example of a header. Headers are great for a couple of reasons:

1. They break up your script. When you start getting into hundreds and thousands of lines of code, it's nice to have key words/phrases that you can pick out to move from section to section.
2. Instructions can be placed in them. You can leave installation or execution instructions in the header to help yourself and others utilize the full potential of a script.
3. You can add reminders into them. When I'm writing a script, I like to put a "to do" list of sorts at the top to remind myself what I have written, what needs to be written, and I also record that last thing that I completed or the next thing I need to work on so that I can jump back into writing when I open the file again.

I write headers throughout my script to explain what the following section is meant to accomplish. This also helps when you are following the next tip because you can write out the entire script in psuedo-code and use the headers as benchmarks in the progression of the code.


Fig. 1: An example header for a MEL script. This was made with MEL Studio LE.
*Note* As any experienced scripter will know, always make sure that your headers are commented out (most commonly done by "//" or "/*" types in most languages).

3. Write out the process in plain english (or whatever language) first.

This is helpful before jumping into code because you may discover a more efficient way of writing a function or procedure in the process. It helps you to think about what you are trying to accomplish on a basic level. It also makes it easier to write because all you have to do is translate the sentence from your native languague to the scripting language.

For example, let's say you wanted to write a script to do the following:

Take the average of two numbers and apply it to the twist rotation of the arm.


I can easily look at this sentence and translate it into this:

armTwist.rotateX = (firstNumber + secondNumber)/2;


I can also leave the plain English in my script (but commented out of course) to help remind me what the code below it aims to do. So my final script would look like this:

//Take the average of two numbers and apply it to the twist rotation of the arm.
armTwist.rotateX = (firstNumber + secondNumber)/2;


4. Debug as you write.

One of the worst mistakes you can make is to write huge chunks of code at one time without debugging them. The more you write at once, the more errors you are likely to have. Debugging gets really complicated when errors start working together to give new errors that you're unfamiliar with. I will usually write until I have reached a point where I can execute the script and get visual results. For me, this is no more than 4 or 5 lines of code before I start checking what I have written.

5. Add Quick Links to move through your script.

This idea occurred to me while I was reading a Walkthrough for a video game online. (as a side note, I am not a fan of using walkthroughs unless I've beaten a game first. However, since entering my career I will shamefully admit that I have started using them because I just don't have as much time as I used to).

Many of these Walkthroughs have a table of contents listed at the top in order to access particular sections of the document quickly. In order to use them, you hit Ctrl + F in your browser to bring up the "Find" command, and then type in (or copy and paste) a unique code to jump to a specific part. As I was using one of these quick links I thought to myself, why haven't I been doing this in my MEL scripts?

I have found that creating Quick Links really helps me to make sure that I'm in the right section. Go back to Fig. 1 and look at the Table of Contents for the script. You should see a long list of Quick Links. You will need to write cooresponding lines of code further down in your script, such as in Fig. 2.


Fig. 2: The Quick Link that will be found after entering it into the Find Box.

It is best to write these while you are writing your script as opposed to when you are finished so that you can use the Quick Links during the creation of the script as well.

These are also helpful when revisiting a script, as you can find inefficient lines of code quickly and update them.

So, there you have it! Hopefully these tips will help you to create faster and more efficient scripts! Now get cracking and write the next great plug-in!

1 comment:

  1. Cool tips :)

    You should check out my site, i did some maya rigging tuts as well: http://poly-face.com

    Cheers

    ReplyDelete