Thursday, March 24, 2011

A quick little Gamma script

I said I would post some stuff and 10 minutes later, here I am (pats self on back) . . .
Working on a job recently, we were using linear workflow, which is sweeeeet, but not so shocking since this was a pretty big studio that does mostly photo-real work. But they actually didn't have any automated way to deal with the gamma correct nodes in the scene (that I or anyone I talked to knew about). I got to one scene that I was animating and went to do a rough mock up of the texturing and lighting and realized with all of the objects and references and such that there were 40 or 50 shaders that needed a gamma correct node. So I decided to just write a quick script to do it. I know there are versions of this kind of stuff out there already, but if it's something manageable, I prefer to do it myself. I feel like I learn more that way.

Turns out, it's actually kind of hard to do scripting for this stuff. Basically, I just wanted 3 basic things. Add a gamma correct node right before the color slot of any shader I select (or all shaders), be able to adjust those nodes (turn the gamma from .455 to 1 and visa versa) and be able to remove all the gamma nodes if I want to. What turns out to be really tricky is navigating upstream/downstream from the selected materials. If I wanted to put a gamma correct node somewhere upstream from the selected shaders I would have to traverse back up through connections recursively and run the risk of encountering loops and such. Basically, navigating through shader trees is trickier than objects, because without the DAG hierarchy (parent/child) there's not such a clear and definitive relationship between nodes. I'm sure it's doable, but started getting deeper than I was interested in going, so I simplified things.

I wrote a script that will look at the "color" channel of a shader (I did blinn, phong, anisotropic, lambert, surface shader, mia, mia_x, mia_x_passes, testing for their respective color channels) and look for  a gamma correct node connected there. If there's not one, it will add it with the values you input. You can choose materials to apply this to, or do it to all of them. If there's nothing going into the color channel, no gamma is created. You can then adjust or delete the gammas (again only the gammas connected to the color channels) from the other tabs in the UI.

Basically, as long as your only worried about color information connected to the material and you're not adding any gamma correct nodes elsewhere in the tree, this script should cover you. Just run the scripts (zbw_gamma) select the materials you need gammas for (i.e. blinn, mia_material_x_passes, etc) and use the buttons/tabs to add, adjust or remove them.

As usual, I make no guarantees about how it will work for you, so don't plan on funding your retirement by suing me. (Like I've mentioned before, part of my reasoning for posting these scripts is just so that I have access to them if the studio I'm at has locked USB ports or something. Easier than emailing them to myself every job :)
Oh, BTW, I know there is some pretty useful gamma stuff in 2011 now. I've only been at 1 (!) studio that uses 2011 in production. And that was pretty hairy. I'm sure more than that do, but 2010 is still the most common release for use in production in NYC as far as I can tell. Any studio that has any significant pipeline tools, render farms, etc, would have to change/update all the code to incorporate 2011 into the pipeline and I'm not sure it's worth it at this point (actually, it's obviously not worth it yet, the proof is in the pudding, as they say)
Anyways, happy gamma-ing!

 You can download the script HERE.


  1. thank for sharing your excellent work zeith! one thing i couldnt manage is to download your gamma script

  2. Ahh.I did a follow up post that covered some of this stuff here:
    and updated the gamma script. It's now here:
    I'll update that link in the older post. Thanks for pointing it out!