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