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