Wednesday, March 21, 2012

How I write a MEL script - Identify the need

Here we will identify the need for the proc.  This not only helps us clarify our vision but could potentially stop us from writing code that has no clear purpose.

In order to illustrate my script writing process, I'll identify a need, come up with an idea, make a scriptlet, then write the proc.  The example will be to help lighters make a matte render to use in Photoshop.

I do a lot of concept designs, so I tweak single images in Photoshop all the time.  If you want mates for a sequence of frames, search for mental ray custom color pass.

Before we start writing anything, look before you leap  Part 1 of the identification process
  • Search is your friend - Look for similar code that might exist online.  Use google, cg forums, whatever.  Just start searching.
  • Let Maya do the work - Most likely Maya has something that might do exactly what you want.  Search Maya's docs also.
  • The scriptEditor is your friend - Always keep the scriptEditor open.  Poke around and try a few things.  Look at the commands Maya spits out, they are incredibly helpful.
  • Ask your colleges - People in your workplace are your greatest source of information.  Collaborate often and you'll see your productivity go up.
  • Write down your findings, bookmark pages of interest, copy ( legal ) source material, etc.  You might think you can remember where you found that crucial tidbit of information, but if you're anything like me, then if it isn't written down somewhere, its going to take a while to find it once again.  There is no such thing as too much reference.
    Now ask yourself a few questions and answer them. Part 2 of the identification process
    • Who will use it? - Lighters will use it .
    • What will it be used for? - To make a matte render, isolating pieces of geometry in a single image.
    • Where will it be used? - In the lighting stage of the pipeline.
    • When will it be used? - Once a lighter determines that they need an isolated piece of geometry.
    • Why will it be used? -  Mulit-colored matte renders are needed in Photoshop to quickly create masks.
    • How will it be used? - User will supply a list of geo and we will make a shader, a switch node, a gamma node per object, assign the objects to the switch node and set a random value per channel on the gamma node.
    If you are stuck on the "how" part, it would be an ideal time to talk to your colleges, a potential user of your proc, your significant other, your parents, whomever.  I find that having to explain what I want to do informs me about how I should do it.  Talking to non-technical people is as important as talking to a fellow programmer.  Chances are that your tool will need to be used by a non-technical person anyway.  Call it market research.

    If you are still stuck, then sleep on it, do something else.  Take a break.  When you come back you might have a new approach.

    Narrow the scope. Part 3 of the identification process

    Now that you've answered your 5Ws. Take a step back.  Look objectively at what you are intending to do, and break it down into logical steps.  Notice that we're not trying to make the matte pass, render it, send it to Photoshop and pull all the masks in one proc.  Though that sounds like a time saver for a lighter, that's a tremendous amount of code to write.  So go after it piece by piece.  Narrow the scope to a single step - aka low level tools.  Once you've written code to do the low level tools, then comes the time to write a high level tool that pulls all the pieces together.

    No comments:

    Post a Comment