Last active
August 29, 2015 13:59
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I forked this to https://gist.github.com/shidarin/10748812 and made some changes, here's my logic behind them:
xrange
instead ofrange
, asxrange
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 usexrange
to generate and call each item one at a time. Better memory use. In python 3x,range
has been replaced byxrange
.0.5 + (x * (imgWidth / gridWidth))
to a variable.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.c
representing the constant node, but I probably should have.__name__ == '__main__':