Package nukescripts :: Module psd
[hide private]
[frames] | no frames]

Source Code for Module nukescripts.psd

  1  ##### 
  2  ## 
  3  ##  Uses meta-data from the incoming image stream to break a PSD files into layers 
  4  # 
  5  #   Each layer is combined together with a PSDMerge node which emulates the blending modes 
  6  # 
  7   
  8   
  9  import nuke 
 10  import random 
 11  import math 
 12  import threading 
 13   
 14   
15 -class Layer():
16 - def __init__(self):
17 self.attrs = {}
18
19 -def getLayers(metadata):
20 layers = [] 21 22 for key in metadata: 23 if key.startswith( 'input/psd/layers/' ): 24 splitKey = key.split( '/' ) 25 num = int( splitKey[3] ) 26 attr = splitKey[4] 27 try: 28 attr += '/' + splitKey[5] 29 except: 30 pass 31 32 while ( len(layers) <= num ): 33 layers.append( Layer() ) 34 layers[num].attrs[ attr ] = metadata[key] 35 36 37 return layers
38
39 -def breakoutLayers( node, sRGB = True ):
40 41 if not node: 42 return 43 44 nuke.Undo().begin() 45 46 blendMap = {} 47 blendMap['norm'] = "normal" 48 blendMap['scrn'] = "screen" 49 blendMap['div '] = "color dodge" 50 blendMap['over'] = "overlay" 51 blendMap['mul '] = "multiply" 52 blendMap['dark'] = "darken" 53 blendMap['idiv'] = "color burn" 54 blendMap['lbrn'] = "linear burn" 55 blendMap['lite'] = "lighten" 56 blendMap['lddg'] = "linear dodge" 57 blendMap['lgCl'] = "lighter color" 58 blendMap['sLit'] = "soft light" 59 blendMap['hLit'] = "hard light" 60 blendMap['lLit'] = "linear light" 61 blendMap['vLit'] = "vivid light" 62 blendMap['pLit'] = "pin light" 63 blendMap['hMix'] = "hard mix" 64 blendMap['diff'] = "difference" 65 blendMap['smud'] = "exclusion" 66 blendMap['fsub'] = "subtract" 67 blendMap['fdiv'] = "divide" 68 blendMap['hue '] = "hue" 69 blendMap['sat '] = "saturation" 70 blendMap['colr'] = "color" 71 blendMap['lum '] = "luminosity" 72 73 metaData = node.metadata() 74 layers = getLayers(metaData) 75 76 xspacing = 80 77 78 dotXfudge = 34 79 dotYfudge = 4 80 81 backdropXfudge = -( xspacing/2 ) + 10 82 backdropYfudge = -40 83 84 spacing = 70 85 86 x = node.xpos() 87 y = node.ypos() 88 curY = y + spacing * 2 89 90 if not sRGB: 91 colorSpace = nuke.nodes.Colorspace() 92 colorSpace['channels'].setValue( 'all' ) 93 colorSpace['colorspace_out'].setValue( 'sRGB') 94 colorSpace.setInput(0, node ) 95 colorSpace.setXYpos( x, curY ) 96 97 inputNode = colorSpace 98 else: 99 inputNode = node 100 101 curX = x 102 curY = y + spacing * 2 103 topY = curY 104 105 lastLayer = None 106 background = None 107 108 i = 0 109 110 for l in layers: 111 112 try: 113 if l.attrs['divider/type'] > 0: ## hidden divider or start of group 114 continue 115 except: 116 pass 117 118 i = i + 1 119 if i > 100: 120 nuke.message( "Too many layers, stopping at layer 100." ) 121 break; 122 123 name = l.attrs['nukeName'] 124 125 curY = topY 126 127 if i % 2 : 128 tileColor = 2829621248 129 else: 130 tileColor = 1751668736 131 132 backdrop = nuke.nodes.BackdropNode(tile_color = tileColor, note_font_size=18) 133 backdrop.setXYpos( curX + backdropXfudge, curY + backdropYfudge ) 134 135 curY += spacing/2 136 137 dot = nuke.nodes.Dot() 138 dot.setInput( 0, inputNode ) 139 dot.setXYpos( curX + dotXfudge , curY + dotYfudge) 140 curY += spacing 141 142 inputNode = dot 143 144 shuffle = nuke.nodes.Shuffle() 145 shuffle['label'].setValue( name ) 146 shuffle['in'].setValue( name ) 147 shuffle['in2'].setValue( 'none' ) 148 149 shuffle['red'].setValue( 'red' ) 150 shuffle['green'].setValue( 'green' ) 151 shuffle['blue'].setValue( 'blue' ) 152 shuffle['alpha'].setValue( 'alpha' ) 153 154 ## if no 'alpha' assume alpha of 1 155 alphaChan = name + ".alpha" 156 if not alphaChan in inputNode.channels(): 157 shuffle['alpha'].setValue( 'white' ) 158 159 shuffle['black'].setValue( 'red2' ) 160 shuffle['white'].setValue( 'green2' ) 161 shuffle['red2'].setValue( 'blue2' ) 162 shuffle['green2'].setValue( 'alpha2' ) 163 164 shuffle['out'].setValue( 'rgba' ) 165 shuffle['out2'].setValue( 'none' ) 166 167 shuffle.setInput(0, inputNode ) 168 shuffle.setXYpos( curX, curY ) 169 170 curY += spacing 171 172 crop = nuke.nodes.Crop() 173 crop['box'].setValue( l.attrs['x'], 0 ) 174 crop['box'].setValue( l.attrs['y'], 1 ) 175 crop['box'].setValue( l.attrs['r'], 2 ) 176 crop['box'].setValue( l.attrs['t'], 3 ) 177 178 crop.setInput(0, shuffle ) 179 crop.setXYpos( curX, curY ) 180 181 curY += spacing * 2 182 183 layer = crop 184 merge = None 185 186 try: 187 operation = blendMap[ l.attrs['blendmode'] ] 188 except: 189 print "unknown blending mode " + l.attrs['blendmode'] 190 operation = "normal" 191 192 if lastLayer: 193 psdMerge = nuke.nodes.PSDMerge() 194 psdMerge['operation'].setValue( operation ) 195 196 psdMerge.setInput(0, lastLayer ) 197 psdMerge.setInput(1, layer ) 198 psdMerge.setXYpos( curX, curY ) 199 psdMerge['sRGB'].setValue( sRGB ) 200 psdMerge['mix'].setValue( (l.attrs['opacity'] / 255.0) ) 201 try: 202 if ( l.attrs['mask/disable'] != True ): 203 psdMerge['maskChannelInput'].setValue( name + '.mask' ) 204 if ( l.attrs['mask/invert'] == True ) : 205 psdMerge['invert_mask'].setValue( True ) 206 except: 207 pass 208 lastLayer = psdMerge 209 else: 210 dot = nuke.nodes.Dot() 211 dot.setInput( 0, layer ) 212 dot.setXYpos( curX + dotXfudge, curY + dotYfudge ) 213 lastLayer = dot 214 215 curY += spacing 216 217 backdrop['bdwidth'].setValue( xspacing * 2 + backdropXfudge * 2 + 50) 218 backdrop['bdheight'].setValue( ( curY - backdrop.ypos() ) - backdropYfudge - 50 ) 219 backdrop['label'].setValue( l.attrs['name'] ) 220 221 curY += spacing 222 223 curX = curX + xspacing * 2 + backdropXfudge * 2 + 50 224 225 226 if not sRGB: 227 colorSpace2 = nuke.nodes.Colorspace() 228 colorSpace2['channels'].setValue( 'all' ) 229 colorSpace2['colorspace_in'].setValue( 'sRGB') 230 colorSpace2.setInput(0, lastLayer ) 231 colorSpace2.setXYpos( lastLayer.xpos(), lastLayer.ypos() + 2 * spacing ) 232 233 nuke.Undo().end()
234
235 -class BreakoutThreadClass(threading.Thread):
236
237 - def __init__(self, node):
238 threading.Thread.__init__(self) 239 self.node = node
240
241 - def run(self):
243 244
245 -def doReaderBreakout():
246 ## horrible workaround for Bug 24578 that doesn't allow creating gizmos from a script button 247 248 global readerBreakoutThread 249 readerBreakoutThread = BreakoutThreadClass( nuke.thisNode() ) 250 readerBreakoutThread.start()
251