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

Source Code for Module nukescripts.misc

  1  # Copyright (c) 2009 The Foundry Visionmongers Ltd.  All Rights Reserved. 
  2   
  3  import nuke, nukescripts 
  4  import random 
  5  import os 
  6  import textwrap 
  7   
  8   
9 -def copy_knobs(args):
10 g1 = nuke.thisGroup() 11 selNodes = g1.selectedNodes() 12 13 g2 = nuke.nodes.Group(name = "____tempcopyknobgroup__") 14 with g2: 15 nuke.nodePaste(nukescripts.cut_paste_file()) 16 17 excludedKnobs = ["name", "xpos", "ypos"] 18 19 nodes = g2.nodes() 20 for i in g2.nodes(): 21 for j in selNodes: 22 k1 = i.knobs() 23 k2 = j.knobs() 24 intersection = dict([(item, k1[item]) for item in k1.keys() if item not in excludedKnobs and k2.has_key(item)]) 25 for k in intersection.keys(): 26 x1 = i[k] 27 x2 = j[k] 28 x2.fromScript(x1.toScript()) 29 30 nuke.delete(g2)
31 32
33 -def connect_selected_to_viewer(inputIndex):
34 """Connects the selected node to the given viewer input index, ignoring errors if no node is selected.""" 35 36 selection = None 37 try: 38 selection = nuke.selectedNode() 39 except ValueError: # no node selected 40 pass 41 42 if selection is not None and selection.Class() == 'Viewer': 43 selection = None 44 45 nuke.connectViewer(inputIndex, selection)
46 47
48 -def toggle_monitor_output():
49 """Toggles monitor output (switches it on if it's off, or vice versa) for the currently active viewer.""" 50 viewer = nuke.activeViewer() 51 if viewer is not None: 52 enableKnob = viewer.node().knob('MonitorOutEnable') 53 enableKnob.setValue(not enableKnob.value())
54 55
56 -def clear_selection_recursive(group = nuke.root()):
57 """Sets all nodes to unselected, including in child groups.""" 58 for n in group.selectedNodes(): 59 n.setSelected(False) 60 groups = [i for i in group.nodes() if i.Class() == 'Group'] 61 for i in groups: 62 clear_selection_recursive(i)
63 64
65 -def goofy_title():
66 """Returns a random message for use as an untitled script name. 67 Can be assigned to nuke.untitled as a callable. 68 Put a goofy_title.txt somewhere in your NUKE_PATH to customise.""" 69 70 goofyFile = None 71 for dir in nuke.pluginPath(): 72 fileName = os.path.join(dir, "goofy_title.txt") 73 if os.path.exists(fileName): 74 goofyFile = fileName 75 break 76 77 if goofyFile is None: 78 return "Missing goofy_title.txt" 79 80 file = open(goofyFile) 81 lines = file.readlines() 82 file.close() 83 84 lines = [line.strip() for line in lines] 85 lines = [line for line in lines if len(line) > 0 and line[0] != '#'] 86 87 if len(lines) < 1: 88 return "Empty goofy_title.txt" 89 90 return random.choice(lines)
91 92
93 -def declone(node):
94 if node.clones() == 0: 95 return 96 args = node.writeKnobs(nuke.WRITE_ALL | nuke.WRITE_USER_KNOB_DEFS | nuke.WRITE_NON_DEFAULT_ONLY | nuke.TO_SCRIPT) 97 newnode = nuke.createNode(node.Class(), knobs = args) 98 nuke.inputs(newnode, nuke.inputs(node)) 99 num_inputs = nuke.inputs(node) 100 for i in range(num_inputs): 101 newnode.setInput(i, node.input(i)) 102 node.setInput(0, newnode) 103 nuke.delete(node)
104 105
106 -def showname():
107 '''Shows the current script path and, if the selected node is a Read or Write node, the filename from it.''' 108 109 # get the nuke script path 110 # we always need this 111 nukescript = nuke.value("root.name") 112 113 # look if there is a selected node 114 # if not, output the script only 115 p = nuke.Panel("Current Info", 500) 116 try: 117 n = nuke.selectedNode() 118 if n.Class() == "Read" or n.Class() == "Write": 119 a = nuke.value(n.name()+".first", nuke.value("root.first_frame")) 120 b = nuke.value(n.name()+".last", nuke.value("root.last_frame")) 121 curfile = n.knob("file").value()+" "+str(a)+"-"+str(b) 122 p.addSingleLineInput("Filename", curfile) 123 p.addSingleLineInput("Script", nukescript) 124 p.show() 125 else: 126 p.addSingleLineInput("Script", nukescript) 127 p.show() 128 except: 129 p.addSingleLineInput("Script", nukescript) 130 p.show()
131 132
133 -def swapAB(n):
134 """Swaps the first two inputs of a node.""" 135 136 if max(n.inputs(), n.minimumInputs()) > 1: 137 a = n.input(0) 138 n.setInput(0, n.input(1)) 139 n.setInput(1, a)
140 180