maya file paths on windows

369 views
Skip to first unread message

Chadrik

unread,
Jun 9, 2009, 6:33:14 PM6/9/09
to python_inside_maya
i've been messing around with maya on windows and i've noticed that
there are some inconsistencies with path slashes that need to be
resolved. maya returns forward slashes (/) on both windows and unix-
like systems, but python tries to use back slashes (\) on windows. as
a result, using os.path.join with paths returned by maya ends up
producing mixed slashes:

>>> os.path.join( workspace(q=1, fullName=1), 'scenes' )
'C:/path/to/project\scenes'

i was wondering how people out there are dealing with this. it seems
that maya and python can both understand paths with mixed slashes, but
are there edge cases where this becomes a problem or where forward
slashes do not work on windows?

pymel provides the Path class, which overrides the / operator to mean
os.path.join, which obviously has the same problem:

>>> workspace.getcwd() / 'scenes'
'C:/path/to/project\scenes'

however, i was considering making this operator smarter. options
include:
1) always join with forward slash
2) choose the joining slash based on the left operand
3) join, then normalize resulting path to forward slash
4) join, then normalize resulting path using slash determined from
left operand

which of these seem like the best measure? is there any option i'm
leaving out?

-chad



Ben Barker

unread,
Jun 9, 2009, 7:31:28 PM6/9/09
to python_in...@googlegroups.com
I stopped using os.path.join and simply use '/'.join because of this issue.
I pretty much just use '/' in all instances now just to be safe.

Another thing that came up here was with network drives.
On Windows we would get the following path:

//ourServer/somepath/foo

And on Linux this:

/ourServer/somepath/foo

Linux can handle both the first and second versions, but Windows requires the first version. So file nodes and things in scenes made in Linux would fail when the same scene was opened in Windows. We solved the problem by forcing our Linux scripts to append the extra / at the beginning.

-Ben

Ofer Koren

unread,
Jun 9, 2009, 7:42:18 PM6/9/09
to python_in...@googlegroups.com
I have seen this for a long time, but it never seemed to create any problems
really. As long as you use the same best-practices you'd use to keep code
cross-platform everything should work fine.

If you still want to go ahead with a smart operator, I'd vote for:

3) join, then normalize resulting path to forward slash

since it is consistent with the way os.path 'wants' to work (which is make
the separator compatible with the OS)

Ofer

Seth Lippman

unread,
Jun 10, 2009, 7:58:55 PM6/10/09
to python_in...@googlegroups.com
Check out: os.path.abspath
given a path string, of any platform, returns the correct slash order
for the host platform. Nice for cross-platform scripting. I wrap most
my paths in this..
Reply all
Reply to author
Forward
0 new messages