Arnold RGB Matte AOV

This script creates RGB Matte AOV similar to VRays Multi Matte set up. I personally prefer this to Arnolds built in Object ID pass.

To Run: Put objects you want to have mattes for into sets. Make sure anything you want in the same Matte to be in the same set. Then select the Sets and run the script. It will create a new AOV for every three sets. So your first set will be red, second green, third blue, then it will create a new AOV and repeat.

#Arnold RGB MATTE AOV version 1.0.0
#For Maya. Tested in Maya 2014 & 2015 with Arnold 4.2.9.0
#Bryanna London www.bryannalondon.com
#!/usr/bin/env python

#TO RUN:
#Before running create maya sets for the different objects you want to mask
#Select the sets you want to create masks for before pressing run.

import maya.cmds as cmds

def rgbMatteSets():

    selSets = cmds.ls(selection = True , set = True)

    #If there are no selected sets stop script and send warning message
    if selSets == []:

        cmds.confirmDialog(title='No Selection', message='Nothing Selected. Nothing Done.' , button =['Ok'])

    else:

        #Create Gamma Nodes than set the colors to RGB
        gammaRed = cmds.shadingNode('gammaCorrect' , asUtility = True , name = 'gammaRed')
        cmds.setAttr(gammaRed + '.value' , 1,0,0 , type = 'double3')
        gammaGreen = cmds.shadingNode('gammaCorrect' , asUtility = True , name = 'gammaGreen')
        cmds.setAttr(gammaGreen + '.value' , 0,1,0 , type = 'double3')
        gammaBlue = cmds.shadingNode('gammaCorrect' , asUtility = True , name = 'gammaBlue')
        cmds.setAttr(gammaBlue + '.value' , 0,0,1 , type = 'double3')

        #For every three selected sets
        #Create Triple switch and Surface Shader
        for sel in range(0, len(selSets) , 3):

            tripSwitch = cmds.shadingNode('tripleShadingSwitch' , asUtility = True)
            cmds.setAttr(tripSwitch + '.default' , 0,0,0 , type = 'double3')

            rgbShader = cmds.createNode('surfaceShader' , name = 'rgbMatteMtl')
            #connect Triple Switch to Surface Shader
            cmds.connectAttr(tripSwitch + '.output' , rgbShader + '.outColor')

            #List the geo shape node in each Set
            #Connect shape node to the Triple Switch
            for index , geo in enumerate(selSets[sel:sel + 3]):

                geoShape = cmds.listRelatives(geo , path = True)
                #Apply Red Gamma to Objects in first set, Green to second set, and Blue to third set
                if index == 0:
                    gammaNode = gammaRed
                if index == 1:
                    gammaNode = gammaGreen
                if index == 2:
                    gammaNode = gammaBlue

                #For each object
                for obj in geoShape:
                    #Create Variable for Triple Switch Input since multiple things will be plugged in
                    tripInput = cmds.getAttr(tripSwitch + '.input' , size = 1)
                    #Connect Geo to Triple switch to the next available input number
                    cmds.connectAttr( obj + '.instObjGroups[0]' , tripSwitch + '.input['+ str(tripInput) + '].inShape')
                    #Connect Gammas to Triple Switch
                    cmds.connectAttr(gammaNode + '.outValue' , tripSwitch + '.input['+ str(tripInput) + '].inTriple')

            #create AOV and iterate the name
            rgbMatteAOV = cmds.createNode( 'aiAOV' , name= 'RGB_Matte_' + str(sel//3+1))
            cmds.setAttr( rgbMatteAOV + '.name' , 'RGB_Matte_' + str(sel//3+1), type='string')
            cmds.setAttr( rgbMatteAOV + '.type' , 5 )
            #Basic AOV Connections
            cmds.connectAttr( rgbMatteAOV + '.message' , 'defaultArnoldRenderOptions.aovList', nextAvailable=True )
            cmds.connectAttr( 'defaultArnoldDriver.message' , rgbMatteAOV + '.outputs[0].driver' )
            cmds.connectAttr( 'defaultArnoldFilter.message' , rgbMatteAOV + '.outputs[0].filter' )

            #Connect Surface Shader to AOV
            cmds.connectAttr(rgbShader + '.outColor' , rgbMatteAOV + '.defaultValue' )

rgbMatteSets()

© 2016 Bryanna London

Advertisement