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

Source Code for Module nukescripts.autobackdrop

 1  # Copyright (c) 2009 The Foundry Visionmongers Ltd.  All Rights Reserved. 
 2   
 3  import nuke, random 
 4   
5 -def nodeIsInside (node, backdropNode):
6 """Returns true if node geometry is inside backdropNode otherwise returns false""" 7 topLeftNode = [node.xpos(), node.ypos()] 8 topLeftBackDrop = [backdropNode.xpos(), backdropNode.ypos()] 9 bottomRightNode = [node.xpos() + node.screenWidth(), node.ypos() + node.screenHeight()] 10 bottomRightBackdrop = [backdropNode.xpos() + backdropNode.screenWidth(), backdropNode.ypos() + backdropNode.screenHeight()] 11 12 topLeft = ( topLeftNode[0] >= topLeftBackDrop[0] ) and ( topLeftNode[1] >= topLeftBackDrop[1] ) 13 bottomRight = ( bottomRightNode[0] <= bottomRightBackdrop[0] ) and ( bottomRightNode[1] <= bottomRightBackdrop[1] ) 14 15 return topLeft and bottomRight
16
17 -def autoBackdrop():
18 ''' 19 Automatically puts a backdrop behind the selected nodes. 20 21 The backdrop will be just big enough to fit all the select nodes in, with room 22 at the top for some text in a large font. 23 ''' 24 selNodes = nuke.selectedNodes() 25 if not selNodes: 26 return nuke.nodes.BackdropNode() 27 28 # Calculate bounds for the backdrop node. 29 bdX = min([node.xpos() for node in selNodes]) 30 bdY = min([node.ypos() for node in selNodes]) 31 bdW = max([node.xpos() + node.screenWidth() for node in selNodes]) - bdX 32 bdH = max([node.ypos() + node.screenHeight() for node in selNodes]) - bdY 33 34 zOrder = 0 35 selectedBackdropNodes = nuke.selectedNodes( "BackdropNode" ) 36 #if there are backdropNodes selected put the new one immediately behind the farthest one 37 if len( selectedBackdropNodes ) : 38 zOrder = min( [node.knob( "z_order" ).value() for node in selectedBackdropNodes] ) - 1 39 else : 40 #otherwise (no backdrop in selection) find the nearest backdrop if exists and set the new one in front of it 41 nonSelectedBackdropNodes = nuke.allNodes("BackdropNode") 42 for nonBackdrop in selNodes: 43 for backdrop in nonSelectedBackdropNodes: 44 if nodeIsInside( nonBackdrop, backdrop ): 45 zOrder = max( zOrder, backdrop.knob( "z_order" ).value() + 1 ) 46 47 # Expand the bounds to leave a little border. Elements are offsets for left, top, right and bottom edges respectively 48 left, top, right, bottom = (-10, -80, 10, 10) 49 bdX += left 50 bdY += top 51 bdW += (right - left) 52 bdH += (bottom - top) 53 54 n = nuke.nodes.BackdropNode(xpos = bdX, 55 bdwidth = bdW, 56 ypos = bdY, 57 bdheight = bdH, 58 tile_color = int((random.random()*(16 - 10))) + 10, 59 note_font_size=42, 60 z_order = zOrder ) 61 62 # revert to previous selection 63 n['selected'].setValue(False) 64 for node in selNodes: 65 node['selected'].setValue(True) 66 67 return n
68