2
votes

I am Trying to add a custom path filed in my GUI but the problem is that when i use the command

cmds.fileDialog2(filemode=3,dialogStyle =1) 

i get a file path like

C:\Users\anoorani\Desktop\Test

However Maya only seems to be reading paths like

C:/Users/anoorani/Desktop/Test

The backticks seem to be a problem is there a way to replace "\" with "/" in python maya.....?

1
os.path.normpath(filePath) has worked for me so far... - Argiri Kotsaris
you maybe can do out.replace('\\', '/') - Saullo G. P. Castro
@ArgiriKotsaris: I think you should post that as an answer. Just add a link and explain what it does, and there's not much more to say. - abarnert
It's good practice to always swap your paths to forward slashes , even on windows, since Windows works with forward slashes and backslashes are typo magnets. - theodox
Thankyou so much!!!!@ Argiri Kotsaris the os.path.normpath worked for me!!!! - Arnvfx

1 Answers

3
votes

Acording to @ArgiriKotsaris note, you can use os.path.normpath(path):

Normalize a pathname by collapsing redundant separators and up-level references.

So that A//B, A/B/, A/./B and A/foo/../B all become A/B.

This string manipulation may change the meaning of a path that contains symbolic links.

On Windows, it converts forward slashes to backward slashes.

So your code be:

import maya.cmds as cmds
import os

path = cmds.fileDialog2(fm=3,dialogStyle =1)
path = path and os.path.normpath(path[0])

Or if you want to always using forward slashes, then no needs to os module and change last line to:

path = path and path[0].replace('\\', '/')

Note that name of file mode argument for fileDialog2 is fileMode or fm and not filemode.

Also fileDialog2 return a list of paths or None.