Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
5.6 kB
1
Indexable
import uuid
import os

#define global variable for post backgroundRender action
IPPostBackgroundRenderNode = None

def createIP():
    if nuke.activeViewer() is not None:
        viewer = nuke.activeViewer().node().fullName()
        IP = nuke.createNode('InputProcessNode')
        nuke.activeViewer().node()['input_process_node'].setValue(IP.fullName())
        IP['snapshot'].setValue("IPSnapshot('%s')" % viewer)
        #set uuid
        IP['uuid'].setValue(str(uuid.uuid4()))
	#create onDestroy Expressoin
        IP['onDestroy'].setValue("try:\n    node = nuke.thisNode()\n    IPRemove(node['uuid'].value())\nexcept:\n    pass")

        #set expressions inside IP
        with IP:
            transform = nuke.toNode('Transform2')
            transform['translate'].setExpression('verPos == 1 || verPos == 3 ? %s.input[value %s.input_number].width - width : 0' % (viewer, viewer), 0)
            transform['translate'].setExpression('verPos == 0 || verPos == 1 ? %s.input[value %s.input_number].height - height : 0' % (viewer, viewer), 1)
            nuke.toNode('ViewerGain1')['value'].setExpression('%s.gain' % viewer)
            nuke.toNode('Transform1')['center'].setExpression('%s.width/2' % viewer,0)
            nuke.toNode('Transform1')['center'].setExpression('%s.height/2' % viewer,1)
        return IP
    else:
        nuke.message('No active viewer available.')

def showIP():
    if nuke.activeViewer() is not None:
        IPVal = nuke.activeViewer().node()['input_process_node'].value()
        if nuke.toNode(IPVal) is not None:
            IPNode = nuke.toNode(IPVal)
            nuke.show(IPNode)
        else:
            createIP()

def IPMirrorX():
    try:
        IPNode = nuke.toNode(nuke.activeViewer().node()['input_process_node'].value())
        knob = IPNode['flop']
        knob.setValue(not knob.value())
    except:
        pass

def IPMirrorY():
    try:
        IPNode = nuke.toNode(nuke.activeViewer().node()['input_process_node'].value())
        knob = IPNode['flip']
        knob.setValue(not knob.value())
    except:
        pass

def IPVersion():
    try:
        IPNode = nuke.toNode(nuke.activeViewer().node()['input_process_node'].value())
        knob = IPNode['view']
        if knob.value() == 'version':
            knob.setValue('viewer')
        else:
            knob.setValue('version')
    except:
        pass

def IPToggleSnapshot():
    try:
        IPNode = nuke.toNode(nuke.activeViewer().node()['input_process_node'].value())
        knob = IPNode['view']
        if knob.value() == 'snapshot':
            knob.setValue('viewer')
        else:
            knob.setValue('snapshot')
    except:
        pass

def IPReset(node = None):
    try:
        if node is None:
            node = nuke.toNode(nuke.activeViewer().node()['input_process_node'].value())

        node['view'].setValue('viewer')
        node['exposure'].setValue(0)
        node['gammaKnob'].setValue(1)
        node['saturation'].setValue(1)
        node['flip'].setValue(False)
        node['flop'].setValue(False)
    except:
        pass

def IPSnapshot(viewer = None):
    if viewer is None:
        viewer = nuke.activeViewer().node().fullName()
        node =  nuke.toNode(nuke.activeViewer().node()['input_process_node'].value())
    else:
        node = nuke.thisNode()

    #set IP input to viewer input
    current = nuke.toNode(viewer).input(int(nuke.expression('[value %s.input_number]' % viewer)))
    node.setInput(0, current)

    #set node for disconnect post render
    global IPPostBackgroundRenderNode
    IPPostBackgroundRenderNode = node

    #set off backgroundRender
    proxySetting = nuke.root().proxy()
    nuke.root().setProxy(False)
    with node:
        nuke.addAfterBackgroundRender(IPPostBackgroundRender)
        write = nuke.toNode('Write1')
        nuke.executeBackgroundNuke(nuke.EXE_PATH, [write], nuke.FrameRanges( str(nuke.frame())), write['views'].value().split(' '), {})
    nuke.root().setProxy(proxySetting)

def IPPostBackgroundRender(context):
    try:
	#undo IP node input
        global IPPostBackgroundRenderNode
        IPPostBackgroundRenderNode.setInput(0, None)
	#refresh snapshot read node
        with IPPostBackgroundRenderNode:
            nuke.toNode('Read1')['reload'].execute()
    except:
        pass
    #remove afterBackgroundRender callback for other background renders
    nuke.removeAfterBackgroundRender(IPPostBackgroundRender)

def IPRemove(uuid = None):
    path = os.path.expanduser('~/.nuke/snapshots/')
    #delete old snapshots
    if uuid == None:
        #list all files
        IPSSpath = os.path.expanduser('~/.nuke/snapshots/')
        files = os.listdir(IPSSpath)
        for file in files:
            filePath = IPSSpath + file
            #continue if file is exr
            if os.path.isfile( filePath ) and file.split('.')[-1] == 'exr':
                #delete exr if older than 24 hours
                if time.time() - os.stat(filePath).st_mtime > 86400:
                    os.remove(filePath)
    #delete uuid based screenshot
    else:
        try:
            os.remove( path + str( uuid ) + '.exr' )
        except:
            pass

nuke.addOnScriptLoad(IPRemove)
nuke.addOnScriptClose(IPRemove)

menubar = nuke.menu('Nuke')
menubar.addCommand("@;showIP", "showIP()", 'alt+p')
menubar.addCommand("@;IPMirrorX", "IPMirrorX()", 'shift+o')
menubar.addCommand("@;IPMirrorY", "IPMirrorY()", 'shift+i')
menubar.addCommand("@;IPVersion", "IPVersion()", 'shift+v')
menubar.addCommand("@;IPToggleSnapshot", "IPToggleSnapshot()", 'shift+n')
menubar.addCommand("@;IPReset", "IPReset()", 'shift+p')