我正在尝试在 GUI 中添加自定义路径,但问题是当我使用命令时
cmds.fileDialog2(filemode=3,dialogStyle =1)
我得到一个像
这样的文件路径C:\Users\anoorani\Desktop\Test
但是 Maya 似乎只读取类似的路径
C:/Users/anoorani/Desktop/Test
反引号似乎是一个问题 有没有办法在Python Maya中用“/”替换“\”......?
最佳答案
根据 @ArgiriKotsaris 的注释,您可以使用 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.
所以你的代码是:
import maya.cmds as cmds
import os
path = cmds.fileDialog2(fm=3,dialogStyle =1)
path = path and os.path.normpath(path[0])
或者,如果您想始终使用正斜杠,则无需 os
模块并将最后一行更改为:
path = path and path[0].replace('\\', '/')
请注意 fileDialog2 的文件模式参数名称是 fileMode
或 fm
,而不是 filemode
。
此外fileDialog2
返回路径列表或无。
关于python - Maya python 反斜杠替换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21197602/