More Fun with "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 Studio 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 tcl /path/to/your/scripts/Web.tclNeedless to say, adjust the path accordingly for your pipeline.Next, let's verify that the extension is getting loaded properly. Create the aforementioned Web.tcl file and put the following in it:
RAT::LogMsg COPIOUS "Web Page Extension"Make sure you have saved both files (the .ini and .tcl) and then launch "it". Once "it" is open, go to the Message Log window (right click in the hub, then select Windows>Message Log). Change the Message Filter to its second-most verbose setting, which is COPIOUS. You will see all the files that were loaded as "it" started, ending with our new one: Web.tcl. You can also see that Web.tcl 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:
- Getting the current catalog
- Getting a list of all the images in that catalog
- For every image we find out its name
Here's what our simple Web page script looks like:
proc makeWebPage {fileName} { set f [open $fileName w] puts $f "<html>" set cat [it GetCurrentCatalog] set imgList [$cat GetImageList] puts $f "<ul>" foreach img $imgList { set name [file tail [$img GetFileName]] puts $f "<li>$name</li>" } puts $f "</ul>" puts $f "</html>" close $f }To run this, first load a few images in "it". Open the "it" console and run the script with the following command:
makeWebPage /tmp/index.htmlThe script will generate a simple bulleted list of the images loaded in our catalog.
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.
As it happens, "it" images can be annotated. Right-click in an image window and select Notes to reveal the note display/editor. 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" hub 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:
RAT::LogMsg COPIOUS "Defining Web Page Extension" proc makeWebPage {fileName} { set f [open $fileName w] set topDir [file dirname $fileName] set thumbDir [file join $topDir thumbs] if ![file isdirectory $thumbDir] { file mkdir $thumbDir } puts $f "<html>" puts $f {<table cellspacing="10" align="center">} set cat [it GetCurrentCatalog] set imgList [$cat GetImageList] set col 0 foreach img $imgList { # do we need an new row? if {$col == 0} { puts $f "<tr>" } # the start of a table cell puts $f "<td valign=\"top\">" # retrieve some basic information about the image set name [file tail [$img GetFilename]] set h [file tail [$img GetHandle]] # this is where we'll save the thumbnail image to set thumbFile [file join $thumbDir $h.jpg] # create an expression to make the image a fixed # size an to save it to disk as jpeg. The image will be at # maximum 200x200. Reformat takes care of any letter-boxing # that might be required set thumbExpr "_thumb.$h := $h Reformat(list(0,0,200,200), 1, 0);" append thumbExpr "_thumb.$h Save(\"$thumbFile\", IceOutputType Jpeg)" # "run" the expr it IceExpr $thumbExpr # put the image in the page using a page relative # path name puts $f "<img src=\"thumbs/$h.jpg\" alt=\"$name\">" puts $f "<br><b>$name</b>" # if the image has notes put them in too set notes [$img GetNotes] if ![string equal $notes {}] { puts $f "<small><pre>\n$notes</pre></small>" } # the end of a table cell puts $f "</td>" incr col # did we get to the end of a row? if {$col >= 4} { puts $f "</tr>" set col 0 } } if {$col != 0} { # there was an incomplete last row puts $f "</tr>" } puts $f "</table>" puts $f "</html>" close $f }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.
4 MORE BLING! |
There's one more cool trick we can add to this example. Make sure "it" is not already running, and that your PATH includes $RMSTREE/bin (assuming your RMSTREE is properly set), then open a command prompt window and find a directory containing 10 or so images. Now let's make a file called mkpage.tcl contaning the single line:
makeWebPage /some/path/index.html(Change the path to whatever works best for you.)Now at the command line type:
it *.tif -script mkpage.tclSit back and watch as this will cause "it" launch, open all the files ending in .tif as images, and then run the script in mkpage.tcl. In the event you don't care to see the progression of the images open before your very eyes, you can throw in the -hide option on the command line above. Note that "it" reads the images and -script arguments in left to right order, which is why the -script had to come after the image names.Pretty cool, eh?
Pixar Animation Studios
|