snip–><–snip

Nuke

Nuke save / restore script editor input

by on Jan.24, 2015, under Nuke, Python

This is something that has bugged me since i started working with nuke, if you doing a little interactive scripting, if you forgot to copy/paste it to your script editor, then it was gone on load / nuke exit.

Her is a snap shot of the code i have in my site menu.py , that is in the NUKE_PATH:

# find the script editor:
import PySide
import PySide.QtGui as QtGui
import os
# get the current qtgui instance
nuke_app = QtGui.QApplication.instance()
# global vars
temp_dir = os.environ['NUKE_TEMP_DIR']
saved_text = '%s/nuke_script_editor.txt' % temp_dir


def get_script_editor():
       '''
              returns the QTextDocument which is the input area for the script editor
       '''
       # get all the widgets
       all_widgets = nuke_app.allWidgets()
       
       script_editor = None
       for w in all_widgets:
           name =  w.objectName()
           # lots of the wigets are not named, ignore them
           if not len(name) == 0:
              if name == 'Script Editor.1':
                   script_editor = w
       
       # get plain text:
       #script_editor.children()[0].toPlainText()
       #get the QTextDocument correctly from the 
       txt_doc = None
       for child in script_editor.children():
           if isinstance(child,PySide.QtGui.QTextDocument):
               txt_doc = child
       return txt_doc

       
def save_script_editor():
       '''
              saves the data from the script edotor to disk
       '''
       script_editor = get_script_editor()
       text = script_editor.toPlainText()
       
       fid = open(saved_text,'w')
       fid.write(text)
       fid.close()
       
       print "script editor saved to: ",saved_text
       

def restore_script_editor():
       '''
              restore the saved script editor data
       '''
       script_editor = get_script_editor()
       
       text = open(saved_text,).read()
       script_editor.setPlainText(text)
       
def checkRootCallbacksSet():
       '''
              setup all the callbacks
       '''
       if nuke.Root().knob('onScriptClose').getValue() == '':
           nuke.addOnScriptClose(save_script_editor, nodeClass='Root')
           
       
       if nuke.Root().knob('onScriptLoad').getValue() == '':
           nuke.addOnScriptLoad(restore_script_editor, nodeClass='Root')
       
       restore_script_editor()

nuke.addOnCreate(checkRootCallbacksSet, nodeClass='Root')

So a quick explanation for what im doing.
Using the QApplication.instance(), im getting the Nuke app, from here i have a convenience method “get_script_editor” that i use to get the QTextDocument that nuke is using to store the script editor data.
I then have 2 methods for saving the data to disk and reading it back from disk.

Then to tie it all together, i use nukes call backs to set it so, when nuke loads for the first time or loads a nuke script, it loads the data from disk. I then use the script close function, this works for either a save / clear or closing the ui, it will write out the script editor data to disk.

I have tested this with nuke 7.0v9, but it should work with all versions of nuke.

Leave a Comment :, , , more...

Capturing Nuke’s stdout and err to a file

by on Jan.20, 2015, under Nuke, Python

Something that has bothered me for quite some time, is not being able to capture Nuke’s STDOUT and STDERR, to a file ot ticket, so that when the Artist come accross an issue and none of the TD’s can help we can save it to a ticket or log for when someone has time.

I was reading through some of the python script in nuke an came accross what i was after at the bottom of:

C:\Program Files\Nuke<version>\plugins\menu.py

So in our site menu.py, i have added the following code that is executed  after the above script is run by nuke:

#################################################################################

# save to an env, so that nuke and any threads that share the environment can find the logger
log_path = os.environ['NUKE_LOGGING_PATH']

import logging
logger = logging.getLogger()
hdlr = logging.FileHandler(log_path)
formatter = logging.Formatter('%(levelname)s %(message)s') #%(asctime)s
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)

# stream redirectors
# pulled from: C:\Program Files\Nuke<version>\plugins\menu.py

class SESysStdIn(SERedirector, nuke.FnPySingleton):
  def readline(self):
    return ""

class SESysStdOut(SERedirector, nuke.FnPySingleton):
  def write(self, out):
    if not len(out.strip()) == 0:
        logger.info(out.strip('\n'))
    nuke.output_redirector(out)

class SESysStdErr(SERedirector, nuke.FnPySingleton):
  def write(self, out):
    if not len(out.strip()) == 0:
        logger.error(out.strip('\n'))
    nuke.stderr_redirector(out)

if nuke.GUI:
  sys.stdin  = SESysStdIn(sys.stdin)
  sys.stdout = SESysStdOut(sys.stdout)
  sys.stderr = SESysStdErr(sys.stderr)
#################################################################################

Then, when nuke ui starts it will create this log and write to it.
The “os.environ[‘NUKE_LOGGING_PATH’]” is because we define a new log for every nuke instance. this is defined when running the init.py in your site config.

Leave a Comment :, more...

Useful links for scripting with Nuke

by on Mar.05, 2012, under Nuke, Python

I have been doing some scripting with Nuke, While the Documentation is great, not being a compositor by trade, knowing the lingo for items in nuke was a pain to get my head around.

So here are some useful links in case i forget what im looking for…

Creating custom panels / widgets and popups in nuke’s python:

http://docs.thefoundry.co.uk/nuke/63/pythondevguide/custom_panels.htm

Visual guide to nuke Knob types:

http://docs.thefoundry.co.uk/nuke/63/ndkdevguide/knobs-and-handles/knobtypes.html

Nuke knob flags (These apply in  python aswell):

http://docs.thefoundry.co.uk/nuke/63/ndkdevguide/knobs-and-handles/knobflags.html

Some useful example scripts i have found:

Pyside webbroswer in nuke:

http://pastebin.com/CC0CJ2Qu

Split multichannel exrs into visible layers in nuke:

http://pastebin.com/G7fFbsXz

Query all the read nodes in a script:

http://pastebin.com/wKFXh3Vf

Path swapping reads and write for a company that has 2 facilities. hmm its hard to guess:

http://pastebin.com/8fK00AMx

Leave a Comment more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!