RfM "it"
Sample Python Script


return to main index


Introduction

With the introduction of RMS18 the programming interface of the Image Tool ("it") application changed from TCL to python. Examples of the old style TCL (pre-RMS18) "it" scripts can be found here.

This tutorial presents an example of a python script - called SaveAll.py. The script should be executed after several images have been rendered into "it". All the images, except those named "_preview", will be saved as a numbered sequence of good quality jpegs either in the current Maya project directory or in the users "home" directory. This tutorial provides step-by-step instructions about the use of the SaveAll script.

The reader should review the tutorial,
    "RfM: Customizing"
It provides suggestions for setting up a directory structure for RenderMan for Maya as well as information about environment variables and initialization files.



Installing and Using SaveAll.py

Open your "it.ini" initialization script, for example, mine is located here,
    maya/projects/RfM_ini/it.ini
and add a LoadExtension command. For example, using MacOSX the line of code might look like this,

    LoadExtension python /Users/[GetEnv USER]/Documents/maya/projects/RfM_it/SaveAll.py

Copy the script given in listing 1 and save it as "SaveAll.py" in your "it" scripts directory. For example, my copy of the script is saved here,
    maya/projects/RfM_it/SaveAll.py


Using the Script with "it"

Render one or more images with Maya/RenderMan Studio and choose Window->Console...



Figure 1

To save all the catalog images as a numbered sequence of jpegs run the following command.



Figure 2


Remember to add the opening and closing parentheses to the SaveAll command.

The jpegs will be either saved in the users "home" directory or the current project directory. The message log window of "it" will show where the images have been saved.



Figure 3


The images are saved in a date and time stamped directory, for example,
    catalog_Fri_Jan_10_12_29_27_2014


Listing 1 (SaveAll.py)


# A utility proc for saving ALL the images from the "it" window as
# numbered jpeg files. The images are saved in a folder with a
# date and time-stamped name, for example, 
#    catalog_Tue_Jan__7_21/59/37_2014/
# The location of the directory is determined by the current working
# directory (cwd) of "it". When "it" has been launched by Maya the cwd
# will be the project directory of Maya.
# Useage:
# 1. Open the "it" Console window.
# 2. At the python prompt enter the following command,
#    it.extensions.SaveAll()
# or, specify the gamma,
#   it.extensions.SaveAll(1.8)
# or, specify the gamma and jpeg quality,
#   it.extensions.SaveAll(1.8, 75)
#
# M.Kesson Jan 7 2014
# Edit: Aug 26 2014 Changed default gamma from 2.2 to 1.0
  
import it, time, os
  
def SaveAll(gamma=1.0,quality=100):
    localtime = time.asctime( time.localtime(time.time()) )
    localtime = localtime.replace(' ', '_')
    localtime = localtime.replace(':', '_')    
    outname = 'catalog_%s' % localtime
    cwdpath = os.getcwd()
    if len(cwdpath) < 3:
        it.app.Error('Unable to determine your current working directory.')
        it.app.Error('Unable to save the images.')
        it.app.RaiseLogWindow()
        return
    it.app.Info('Saving images to "%s"' % cwdpath)
        
    out_dirpath = os.path.join(cwdpath,outname)
    if not os.path.exists(out_dirpath):
        os.mkdir(out_dirpath)
    aovDict = {}
    cat = it.app.GetCurrentCatalog()
    img_counter = 1
    for i in range (0, cat.GetChildCount()):
        img = cat.GetChild(i)
        imgname = it.os.path.basename( img.GetFilename() )
        if imgname == '_preview':
            continue
        # Add a padded numeric extension
        imgname = imgname + ('.%04d' % img_counter) + '.jpg'
        out_imgpath = os.path.join(out_dirpath, imgname)
  
        __saveImage(img, out_imgpath, gamma, quality)
        img_counter += 1
        
        # Save any AOV's
        for j in range (0, img.GetChildCount()):
            aov = img.GetChild(j)
            aovname = it.os.path.basename( aov.GetFilename() )
            # Remove the extension
            aovname = os.path.splitext(aovname)[0]
            # Maintain unique counters for each AOV
            aov_counter = 1
            if aovDict.has_key(aovname):
                aov_counter = aovDict[aovname]
                aov_counter += 1;
            aovDict[aovname] = aov_counter
            
            # Add a padded numeric extension
            aovname = aovname + ('.%04d' % aov_counter) + '.jpg'
            out_aovpath = os.path.join(out_dirpath, aovname)
            __saveImage(aov, out_aovpath, gamma, quality)
            
#=====================================
# Local private utility procedure
#=====================================
def __saveImage(img,path,gamma,quality):
    image = img.GetImage()
    image = image.Gamma(gamma);
    image.SetMetaDataItem('JPEG_QUALITY', quality)
    image.Save(path, ice.constants.FMT_JPEG)    






© 2002- Malcolm Kesson. All rights reserved.