Environment light in Nuke

Here’s a python script that translates Lighting Coefficients from the paper by Ravi Ramamoorthi to a expression node. Connect a normal pass to the expression node.

import nuke
import string

def LCoeff2Nuke():
	p = nuke.Panel("Paste Lighting Coefficients")
	p.addMultilineTextInput("Coefficients:", '')
	p.addButton("Cancel")
	p.addButton("OK")
	p.show()

	coeffVal= p.value("Coefficients:")

	#CONSTANTS
	c1 = '0.429043'
	c2 = '0.511664'
	c3 = '0.743125'
	c4 = '0.886227'
	c5 = '0.247708'

	Coefficients =[]

	i=0
	for line in coeffVal.split('\n'):
		if line.startswith('L_'):
			Coefficients.append(line.split()[1:4])
			print line
			i+=1

	expTemplate = 'c1*L22*(pow2(r)-pow2(g)) + (c3*L20*pow2(b)) + c4*L00-(c5*-L20) + 2*c1*((L2m2*r*g)+(L21*r*b)+(L2m1*g*b)) + 2*c2*((L11*r)+(L1m1*g) + (L10*b))'

	expressions = ['','','']
	for i in range(3):
		expressions[i] = expTemplate
		expressions[i] = expressions[i].replace('c1', c1) 	#
		expressions[i] = expressions[i].replace('c2', c2) 	#
		expressions[i] = expressions[i].replace('c3', c3) 	#
		expressions[i] = expressions[i].replace('c4', c4) 	#
		expressions[i] = expressions[i].replace('c5', c5) 	#
		expressions[i] = expressions[i].replace('L00', Coefficients[0][i]) #
		expressions[i] = expressions[i].replace('L1m1', Coefficients[1][i]) #
		expressions[i] = expressions[i].replace('L10', Coefficients[2][i]) #
		expressions[i] = expressions[i].replace('L11', Coefficients[3][i]) #
		expressions[i] = expressions[i].replace('L2m2', Coefficients[4][i])#
		expressions[i] = expressions[i].replace('L2m1', Coefficients[5][i])#
		expressions[i] = expressions[i].replace('L20', Coefficients[6][i]) #
		expressions[i] = expressions[i].replace('L21', Coefficients[7][i]) #
		expressions[i] = expressions[i].replace('L22', Coefficients[8][i]) #

	expNode = nuke.nodes.Expression(expr0=expressions[0],expr1=expressions[1],expr2=expressions[2])

3 Responses to “Environment light in Nuke”

  1. The Method Blog » Blog Archive » Nuke Expression/PythonScript/Training Resources Says:

    [...] Environment Light in Nuke http://www.madcrew.se/blog/?p=106 [...]

  2. Matt Whelan Says:

    What do i type in for the lighting co-efficients?

    thanks
    –matt–

  3. Matt Whelan Says:

    Also Im trying to get this into a Gizmo instead of a python script – if you’re willing to help im sure you’l know how to do it better than i!

    and i could really use the help as im just learning this stuff.

    thanks
    –matt–
    excuse my crap coding! :)

    def VraySpine():
    # get the file location from selected Diffuse node
    # strip it to location and Filename as 2 separate variables
    #create readnodes for RawLighting, GI, Reflection, Refraction, Specular
    # create merge nodes and Copy node and hook them up 2 multiplys 4 plus’s and copy alpha from spec

    import nuke, os

    baseRead = nuke.selectedNode()
    base = baseRead.knob ( “file” ).getValue()
    frameFirst = baseRead.knob( “first” ).getValue()
    frameLast = baseRead.knob( “last” ).getValue()
    basename = os.path.basename( base )
    dirname = os.path.dirname( base )

    # loose the last 8 characters – to be added back later
    basename = basename[0:len(basename)-8]

    diffuse = dirname + ‘/VRay_DiffuseFilter_VRay_DiffuseFilter/’ + basename + ‘_VRay_DiffuseFilter%04d.tga’
    print diffuse

    rawLighting = dirname + ‘/VRay_RawLighting_VRay_RawLighting/’ + basename + ‘_VRay_RawLighting%04d.tga’
    print diffuse

    GI = dirname + ‘/VRay_RawGlobalIllumination_VRay_RawGlobalIllumination/’ + basename + ‘_VRay_RawGlobalIllumination%04d.tga’
    print diffuse

    reflection = dirname + ‘/VRay_Reflection_VRay_Reflection/’ + basename + ‘_VRay_Reflection%04d.tga’
    print diffuse

    refraction = dirname + ‘/VRay_Refraction_VRay_Refraction/’ + basename + ‘_VRay_Refraction%04d.tga’
    print diffuse

    specular = dirname + ‘/VRay_Specular_VRay_Specular/’ + basename + ‘_VRay_Specular%04d.tga’
    print diffuse

    # Create the Read Nodes and MERGES

    nuke.knobDefault(“Merge2.operation”, “multiply”)

    diffuseRead = nuke.nodes.Read ()
    diffuseRead.knob( “file” ).setValue(diffuse)
    diffuseRead.knob( “first” ).setValue( frameFirst )
    diffuseRead.knob( “last” ).setValue( frameLast )

    lightRead = nuke.nodes.Read ()
    lightRead.knob( “file” ).setValue(rawLighting)
    lightRead.knob( “first” ).setValue( frameFirst )
    lightRead.knob( “last” ).setValue( frameLast )
    lightDiffuse = nuke.nodes.Merge2()
    lightDiffuse.setInput(1, diffuseRead)
    lightDiffuse.setInput(0, lightRead)

    GIRead = nuke.nodes.Read ()
    GIRead.knob( “file” ).setValue(GI)
    GIRead.knob( “first” ).setValue( frameFirst )
    GIRead.knob( “last” ).setValue( frameLast )
    GIDiffuse = nuke.nodes.Merge2()
    GIDiffuse.setInput(1, diffuseRead)
    GIDiffuse.setInput(0, GIRead)

    nuke.knobDefault(“Merge2.operation”, “plus”)

    totalLighting = nuke.nodes.Merge2()
    totalLighting.setInput(1, GIDiffuse)
    totalLighting.setInput(0, lightDiffuse)

    reflectionRead = nuke.nodes.Read ()
    reflectionRead.knob( “file” ).setValue(reflection)
    reflectionRead.knob( “first” ).setValue( frameFirst )
    reflectionRead.knob( “last” ).setValue( frameLast )
    reflectionSpine = nuke.nodes.Merge2()
    reflectionSpine.setInput(0, totalLighting)
    reflectionSpine.setInput(1, reflectionRead)

    refractionRead = nuke.nodes.Read ()
    refractionRead.knob( “file” ).setValue(refraction)
    refractionRead.knob( “first” ).setValue( frameFirst )
    refractionRead.knob( “last” ).setValue( frameLast )
    refractionSpine = nuke.nodes.Merge2()
    refractionSpine .setInput(0, reflectionSpine)
    refractionSpine .setInput(1, refractionRead)

    specularRead = nuke.nodes.Read ()
    specularRead.knob( “file” ).setValue(specular)
    specularRead.knob( “first” ).setValue( frameFirst )
    specularRead.knob( “last” ).setValue( frameLast )
    specularSpine = nuke.nodes.Merge2()
    specularSpine.setInput(0, refractionSpine)
    specularSpine.setInput(1, specularRead)

    # deletes the original READ node
    nuke.delete( baseRead )

    # Creates Copy node

    copyAlpha = nuke.nodes.Copy()
    copyAlpha.setInput(0, specularSpine)
    copyAlpha.setInput(1, specularRead)

    # Create Premult
    myPremult = nuke.nodes.Premult()

Leave a Reply