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