Wednesday 18 May 2011

A Simple Nuke file loader

#!/usr/bin/python
import os

def scanForFiles(_dir) :
 # grab all the files in the directory
 files = os.listdir(_dir)
 # create an empty dictionary for the results
 result = {}
 # this will hold our sorted list to be returned
 sortedList = []
 # these are the valid file extensions as a tuple as ends with needs it
 validFiles=("tiff","tif","exr","jpg","jpeg")

 # process each of the files found and see if it's in the format
 # name.#####.ext
 for file in files:
  # so first we try and split on the . and see if we have a valid suffix
  try:
   prefix, frame, suffix = file.split('.')
   if suffix  in validFiles :
    try:
     # if we do we add it to the dictionary (using the frame value)
     result[prefix][0].append(frame)
    except KeyError:
     # we have a new file sequence, so create a new key:value pair
     result[prefix] = [[frame],suffix]
  except ValueError:
    print "ignoring file in directory %s" %(file)

 # now we have a list of data I want to return in the form that nuke needs
 # in this case we need name.%xd.ext so we grab the data and format the string
 for file in result :
  # get the filename
  fileName=file
  # get the extention
  extention=result[file][1]
  # grab the numeric start and end values
  minFrameValue=int(min(result[file][0]))
  maxFrameValue=int(max(result[file][0]))
  # this is a bit of a hack as the values could be different
  # but we basically form the frame pad values
  framePadSize=len(result[file][0][0])
  padString="%%0%dd" %(framePadSize)
  fileNameString="%s.%s.%s" %(fileName,padString,extention)
  # add the data to the list
  sortedList.append([fileNameString,minFrameValue,maxFrameValue])
 # sort it and return
 sortedList.sort()
 return sortedList

path = nuke.getFilename("Select Directory")

files=scanForFiles(path)
print files
for fileData in files :
 fileName=fileData[0].split(".")
 fileName=fileName[0]
 try :
  fileNode = nuke.nodes.Read(file="%s/%s" %(path,fileData[0]),name=fileName)
  fileNode.knob("first").setValue(fileData[1])
  fileNode.knob("last").setValue(fileData[2])
  fileNode.knob("on_error").setValue(3)
  Shuffle = nuke.nodes.Shuffle(inputs=[fileNode],name=fileName)
 except :
  print "error on ",fileName

No comments:

Post a Comment