Monday, March 12, 2012

Matte Passes Quick and Clean

There are a bunch of scripts to create matte passes for mental ray, but these tend to be on the more complicated end of the spectrum. So to speed up my workflow a little I wrote this script. Select some geo, go to the render layer you'd like to add the matte pass to, and type 'dbrMattePass("myName")' in the command line.



global proc dbrMattePass(string $name){
    float $mayaVersion = `getApplicationVersionAsFloat` ; 
    //get selected objects
    string $sel[] = `ls -sl`;
    
    if (`size($sel)`){
        //get current render layer and create new pass contribution map
        string $curLayer = `editRenderLayerGlobals -q -currentRenderLayer`;
        string $contMap = `createNode -ss -name ($name + "PCM") passContributionMap`;
        connectAttr -na ($curLayer + ".passContributionMap") ($contMap + ".owner");

        //create new 1 channel matte render pass and associate with current render layer and new PCM
        string $mattePass;
        if ($mayaVersion == 2012)
            $mattePass = `shadingNode -ss -name ($name + "Matte") -asRendering renderPass`;
        else
            $mattePass = `createNode renderPass -name ($name + "Matte")`;
        
        setRenderPassType -type "MATTE" ($name + "Matte");
        setAttr -type "string" ($mattePass + ".passGroupName") ($name + "MatteGRP");
        setAttr ($mattePass + ".numChannels") 1;
        setAttr ($mattePass + ".frameBufferType") 256;
        setAttr ($mattePass + ".useTransparency") 1;
        setAttr ($mattePass + ".holdout") 1;
        connectAttr -na ($curLayer + ".renderPass") ($mattePass + ".owner");
        connectAttr -na ($mattePass + ".message")($contMap + ".renderPass");
      
        //add selected objects to pass contribution map
        for ($each in $sel){
            connectAttr -na ($each + ".message") ($contMap + ".dagObjects");
        }
    }
    else
    {
        print "Select some objects to create a matte.";
    }
} 



**Update: Didn't realize this initially because I hadn't reached post-production yet. Slipped right through my fingers, if you don't specify the values of hold-out and use transparency when creating pass contribution maps via mel, they will default to OFF. A huge error, hopefully nobody was relying on the previous code. The above represents working code.

No comments:

Post a Comment