From: Greg Ercolano <erco@(email surpressed)>
Subject: Re: [Q+A] Nuke cross platform pathnames
   Date: Wed, 30 Jun 2010 18:45:30 -0400
Msg# 1947
View Complete Thread (12 articles) | All Threads
Last Next
Robert Minsk wrote:
> [posted to rush.general]
> 
> Greg Ercolano wrote:
>> 	Yes; short answer is to see Nuke's own 'customizing' documentation
>> 	on handling file paths across platforms.
> 
> More specifically you will need to define a python function that takes a
> filename as an argument a returns a new filename.  You then need to install it
> as a FilenameFilter callback (nuke.addFilenameFilter) in your sites init.py file.

	Right; I believe the "Nuke-5.1v6-customizing.pdf" documentation cites
	this exact example [sic]:

----
def filenameFix(filename):
    if platform.system() in ("Windows", "Microsoft"):
        return filename.replace("/SharedDisk/", "p:\")
    else:
        return filename.replace("p:\", "/SharedDisk/")
    return filename
----

	..which appears to show how to replace e.g. "/SharedDisk/foo" <-> "p:\foo"
	depending on the platform it's running on.

	However, there's a few things wrong with that example I think; that backslash
	after the "p:" needs to be protected, otherwise python will interpret it as an
	escape character. Also, that final 'return filename' appears to be extraneous,
	as there's no way that line will ever execute.

	So I would suggest the following instead:

----
def filenameFix(filename):
    if platform.system() in ("Windows", "Microsoft"):
        return filename.replace("/SharedDisk/", "p:\\")
    else:
        return filename.replace("p:\\", "/SharedDisk/")
----

	Another way to protect the backslashes would be to use:

		r"p:\"			# same as "p:\\"

	This is pythons little way of protecting all characters between the
	double quotes, the leading 'r' in front of the leading double quote
	means "raw string", protecting the backslash from being mistaken
	for an escape sequence.

	An example that worked for a recent client who wanted to make sure
	UNC style pathnames were used on windows, (eg. "/bart/foo" would become
	"//bart/foo" on windows machines), I suggested they use:

----
import re
import platform
def filenameFix(filename):
    if platform.system() in ("Windows", "Microsoft"):
        return re.sub("^/*bart","//bart",filename)
    else:
        return re.sub("^/*bart","/bart",filename)
----

	..the special regex ensuring that e.g. //bart doesn't become ///bart


-- 
Greg Ercolano, erco@(email surpressed)
Seriss Corporation
Rush Render Queue, http://seriss.com/rush/
Tel: (Tel# suppressed)
Fax: (Tel# suppressed)
Cel: (Tel# suppressed)

Last Next