+1 tag for Google+

Thursday, 20 November 2014

Adding ObjectID Pass to Render Output in Maya

(This article has a second part, with a more complete script, which you should check out later. Read part 2 of this article)

So I was trying to find out how to generate ObjectIDs for Mental Ray renders in Maya. Peering into render passes and render layers settings, I could not find an objectId setting or preset. So I had to search the Internet for some answers.

Here's a little help from Autodesk folks on how to generate object ID pass for your mental ray renders.

Quoting from the page:

-- start quote --

To obtain an object ID pass, please follow these steps:
  1. Select all the objects that you wish to include in the pass, and execute the following MEL script:

    addAttr -ln miLabel -at long -k 1;

    This will add a new attribute called “MI Label” for all the objects in the Extra Attributes section.
  2. Once you create a different MI Label for each object, you will assign a different ID value, thus making each object unique during render time.
  3. Select the render camera and under the mental ray section > Output Shaders, select the Create button. This will create a mentalrayOutputPass.
  4. Change the Frame Buffer Type to Label (Integer) 1x32 Bit.
Note: You can change the image format and the name of the pass according to your needs.
The result will be a separate rendered file in the format selected in the Image Format field.
-- end quote --

I believe after we set up the objects adding the extra attribute, we need to give a unique integer number to every object that has the added "miLabel" attribute.
Thus I have coded a procedure in PyMel that goes through every transform node that is selected, makes sure it has a shape node (ie, it is not an empty group). For each one of the qualifying objects, the script creates the "miLabel" attribute if it does not exist, and assigns an incrementing integer. 

# -- code start --
from pymel.core import *
def uniqObjIdAssign():
    # Written by Patrick Woo
    # usage: select all the objects to give unique objectIDs to
    #        and run this script
    # for more info go to:
    # http://patrickvfx.blogspot.com/2014/11/adding-objectid-pass-to-render-output.html
    counter = 0; 
    for x in ls(sl=True, type='transform'):
        if x.getShape():
            # makes that the transform node isn't just an empty group 
            # without a shape node
            if "miLabel" not in str(x.listAttr()):
                addAttr (x, ln='miLabel', at='long', k = True);
            print '%s.miLabel set to %i'%(x,x.miLabel.get())
            counter += 1

# -- code end --

Even with the script the camera output pass will still need to be set-up as per instructed on the Autodesk page above.

Read part 2 of this article where I post my fully automated script

This is one of the resulting renders from the MentalRayOutputPass, with file mode turned on, and written to an external file, separate from the main rendered image.