Python "it" Scripting
Adding to the Commands Menu


return to main index


Introduction

Menu items can be added to the Commands menu of the 'it' window relatively easily. Pixar's document,
    "it" Custom Commands
provides a clear explanation of how it is done. This tutorial presents an example of a python script that can be directly executed from the Commands menu - figure 1. It does not use any dialogs, windows or any other UI elements. For examples of custom commands that use a UI refer to these links,
    save all
    histogram
    email image



Figure 1


To function as a menu item the Thumbnail.py script must,
    - implement a sub-class of Pixar's it.It3Command base class,
    - assign a value (the menu item label) to the m_menuPath instance variable,
    - implement a method called Invoke(), and finally,
    - call itself via it.commands.append(Thumbnail).

The python code is shown next.


import it, os
from it.It3Command import It3Command
  
class Thumbnail(It3Command):
    def __init__(self):
        self.m_menuPath = 'Commands/Save Thumbnail'
        
    def Invoke(self):
        self.save_thumbnail()
  
    def save_thumbnail(self, gamma=1.0,quality=100):   
        element = it.GetCurrentElement()
        image_name = element.GetLabel() + '.jpg'
        cwdpath = os.getcwd()
        path = os.path.join(cwdpath, image_name)
        self.saveImage(element, path, 2.2, 100) 
        it.app.Info('The thumbnail has been saved as "%s"' % os.path.abspath(path))
        it.app.RaiseLogWindow()
        
    def saveImage(self, element,path,gamma,quality):
        image = element.GetImage()
        image = image.Gamma(gamma);
        
        # dimensions, True preserve aspect ratio, True crop to preserve aspect ratio
        image = image.Reformat([0,150,0,150], True, True)
        
        image.SetMetaDataItem('JPEG_QUALITY', quality)
        image.Save(path, ice.constants.FMT_JPEG)    
it.commands.append(Thumbnail)


Installing & Loading the Script

Save the python script in your custom "it" scripts directory. For example, the "RfM: Customizing" tutorial recommends using this directory,
    maya/projects/RfM_it/Thumbnail.py
The users custom "it.ini" script must install the python script when Maya loads the RenderMan_for_Maya plugin. The "RfM: Customizing" tutorial recommends using the following path,
    maya/projects/RfM_ini/it.ini

The "it.ini" script should contain a command of the following form (Windows).


    LoadExtension python "C:/Users/YOUR_NAME/Documents/maya/projects/RfM_it/Thumbnail.py"

If the user has set an environment variable called MAYA_USER_DIR that "points" to the location of their maya directory the following command can be used.


    LoadExtension python "[GetEnv MAYA_USER_DIR]/projects/RfM_it/Thumbnail.py"











© 2002- Malcolm Kesson. All rights reserved.