本節(jié)講如何獲取和創(chuàng)建通道和層(也叫通道,集合)树碱。
讀取通道
獲取當前Nuke工程中所有的通道:
nuke.channels()
# Result:
['rgba.red', 'rgba.green', 'rgba.blue', 'rgba.alpha', 'depth.Z', 'forward.u', 'forward.v', 'backward.u', 'backward.v', 'disparityL.x', 'disparityL.y', 'disparityR.x', 'disparityR.y', 'mask.a', 'rotopaint_mask.a']
獲取層名字:
nuke.layers()
# Result:
['rgb', 'rgba', 'alpha', 'depth', 'motion', 'forward', 'backward', 'disparity', 'disparityL', 'disparityR', 'mask', 'rotopaint_mask']
獲取選取節(jié)點的通道:
node = nuke.selectedNode()
print node.channels()
# Result:
['rgba.red', 'rgba.green', 'rgba.blue', 'rgba.alpha']
添加新通道
添加一個新通道:
nuke.Layer('customLayer', ['red', 'green', 'blue'])
注意: 層不存在時,此函數(shù)會創(chuàng)建一個出來
![]()
警告: 不要給默認層添加通道赎婚,因為Nuke工程不保存樱溉。
重命名現(xiàn)有層:
nuke.Layer( 'customLayer' ).setName( 'myLayer' )
警告: 不要重命名默認層福贞,當重新加載工程時會報錯。
例子
autoComp
這個例子檢查exr文件中的通道完丽,常識講第二pass合到beauty渲染中逻族。
![](https://docs.thefoundry.co.uk/products/nuke/developers/100/pythondevguide/_images/autoComp_02.png)
可以參考腳本the sample exr
首先瓷耙,寫個函數(shù)autoComp刁赖,接收一個節(jié)點作為參數(shù)宇弛。此節(jié)點包含第二通道或者“AOVs"(任意輸出值)。第一步先獲取節(jié)點的通道:
def autoComp( node ):
channels = node.channels()
node.channels()返回此節(jié)點的所有通道彻况,返回值是通道名列表[‘rgba.red’, ‘rgba.green’, ‘rgba.blue’],為了獲取層名纽甘,
使用點號來切分通道名悍赢,并獲取結(jié)果的第一部分:
layers = [c.split('.')[0] for c in channels]
但是货徙,這留給我們很多層復(fù)制名痴颊,想去掉這些復(fù)制層,先把這些結(jié)果放入set锌杀,然后在倒回list:
layers = list( set([c.split('.')[0] for c in channels]) )
方便起見先排序糕再,代碼如下:
def autoComp( node ):
channels = node.channels()
layers = list( set([c.split('.')[0] for c in channels]) )
layers.sort()
準備好了所有層和通道饰豺,我們就可以建一個簡單UI,在合成中將現(xiàn)有的buffer 對應(yīng)到正確的pass饶套。創(chuàng)建標題為“Map AVOS”的panel垒探,
并添加三個下拉菜單:
- texture -- 附加的層包含紋理顏色
- diffuse -- 附加上的層包含 diffuse 燈光信息
- specular -- 附加的層包含 specular信息
將我們獲取的層名填入下拉菜單圾叼。 此例中夷蚊,值為TCL樣式的列表,空格區(qū)分(和‘ ’.join()很像):
p = nuke.Panel( 'Map AOVs' )
p.addEnumerationPulldown( 'texture', ' '.join( layers ) )
p.addEnumerationPulldown( 'diffuse', ' '.join( layers ) )
p.addEnumerationPulldown( 'specular', ' '.join( layers ) )
需要用戶指定哪個通道包含深度信息筋现,因此添加一個depth菜單矾飞,并給菜單添加通道名:
p.addEnumerationPulldown( 'depth', ' '.join( channels ) )
最后洒沦,添加兩個復(fù)選框來設(shè)置是否歸一化深度緩沖區(qū)(默認勾選)或者反轉(zhuǎn)(默認關(guān)閉):
p.addBooleanCheckBox( 'normalise depth', True)
p.addBooleanCheckBox( 'invert depth', False )
簡單的小界面就做完了价淌。
注意: 想加更多控制和復(fù)雜功能输钩,請看python panels
使用show()來顯示界面买乃。 這樣打開界面是模式對話框(非模式對話框在此章節(jié)木有)剪验。使用OK關(guān)閉窗口返回True,使用Cancel關(guān)閉返回False:
if not p.show():
return
![](https://docs.thefoundry.co.uk/products/nuke/developers/100/pythondevguide/_images/autoComp_03.png)
如果取消了娶眷,代碼就此停止運行届宠。 否則豌注,使用value()將選中的panel存入變量中,名字是panel上的每聪。
![](https://docs.thefoundry.co.uk/products/nuke/developers/100/pythondevguide/_images/autoComp_04.png)
texture = p.value( 'texture' )
diffuse = p.value( 'diffuse' )
spec = p.value( 'specular' )
depth = p.value( 'depth' )
normZ = p.value( 'normalise depth' )
invertZ = p.value( 'invert depth' )
現(xiàn)在利用panel中的信息建立節(jié)點樹吧药薯。
首先童本,為了以后處理方便系谐,將texture纪他,diffuse晾匠,specular緩沖區(qū) 導(dǎo)入rgb中凉馆。
注意: 從合成角度來說沒必要這么干澜共,因為可以將所有層、緩沖區(qū)內(nèi)聯(lián)合并起來母谎,但保險起見我們用了shuffle節(jié)點
使用nuke.nodes 來創(chuàng)建Shuffle節(jié)點奇唤,節(jié)點標簽為texture咬扇,將其連接到當前節(jié)點上:
shuffleNode = nuke.nodes.Shuffle( label='texture', inputs=[node] )
現(xiàn)在廊勃,設(shè)置inknob讀取panel中指定的層來包含紋理信息:
shuffleNode['in'].setValue(layer)
接下來懈贺,打開Shuffle節(jié)點的郵戳,附加一個Dot節(jié)點保持緊湊的布局:
shuffleNode['postage_stamp'].setValue( True )
nuke.nodes.Dot( inputs=[ shuffleNode ] )
同樣的事diffuse,specular層也要來一遍梭灿,所以把上面的代碼封裝成函數(shù)钠至,方便重用:
def shuffleLayer( node, layer ):
shuffleNode = nuke.nodes.Shuffle( label=layer, inputs=[node] )
shuffleNode['in'].setValue( layer )
shuffleNode['postage_stamp'].setValue( True )
return nuke.nodes.Dot( inputs=[ shuffleNode ] )
返回autoComp函數(shù)(在panel保存變量之后)調(diào)用新函數(shù)給每個層創(chuàng)建Shuffle節(jié)點:
texNode = shuffleLayer( node, texture )
diffNode = shuffleLayer( node, diffuse )
specNode = shuffleLayer( node, spec )
至此所有代碼如下:
def shuffleLayer( node, layer ):
'''
Shuffle a given layer into rgba
args:
node - node to attach a Shuffle node to
layer - layer to shuffle into rgba
'''
shuffleNode = nuke.nodes.Shuffle( label=layer, inputs=[node] )
shuffleNode['in'].setValue( layer )
shuffleNode['postage_stamp'].setValue( True )
return nuke.nodes.Dot( inputs=[ shuffleNode ] )
def autoComp( node ):
channels = node.channels()
layers = list( set([c.split('.')[0] for c in channels]) )
layers.sort()
# CREATE SIMPLE PANEL TO MAP THE BUFFERS
p = nuke.Panel( 'Map AOVs' )
p.addEnumerationPulldown( 'texture', ' '.join( layers ) )
p.addEnumerationPulldown( 'diffuse', ' '.join( layers ) )
p.addEnumerationPulldown( 'specular', ' '.join( layers ) )
p.addEnumerationPulldown( 'depth', ' '.join( channels ) )
p.addBooleanCheckBox( 'normalise depth', True)
p.addBooleanCheckBox( 'invert depth', False )
if not p.show():
return
# STORE PANEL RESULt IN VARIABLES FOR EASE OF USE
texture = p.value( 'texture' )
diffuse = p.value( 'diffuse' )
spec = p.value( 'specular' )
depth = p.value( 'depth' )
normZ = p.value( 'normalise depth' )
invertZ = p.value( 'invert depth' )
# CREATE SHUFFLE NODES
texNode = shuffleLayer( node, texture )
diffNode = shuffleLayer( node, diffuse )
specNode = shuffleLayer( node, spec )
現(xiàn)在運行腳本會產(chǎn)生三個Shuffle節(jié)點,將panle中指定的層倒入到rgba中:
![](https://docs.thefoundry.co.uk/products/nuke/developers/100/pythondevguide/_images/autoComp_05.png)
給diffuse緩沖區(qū)添加一個Multiply節(jié)點胎源,用戶可以修改緩沖區(qū)棉钧,并和shuffle節(jié)點的texture合并:
mergeDiff = nuke.nodes.Merge2( operation='multiply', inputs=[ texNode, nuke.nodes.Multiply( inputs=[diffNode] ) ], output='rgb' )
上面那行代碼做了如下事情:
- 創(chuàng)建Merge2節(jié)點
- 將操作knob設(shè)置成multiply
- 輸入連接到texture節(jié)點,另一個輸入(a)連接到新創(chuàng)建的Multiply節(jié)點
- Multiply節(jié)點連接到diffuse節(jié)點
- Merge節(jié)點的輸出設(shè)置成**rgb宪卿,因此原生a通道完整保存下來
創(chuàng)建另一對Multiply和Merge節(jié)點,這次是給specular節(jié)點万栅,將這個Merge節(jié)點連接到上面創(chuàng)建的Merge節(jié)點:
result = nuke.nodes.Merge2( operation='plus', inputs=[ mergeDiff, nuke.nodes.Multiply( inputs=[specNode] ) ], output='rgb' )
![](https://docs.thefoundry.co.uk/products/nuke/developers/100/pythondevguide/_images/autoComp_06.png)
查看用戶是否要歸一化深度佑钾,如果是,運行g(shù)etMinMax函數(shù)烦粒,在Grade節(jié)點中使用其結(jié)果:
if normZ:
black, white = examples.getMinMax( node, depth )
result = nuke.nodes.Grade( channels=depth, blackpoint=black, whitepoint=white, white_clamp=True, label='normalise depth', inputs=[result] )
檢查是否反轉(zhuǎn)深度通道休溶,如果是,使用Invert節(jié)點扰她,通道設(shè)置成depth:
if invertZ:
result = nuke.nodes.Invert( channels=depth, inputs=[result] )
最終兽掰,添加Grade節(jié)點稍微提升下深度通道中的黑色:
g = nuke.nodes.Grade( inputs=[result] )
g['black'].setValue( 0.05 )
g['mask'].setValue( depth )
節(jié)點圖如下:
![](https://docs.thefoundry.co.uk/products/nuke/developers/100/pythondevguide/_images/autoComp_02.png)
所有代碼如下:
import examples
import nuke
def shuffleLayer( node, layer ):
'''
Shuffle a given layer into rgba
args:
node - node to attach a Shuffle node to
layer - layer to shuffle into rgba
'''
shuffleNode = nuke.nodes.Shuffle( label=layer, inputs=[node] )
shuffleNode['in'].setValue( layer )
shuffleNode['postage_stamp'].setValue( True )
return nuke.nodes.Dot( inputs=[ shuffleNode ] )
def autoComp( node ):
channels = node.channels()
layers = list( set([c.split('.')[0] for c in channels]) )
layers.sort()
# CREATE SIMPLE PANEL TO MAP THE BUFFERS
p = nuke.Panel( 'Map AOVs' )
p.addEnumerationPulldown( 'texture', ' '.join( layers ) )
p.addEnumerationPulldown( 'diffuse', ' '.join( layers ) )
p.addEnumerationPulldown( 'specular', ' '.join( layers ) )
p.addEnumerationPulldown( 'depth', ' '.join( channels ) )
p.addBooleanCheckBox( 'normalise depth', True)
p.addBooleanCheckBox( 'invert depth', False )
if not p.show():
return
# STORE PANEL RESULt IN VARIABLES FOR EASE OF USE
texture = p.value( 'texture' )
diffuse = p.value( 'diffuse' )
spec = p.value( 'specular' )
depth = p.value( 'depth' )
normZ = p.value( 'normalise depth' )
invertZ = p.value( 'invert depth' )
# CREATE SHUFFLE NODES
texNode = shuffleLayer( node, texture )
diffNode = shuffleLayer( node, diffuse )
specNode = shuffleLayer( node, spec )
mergeDiff = nuke.nodes.Merge2( operation='multiply', inputs=[ texNode, nuke.nodes.Multiply( inputs=[diffNode] ) ], output='rgb' )
result = nuke.nodes.Merge2( operation='plus', inputs=[ mergeDiff, nuke.nodes.Multiply( inputs=[specNode] ) ], output='rgb' )
if normZ:
black, white = examples.getMinMax( node, depth )
result = nuke.nodes.Grade( channels=depth, blackpoint=black, whitepoint=white, white_clamp=True, label='normalise depth', inputs=[result] )
if invertZ:
result = nuke.nodes.Invert( channels=depth, inputs=[result] )
g = nuke.nodes.Grade( inputs=[result] )
g['black'].setValue( 0.05 )
g['mask'].setValue( depth )