Skip to content

Instantly share code, notes, and snippets.

@sean-c-johnston
Last active August 29, 2015 13:59
Show Gist options
  • Save sean-c-johnston/10633830 to your computer and use it in GitHub Desktop.
Save sean-c-johnston/10633830 to your computer and use it in GitHub Desktop.
Nuke script. Down-rezzes and re-draws the selected read node in the Node Graph, using Constant nodes as pixels. Accounts for aspect ratio. More than 20x20 not recommended for performance reasons.
def draw_constant(res=10):
"""Re-draws a Read node using Constant nodes as pixels."""
# Checks that the user has selected a Read node.
try:
if nuke.selectedNode().Class() != "Read":
# Pushes pop up message to the user
nuke.message("No Read node selected to re-draw!")
return False
except ValueError:
nuke.message("No node selected!")
return False
else:
# Sets the Image Height/Width.
ih = nuke.selectedNode().height()
iw = nuke.selectedNode().width()
# Sets the Node Grid Height/Width, making sure to maintain Aspect
# Ratio.
h = res
w = res * iw / ih
# The loops construct the grid layout of the pixels.
for y in range(h):
for x in range(w):
# Construct the Constant nodes and set their position.
c = nuke.nodes.Constant(xpos=x * 70, ypos=-y * 70)
# Sets Constant colour to sampled pixels.
c['color'].setValue([
nuke.sample(nuke.selectedNode(), "r",
0.5 + (x * (iw / w)), 0.5 + (y * (ih / h))),
nuke.sample(nuke.selectedNode(), "g",
0.5 + (x * (iw / w)), 0.5 + (y * (ih / h))),
nuke.sample(nuke.selectedNode(), "b",
0.5 + (x * (iw / w)), 0.5 + (y * (ih / h))),
nuke.sample(nuke.selectedNode(), "a",
0.5 + (x * (iw / w)), 0.5 + (y * (ih / h)))
])
draw_constant()
@shidarin
Copy link

I forked this to https://gist.github.com/shidarin/10748812 and made some changes, here's my logic behind them:

  1. Instead of calling nuke.selectedNode() all this time, just use the try/except/else to assign selectedNode() to a variable, use the else to check the variable for it's Class. This also let's us put the minimum amount of code needed in the try.
  2. Avoid 1 and 2 letter variables unless you're in a for loop, and even then try and have it be something descriptive enough. Avoid overlong, but they need to be long enough so that you can read a line of code without having to do lots of translation in your head.
  3. Use xrange instead of range, as xrange is more efficient in Python 2x. Range generates a full list- so unless you need the entire list all at once, it's better to use xrange to generate and call each item one at a time. Better memory use. In python 3x, range has been replaced by xrange.
  4. The sample position never changes for the entire loop, so let's just assign the calculation of 0.5 + (x * (imgWidth / gridWidth)) to a variable.
  5. This is also the perfect place for a list comprehension! You call the same function 4 times, with only one arg changed for each iteration. Your goal is to make a list. colorValues = [nuke.sample(node, color, sampleX, sampleY) for color in ['r', 'g', 'b', 'a']] gets it done with no fuss and generates a 4 long list with rgba values, which we then use when we set the constant's color value.
  6. I didn't change the variable c representing the constant node, but I probably should have.
  7. This isn't a standalone script, so there's no need for the if __name__ == '__main__':

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment