Source code for nukescripts.misc

# Copyright (c) 2009 The Foundry Visionmongers Ltd.  All Rights Reserved.

import nuke_internal as nuke
import nukescripts
import random
import os
import textwrap

[docs]def copy_knobs(args): thisGroup = nuke.thisGroup() if( thisGroup is not nuke.root() and ( thisGroup.locked() or thisGroup.subgraphLocked() ) ): raise RuntimeError("Can't paste knob values because " + + " is locked") selNodes = thisGroup.selectedNodes() groupCopy = nuke.nodes.Group(name = "____tempcopyknobgroup__") with groupCopy: nuke.nodePaste(nukescripts.cut_paste_file()) excludedKnobs = ["name", "xpos", "ypos"] try: nodes = groupCopy.nodes() for i in groupCopy.nodes(): for j in selNodes: k1 = i.knobs() k2 = j.knobs() intersection = dict([(item, k1[item]) for item in list(k1.keys()) if item not in excludedKnobs and item in k2]) for k in list(intersection.keys()): x1 = i[k] x2 = j[k] x2.fromScript(x1.toScript()) except Exception as e: nuke.delete(groupCopy) raise e nuke.delete(groupCopy)
[docs]def connect_selected_to_viewer(inputIndex): """Connects the selected node to the given viewer input index, ignoring errors if no node is selected.""" selection = None try: selection = nuke.selectedNode() except ValueError: # no node selected pass if selection is not None and selection.Class() == 'Viewer': selection = None nuke.connectViewer(inputIndex, selection)
[docs]def toggle_monitor_out(): """Toggles monitor out (switches it on if it's off, or vice versa) for the currently active viewer.""" viewer = nuke.activeViewer() if viewer is not None: enableKnob = nuke.toNode('ViewerMonitorOut').knob("enable") enableKnob.setValue(not enableKnob.value())
[docs]def clear_selection_recursive(group = nuke.root()): """Sets all nodes to unselected, including in child groups.""" for n in group.selectedNodes(): n.setSelected(False) groups = [i for i in group.nodes() if i.Class() == 'Group'] for i in groups: clear_selection_recursive(i)
[docs]def goofy_title(): """Returns a random message for use as an untitled script name. Can be assigned to nuke.untitled as a callable. Put a goofy_title.txt somewhere in your NUKE_PATH to customise.""" goofyFile = None for dir in nuke.pluginPath(): fileName = os.path.join(dir, "goofy_title.txt") if os.path.exists(fileName): goofyFile = fileName break if goofyFile is None: return "Missing goofy_title.txt" file = open(goofyFile) lines = file.readlines() file.close() lines = [line.strip() for line in lines] lines = [line for line in lines if len(line) > 0 and line[0] != '#'] if len(lines) < 1: return "Empty goofy_title.txt" return random.choice(lines)
[docs]def declone(node): if node.clones() == 0: return args = node.writeKnobs(nuke.WRITE_ALL | nuke.WRITE_USER_KNOB_DEFS | nuke.WRITE_NON_DEFAULT_ONLY | nuke.TO_SCRIPT) newnode = nuke.createNode(node.Class(), knobs = args) nuke.inputs(newnode, nuke.inputs(node)) num_inputs = nuke.inputs(node) for i in range(num_inputs): newnode.setInput(i, node.input(i)) node.setInput(0, newnode) nuke.delete(node)
[docs]def showname(): '''Shows the current script path and, if the selected node is a Read or Write node, the filename from it.''' # get the nuke script path # we always need this nukescript = nuke.value("") # look if there is a selected node # if not, output the script only p = nuke.Panel("Current Info", 500) try: n = nuke.selectedNode() if n.Class() == "Read" or n.Class() == "Write": a = nuke.value(".first", nuke.value("root.first_frame")) b = nuke.value(".last", nuke.value("root.last_frame")) curfile = n.knob("file").value()+" "+str(a)+"-"+str(b) p.addSingleLineInput("Filename", curfile) p.addSingleLineInput("Script", nukescript) else: p.addSingleLineInput("Script", nukescript) except: p.addSingleLineInput("Script", nukescript)
[docs]def swapAB(n): """Swaps the first two inputs of a node.""" thisGroup = nuke.thisGroup() if thisGroup is not nuke.root() and ( thisGroup.locked() or thisGroup.subgraphLocked() ) : lockedReason = "published" if thisGroup.subgraphLocked() else "locked" raise RuntimeError("Can't swap nodes because " + + " is " + lockedReason) if max(n.inputs(), n.minimumInputs()) > 1: a = n.input(0) n.setInput(0, n.input(1)) n.setInput(1, a)