Skip to content

Instantly share code, notes, and snippets.

@harut-harutyunyan
Created November 30, 2022 09:58
Show Gist options
  • Save harut-harutyunyan/e021affd351750be0ff8ae8a10b3f5cc to your computer and use it in GitHub Desktop.
Save harut-harutyunyan/e021affd351750be0ff8ae8a10b3f5cc to your computer and use it in GitHub Desktop.
Convert nuke Shuffle2 to Shuffle
import nuke
DELETE_AFTER = False
COLOR_CODE = True
COLOR_CODE_COL = 4282078975
NUKE_VERSION = nuke.NUKE_VERSION_MAJOR + nuke.NUKE_VERSION_MINOR/10
DEFAULT_MAPPINGS = "4 rgba.red 0 0 rgba.red 0 0 rgba.green 0 1 rgba.green 0 1 rgba.blue 0 2 rgba.blue 0 2 rgba.alpha 0 3 rgba.alpha 0 3"
KNOB_LIST = ["red", "green", "blue", "alpha", "black", "white", "red2", "green2"]
VALUE_LIST = KNOB_LIST+["blue2", "alpha2"]
CONVERTED_SUFFIX = "conv"
def get_dependents(node):
output = []
for dep in node.dependent():
for i in range(dep.inputs()):
if dep.input(i) == node:
output.append((i, dep))
return output
def get_conversion_list(selected=False, shuffle_copy=False, disabled=False):
node_list = nuke.selectedNodes("Shuffle2") if selected else nuke.allNodes("Shuffle2")
if not disabled:
node_list = [s for s in node_list if s["disable"].getValue() != 1]
if shuffle_copy:
return node_list
return [s for s in node_list if s.inputs()==1]
def get_mappings_str(node):
m_string = [i for i in node["knobs"].value().splitlines() if i.startswith(" mappings")]
if m_string:
m_string = m_string[0].split("\"")[1]
else:
m_string = DEFAULT_MAPPINGS
return m_string
def string_to_mapping(m_string):
m_list = m_string.split()
num = m_list.pop(0)
mapping = ["" for i in range(int(num))]
while len(m_list) > 0:
mm = m_list[:6]
index = int(mm.pop())
row = int(mm.pop())
if row == 1:
index += 4
mm.pop(2)
x = (int(mm[1]), mm[0], mm[2])
mapping[index]=x
m_list = m_list[6:]
return mapping
def get_unknown_shuffle_knobs(sh2):
from1 = 0
from2 = 0
in1 = "rgba"
in2 = "none"
out1 = "rgba"
out2 = "none"
for line in sh2["knobs"].value().splitlines():
if "fromInput1" in line:
if "{0}" in line:
from1 = 0
elif "{1}" in line:
from1 = 1
if "fromInput2" in line:
if "{0}" in line:
from2 = 0
elif "{1}" in line:
from2 = 1
if "in1" in line:
in1 = line.split()[-1]
if "in2" in line:
in2 = line.split()[-1]
if "out1" in line:
out1 = line.split()[-1]
if "out2" in line:
out2 = line.split()[-1]
m_string = get_mappings_str(sh2)
mappings = string_to_mapping(m_string)
return (from1, from2, in1, in2, out1, out2, mappings)
def get_shuffle_knobs(sh2):
if NUKE_VERSION >= 12.1:
from1 = sh2["fromInput1"].getValue()
from2 = sh2["fromInput2"].getValue()
in1 = sh2["in1"].value()
in2 = sh2["in2"].value()
out1 = sh2["out1"].value()
out2 = sh2["out2"].value()
mappings = sh2["mappings"].getValue()
return (from1, from2, in1, in2, out1, out2, mappings)
else:
return get_unknown_shuffle_knobs(sh2)
def copy_konb_values(from_n, to_n):
if COLOR_CODE:
to_n["tile_color"].setValue(COLOR_CODE_COL)
else:
to_n["tile_color"].setValue(int(from_n["tile_color"].getValue()))
to_n["hide_input"].setValue(from_n["hide_input"].getValue())
to_n["label"].setValue(from_n["label"].getValue())
to_n["postage_stamp"].setValue(from_n["postage_stamp"].getValue())
to_n["disable"].setValue(from_n["disable"].getValue())
def create_old_shuffle(sh2):
name = "{}_{}".format(sh2.name(), CONVERTED_SUFFIX)
shuffle_copy = sh2.inputs() > 1
if shuffle_copy:
old = nuke.nodes.ShuffleCopy(name=name)
else:
old = nuke.nodes.Shuffle(name=name)
copy_konb_values(sh2, old)
dep = sh2.dependencies()
index = 0
while dep:
old.setInput(index, dep.pop(0))
index += 1
if shuffle_copy:
old.setXYpos(sh2.xpos() + (0 if DELETE_AFTER else (sh2.screenWidth()+10)), sh2.ypos())
dependent = get_dependents(sh2)
while dependent:
index, node = dependent.pop()
node.setInput(index, old)
else:
old.setXYpos(sh2.xpos(), sh2.ypos()-(0 if DELETE_AFTER else (sh2.screenHeight()+10)))
sh2.setInput(0, old)
sh2["disable"].setValue(1)
return (old, shuffle_copy)
def generate_mappings_list(from1, from2, in1, in2, out1, out2, mappings, shuffle_copy=False):
channels1 = [c for c in nuke.channels() if c.startswith(in1)]
channels2 = [c for c in nuke.channels() if c.startswith(in2)]
mappings_list = []
for i, m in enumerate(mappings):
mapp = {}
which, inpt, outpt = m
if which == -1:
val = inpt
else:
if which == 0:
if from1 == 0 and shuffle_copy:
val = VALUE_LIST[channels1.index(inpt)+6]
else:
val = VALUE_LIST[channels1.index(inpt)]
if which == 1:
if from1 == 0 and shuffle_copy:
val = VALUE_LIST[channels2.index(inpt)]
else:
val = VALUE_LIST[channels2.index(inpt)+6]
knob = KNOB_LIST[i]
mapp["knob"] = knob
mapp["which"] = which
mapp["value"] = val
mappings_list.append(mapp)
return mappings_list
def convert_to_old(sh2):
old, shuffle_copy = create_old_shuffle(sh2)
from1, from2, in1, in2, out1, out2, mappings = get_shuffle_knobs(sh2)
mappings_list = generate_mappings_list(from1, from2, in1, in2, out1, out2, mappings, shuffle_copy)
old["in"].setValue(in1)
old["out"].setValue(out1)
old["in2"].setValue(in2)
old["out2"].setValue(out2)
if from1 == 0 and shuffle_copy:
old["in2"].setValue(in1)
if from2 != 0 and shuffle_copy:
old["in"].setValue(in2)
for ch in mappings_list:
knob = ch["knob"]
val = ch["value"]
old[knob].setValue(val)
if DELETE_AFTER:
nuke.delete(sh2)
if __name__ == "__main__":
selected=True
shuffle_copy=True
disabled=False
conversion_list = get_conversion_list(selected, shuffle_copy, disabled)
for sh2 in conversion_list:
convert_to_old(sh2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment