More "it" Scripting

More "it" Scripting

Once you've run through the basics, you're ready to delve a little deeper into scripting "it". In this tutorial we'll go over the steps to make a script that will create a Web page out of a catalog, including making thumbnails and images notes. This will demostrate the two major objects available in the scripting environment; the Catalog and the Image.

As with the introductory tutorial, you don't need a Maya scene file for this. All you need is a directory full of images to play with.


1. ADDING AN EXTENSION

First, let's set up a new extension script file. This is a file that will get sourced into "it"'s brain each time "it" starts.

All of the tools in RenderMan for Maya use initialization files (.ini) that can be customized by the user. We strongly recommend that you do not edit the .ini files in the installation; instead, create supplemental .ini files and place them in a directory that you can point at with the $RMS_SCRIPT_PATHS environment variable. In this case, if you don't already have one, create an it.ini file and add the following line:

LoadExtension python Web.py

Previous versions of "it" would require a full pathname to the file Web.py which you can still specify. However LoadExtension will look for the file Web.py relative to the it.ini file being processed so it is much simpler just to put that file in the same directory.

Next, let's verify that the extension is getting loaded properly. Create the aforementioned Web.py file and put the following in it:

it.app.Notice("Web Page Extension")

Make sure you have saved both files (the .ini and .py) and then launch "it". Once "it" is open, go to the Message Log window ( Window > Message Log...). Change the Message Filter to its most verbose setting, which is "Debug". You will see all the files that were loaded as "it" started, ending with our new one: Web.py. You can also see that Web.py produced something of its own: the message "Web Page Extension".


2. GETTING YOUR SCRIPT ON

Now lets make our script actually make a small Web page. The important tasks this script is performing are:

  1. Getting the current catalog
  2. Getting a list of all the images in that catalog
  3. For every image we find out its name

Here's what our simple Web page script looks like:

import it
import ice

def makeWebPage(filename):
    f = open(filename, 'w')

    f.write('<html>\n')
    f.write('<ul>\n')

    cat = it.app.GetCurrentCatalog()

    for i in range (0, cat.GetChildCount()):
        child = cat.GetChild(i)
        name = it.os.path.basename( child.GetFilename() )
        f.write('<li>%s</li>\n' % (name) )

    f.write('</ul>\n')
    f.write('</html>\n')

    f.close()

To run this, first load a few images in "it". Open the "it" console and run the script with the following command:

it.extensions.makeWebPage('/tmp/index.html')

The script will generate a simple bulleted list of the images loaded in our catalog.

  • Tip

    You can use the Save Session... and Restore Session... functions here to help you get a catalog set up quickly.


3. WEB BLING!

What say we trick out our little Web page? Let's make a dazzling four column table with thumbnail images of the contents. This will demostrate how to perform some basic image processing, including resizing the images in our catalog and saving the thumbnails to disk.

To create the thumbnail images we use the handy Reformat operator. Reformat can change and/or resize an image in many different ways, depending on how you want to crop or squeeze your images into the new shape. For the Web page we want to createm uniform-sized images that are letterboxed if they are not the right shape. In Reformat terminology that means "preserve aspect ratio and don't crop".

  • Tip

    As it happens, "it" images can be annotated. Go to Window > Inspector and to the right of the image window, you should a large text box for you to add notes. When you save a session the notes get saved along with it. Before you run the example, add notes to a few of the images in your catalog to see how that will come out.

When you run the script this time you'll see that a directory called thumbs is created alongside the html file, and in the small jpeg versions of the images are saved therin. You'll also see in the "it" Catalog window that the Catalog now has a new image for each thumbnail that was made. You can delete them if you like.

So, without further ado, here's the tricked-out script:

import it
import ice

it.app.Info('Defining Web Page Extension')

def makeWebPage2 (filename):
    f = open(filename, 'w')

    topDir = it.os.path.dirname(filename)
    thumbDir = topDir + '/thumbs'

    if it.os.path.lexists(thumbDir) is False:
        it.os.mkdir(thumbDir)

    f.write('<html>\n')
    f.write('<table cellspacing="10" align="center>\n')

    cat = it.app.GetCurrentCatalog()

    col = 0

    for i in range(0, cat.GetChildCount()):
        child = cat.GetChild(i)

        if col == 0:
            f.write('<tr>\n')

        f.write('<td valign="top">\n')

        name = it.os.path.basename( child.GetFilename() )
        h = it.os.path.basename( child.GetLabel() )
        thumbFile = thumbDir + '/' + h + '.jpg'

        thumbIceImage = child.GetImage()
        reformat = thumbIceImage.Reformat([0,200,0,200], True, False)
        reformat.Save( thumbFile, ice.constants.FMT_JPEG)

        f.write('<img src="thumbs/%s.jpg" alt="%s">\n' % (h, name))
        f.write('<br><b>%s</b>' % (name))

        notes = child.GetNotes()
        if notes != '':
            f.write('<small><pre>\n%s</pre></small>\n' % (notes))

        f.write('</td>\n')

        col = col + 1
        if col >= 4:
            f.write('</tr>\n')
            col = 0

    if col != 0:
        f.write('</tr>\n')

    f.write('</table>\n')
    f.write('</html>\n')

    f.close()

Replace the original script with the script above, save the file, and execute as you did in Step 2. This time you'll get a slightly spiffier page with thumbnails, notes, and labels.