Not to beat a dead horse, but there are a couple other things that may be worth
considering:
1) The views your node is trying to write may not match nuke.views().
2) If your code is running on a farm, there’s always the chance of collisions,
even if you check for an existing directory, so it’s a good idea to handle
EEXIST exceptions gracefully. If you wanted to go a step further, you could
just skip the existence check altogether and simply try to create the
directory, ignore an EEXIST exception if raised, and move on.
Another take on the same:
def createWriteDirs():
import os
import re
baseDir = os.path.dirname(nuke.filename(nuke.thisNode()))
viewTokenRE = re.compile(r'%V')
if viewTokenRE.search(baseDir):
nodeViews = nuke.thisNode()['views'].value().split()
outDirs = [nuke.filenameFilter(viewTokenRE.sub(v, baseDir)) for v in
nodeViews]
else:
outDirs = [nuke.filenameFilter(baseDir)]
for outDir in outDirs:
if not os.path.exists(outDir):
print 'Creating output directory: %s' % outDir
try:
os.makedirs(outDir)
except (OSError, IOError) e:
# Don't choke if directory has been created since we checked.
# This can be an issue with farm renders.
import errno
if e.errno != errno.EEXIST:
raise
-Nathan
From: Bill Gilman
Sent: Friday, April 27, 2012 11:42 AM
To: Nuke user discussion
Subject: Re: [Nuke-users] Auto-create directories via Write node -
Python"before render"?
Here's one for stereo work (works for non-stereo as well):
def createWriteDir():
import nuke, os
import re
#view = nuke.thisView()
views = nuke.views()
file = nuke.filename(nuke.thisNode())
dir = os.path.dirname(file)
viewdirs = []
try:
if re.search('%V', dir).group(): # replacing %V with view name
for v in views:
viewdirs.append(re.sub('%V', v, dir))
except:
pass
if len(viewdirs) == 0:
osdir = nuke.callbacks.filenameFilter(dir)
if not os.path.isdir(osdir):
os.makedirs (osdir)
else:
for vd in viewdirs:
osdir = nuke.callbacks.filenameFilter(vd)
if not os.path.isdir(osdir):
os.makedirs (osdir)
print 'Directory (with viewname) created: %s' % (osdir)
nuke.addBeforeRender(createWriteDir)
_______________________________________________
Nuke-users mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-users