Skip to content

Instantly share code, notes, and snippets.

@melMass
Forked from tokejepsen/batch_edit.py
Created May 1, 2016 01:57
Show Gist options
  • Save melMass/d72f05f9b149536335bf4b1941c5058d to your computer and use it in GitHub Desktop.
Save melMass/d72f05f9b149536335bf4b1941c5058d to your computer and use it in GitHub Desktop.
Nuke: Batch Edit Tool
import os
import difflib
import nuke
import nukescripts
class KnobsPanel(nukescripts.PythonPanel):
def __init__(self, knobs):
nukescripts.PythonPanel.__init__(self, 'KnobsPanel')
self.knobs = knobs
def knobChanged(self, knob):
if isinstance(knob, nuke.File_Knob):
v1 = self.knobs[knob.name()][0].value()
v2 = knob.value()
diff = difflib.unified_diff(v1, v2)
orig = ''
sub = ''
for char in list(diff):
if char[0] == ' ':
orig += char[1]
sub += char[1]
if char[0] == '+':
sub += char[1]
if char[0] == '-':
orig += char[1]
orig = orig[4:-3]
sub = sub[4:-3]
for key in self.knobs:
if key == knob.name():
for k in self.knobs[key]:
f = k.value().replace(orig, sub)
k.setValue(f)
return
for key in self.knobs:
if key == knob.name():
for k in self.knobs[key]:
if isinstance(k, nuke.Link_Knob):
k = k.getLinkedKnob()
k.setValue(knob.value())
def main():
nodes = nuke.selectedNodes()
knobs = {}
for node in nodes:
for k in node.knobs():
knob = node.knobs()[k]
if k in knobs:
knobs[k].append(knob)
else:
knobs[k] = [knob]
p = KnobsPanel(knobs)
for k in knobs:
knob = knobs[k][0]
value = knobs[k][0].value()
if isinstance(knob, nuke.Link_Knob):
knob = knobs[k][0].getLinkedKnob()
value = knob.value()
new_knob = nuke.Array_Knob(k)
new_knob.setValue(value)
p.addKnob(new_knob)
new_knob.setFlag(nuke.STARTLINE)
if isinstance(knob, nuke.Boolean_Knob):
new_knob = nuke.Boolean_Knob(k)
new_knob.setValue(value)
p.addKnob(new_knob)
new_knob.setFlag(nuke.STARTLINE)
if isinstance(knob, nuke.Int_Knob):
new_knob = nuke.Int_Knob(k)
new_knob.setValue(value)
p.addKnob(new_knob)
new_knob.setFlag(nuke.STARTLINE)
if isinstance(knob, nuke.Enumeration_Knob):
new_knob = nuke.Enumeration_Knob(k, k, knobs[k][0].values())
new_knob.setValue(value)
p.addKnob(new_knob)
new_knob.setFlag(nuke.STARTLINE)
if isinstance(knob, nuke.File_Knob):
new_knob = nuke.File_Knob(k)
new_knob.setValue(value)
p.addKnob(new_knob)
new_knob.setFlag(nuke.STARTLINE)
p.showModalDialog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment