Skip to content

Instantly share code, notes, and snippets.

@mybikeislost
Last active April 29, 2016 17:54
Show Gist options
  • Save mybikeislost/3a28fbc5253b73f3db52160d655fe742 to your computer and use it in GitHub Desktop.
Save mybikeislost/3a28fbc5253b73f3db52160d655fe742 to your computer and use it in GitHub Desktop.
growCanvasToBbox
############################################
# growCanvasToBbox 0.1
# 2016.04.29
# Brian Willard
# info@brianwillard.org
#
# analyze bbox over a sequence and create a reformat scaled to the optimal size to contain the bbox.
# create a 2nd reformat expression linked to shrink the canvas back to the original size.
############################################
def growCanvasToBbox():
selNode=nuke.selectedNode()
r = nuke.root().frameRange()
range, views = nuke.getFramesAndViews('Calculate Bbox', '%s-%s' %(r.first(), r.last()) )
curFirst, curLast = range.split('-')
# shuffle black into rgba
shuffle = nuke.nodes.Shuffle()
shuffle['red'].setValue('black')
shuffle['green'].setValue('black')
shuffle['blue'].setValue('black')
shuffle['alpha'].setValue('black')
shuffle.setInput(0, selNode)
# sum the bbox across frames by using a FrameBlend node
fb = nuke.nodes.FrameBlend()
fb.setInput(0, shuffle)
fb['userange'].setValue(True)
fb['startframe'].setValue(int(curFirst))
fb['endframe'].setValue(int(curLast))
# grow the format by checking if the bbox fits inside the reformat node
reformat = nuke.nodes.Reformat(label="GrowCanvas", type='scale', resize='none', pbb=True)
reformat.setInput(0, fb)
scale=1.0
while reformat.bbox().x() < 0:
print "reformat.bbox().x(): ", reformat.bbox().x()
scale = scale + 0.1
reformat['scale'].setValue(scale)
while reformat.bbox().y() < 0:
print "reformat.bbox().y(): ", reformat.bbox().y()
scale = scale + 0.1
reformat['scale'].setValue(scale)
while reformat.bbox().w()+ reformat.bbox().x() > reformat.format().width():
print 'reformat.bbox().w()+ reformat.bbox().x() ', reformat.bbox().w()+ reformat.bbox().x()
scale = scale + 0.1
reformat['scale'].setValue(scale)
while reformat.bbox().h()+ reformat.bbox().y() > reformat.format().height():
print 'reformat.bbox().h()+ reformat.bbox().y() ', reformat.bbox().h()+ reformat.bbox().y()
scale = scale + 0.1
reformat['scale'].setValue(scale)
nuke.delete(shuffle)
nuke.delete(fb)
unFormat = nuke.nodes.Reformat(label="ShrinkCanvas", type='scale', resize='none', pbb=True)
unFormat.setInput(0, reformat)
unFormat['scale'].setExpression("1/parent.%s.scale" % reformat['name'].value())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment