Reputation: 11
I have written an autowrite node which has call backs and a knobChange in it but they stop working when I reopen the nuke script. So initial when the autowrite node is created it works how it's meant to but as soon as I save and reopen the script the write node is disconnected from the callbacks and the knobChange. I appreciate some help with this as I think there is something that I have over looked or missing.
Here's the code - I can be simplied but for now anyway.
import re
# Create write node
w = nuke.createNode('Write', inpanel=True)
count = 1
while nuke.exists('MN_AutoWrite_PUBLISH' + str(count)):
count += 1
w.knob('name').setValue('MN_AutoWrite_PUBLISH' + str(count))
# Create knobs
t = nuke.Tab_Knob("Project Path")
w.addKnob(t)
w.addKnob(nuke.String_Knob('proj_root', 'Project Root', ''))
w.addKnob(nuke.String_Knob('nes', '2D Folder', ''))
w.addKnob(nuke.String_Knob('seq', 'Sequence', ''))
w.addKnob(nuke.String_Knob('projShot', 'Shot', ''))
w.addKnob(nuke.String_Knob('projApp', 'App', ''))
w.addKnob(nuke.String_Knob('file_name', 'File Name', ''))
w.addKnob(nuke.String_Knob('projInit', 'Initials', ''))
w.addKnob(nuke.Text_Knob(''))
w.addKnob(nuke.Text_Knob('File Format'))
w.addKnob(nuke.String_Knob('pub', 'Fullpath', ''))
w.addKnob(nuke.Text_Knob(''))
w.addKnob(nuke.Boolean_Knob('png_bool', 'PNG', True))
w.addKnob(nuke.Boolean_Knob('tiff_bool', 'Tiff', False))
w.addKnob(nuke.Boolean_Knob('exr_bool', 'EXR', False))
w.addKnob(nuke.Text_Knob(''))
w.addKnob(nuke.Boolean_Knob('jpg_bool', 'Jpeg', False))
# Set output settings
w.knob('file_type').setValue('exr')
w.knob('create_directories').setValue('true')
def outputPath():
''' # Set output path '''
output_path = (w.knob('pub').getValue())
w.knob('file').fromScript(output_path)
# Set output path function
def updatePaths():
# global projFile
# global projSeq
# global InitReplacePublish
# global projInit
# Get file paths
saveDir = nuke.root()['name'].value()
dir = saveDir.split("/")
dirUnderScore = saveDir.split("_")
dirDot = saveDir.split(".")
projRoot = dir[0] + "/" + dir[1]
projNes = "/2D/_Renders/"
projSeq = dir[4] + "/"
projShot = dir[5] + "/"
projApp = "Nuke/"
projInit = dirUnderScore[-1].split(".")
projInitDot = projInit[0] + "."
split = dir[-1].split(".")
projFile = re.sub(r"(v\d{3,})", "", split[0] + r".####" + setFileType())
projFileReplace = projFile.replace("__", "_",1)
InitReplacePublish = projFileReplace.replace(projInitDot, "Publish.",1)
#assembles full publhs output path
pub = projRoot+projNes+projSeq+projShot+projApp+"Publish/"+InitReplacePublish
print "--------------------------"
print "projInit:" + projInit [0]
print "projInitDot: " + projInitDot
print "split:" + split[0]
print "projFile: " + projFile
print "projFileReplace: " + projFileReplace
print "InitReplacePublish: " + InitReplacePublish
print "--------------------------"
# Update path knobs
w.knob('proj_root').setValue(projRoot)
w.knob('nes').setValue(projNes)
w.knob('seq').setValue(projSeq)
w.knob('projShot').setValue(projShot)
w.knob('projApp').setValue(projApp)
w.knob('file_name').setValue(InitReplacePublish)
w.knob('projInit').setValue(projInit[0])
w.knob('pub').setValue(pub)
outputPath()
def setFileType():
if w.knob('tiff_bool').getValue():
return ".tif"
elif w.knob('exr_bool').getValue():
return ".exr"
elif w.knob('png_bool').getValue():
return ".png"
elif w.knob('jpg_bool').getValue():
return ".jpg"
else:
print("ERROR: File type not selected")
def setFileTypeTiff():
w.knob('exr_bool').setValue(False)
w.knob('png_bool').setValue(False)
w.knob('jpg_bool').setValue(False)
w.knob('file_type').setValue('tiff')
w.knob('compression').setValue('LZW')
w.knob('datatype').setValue('16 bit')
updatePaths()
def setFileTypeEXR():
w.knob('tiff_bool').setValue(False)
w.knob('png_bool').setValue(False)
w.knob('jpg_bool').setValue(False)
w.knob('file_type').setValue('exr')
updatePaths()
def setFileTypePNG():
w.knob('tiff_bool').setValue(False)
w.knob('exr_bool').setValue(False)
w.knob('jpg_bool').setValue(False)
w.knob('file_type').setValue('png')
w.knob('datatype').setValue('16 bit')
updatePaths()
def setFileTypeJpg():
w.knob('tiff_bool').setValue(False)
w.knob('exr_bool').setValue(False)
w.knob('png_bool').setValue(False)
w.knob('file_type').setValue('jpeg')
updatePaths()
setFileTypePNG()
# Callbacks
def removeCallbacks():
nuke.removeOnScriptSave(updatePaths)
nuke.removeOnScriptClose(updatePaths)
nuke.removeBeforeRender(updatePaths)
nuke.removeOnDestroy(removeCallbacks)
print("REMOVED CALLBACKS")
# Add callbacks
nuke.addOnScriptSave(updatePaths)
nuke.addOnScriptClose(updatePaths)
nuke.addBeforeRender(updatePaths)
nuke.addOnDestroy(removeCallbacks)
w['knobChanged'].setValue('''
k = nuke.thisKnob().name()
if k == "tiff_bool":
setFileTypeTiff()
elif k == "exr_bool":
setFileTypeEXR()
elif k == "jpg_bool":
setFileTypeJpg()
elif k == "png_bool":
setFileTypePNG()
else:
outputPath()
''')
Upvotes: 1
Views: 833
Reputation: 373
nuke.addOnScriptSave(updatePaths)
nuke.addOnScriptClose(updatePaths)
nuke.addBeforeRender(updatePaths)
nuke.addOnDestroy(removeCallbacks)
These callbacks are only active in the script instance you have when you initialize the code. After that, they're gone forever!
If you want, you could define this code in your init.py, Though the code would not persist to other users/installs that way.
You might be better off using the Write node's beforeRender/afterRender callbacks for this code. These are conveniently exposed on the write node as text fields (though you'll have to squish your code in there). Once its in those fields, its saved in the script file as well.
Upvotes: 0