如何在 Python 中使用 os.getcwd() 正确向 Maya 发送命令
所以我使用套接字将一些输出发送到 Maya。不幸的是,我从 Maya 得到的信息根本没有意义。我试图引用一个变量,同时转义引用,但一旦到达 Maya,它就会失败。这是代码:
currentDir = os.getcwd()
maya.send('python("import os; import sys; os.chdir(\''+currentDir+'\'); sys.path.append(\''+currentDir+'\'); import pythonExec; pythonExec.main()")')
这是我得到的输出:
python("import os; import sys; os.chdir('C:\Users\pneumonic\Documents\My Dropbox\pythonTesting'); sys.path.append('C:\Users\pneumonic\Documents\My Dropbox\pythonTesting'); import pythonExec; pythonExec.main()");
import os; import sys; os.chdir('C:UserspneumonicDocumentsMy DropboxpythonTesting'); sys.path.append('C:UserspneumonicDocumentsMy DropboxpythonTesting'); import pythonExec; pythonExec.main()
# Error: line 1: [Error 2] The system cannot find the file specified: 'C:UserspneumonicDocumentsMy DropboxpythonTesting'
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# WindowsError: [Error 2] The system cannot find the file specified: 'C:UserspneumonicDocumentsMy DropboxpythonTesting' #
它删除了路径名中的每个 \ 字符。我尝试使用双引号而不是单引号,我从中得到的输出是这样的:
python("import os; import sys; os.chdir("C:\Users\pneumonic\Documents\My Dropbox\pythonTesting"); sys.path.append("C:\Users\pneumonic\Documents\My Dropbox\pythonTesting"); import pythonExec; pythonExec.main()");
// Error: python("import os; import sys; os.chdir("C:\Users\pneumonic\Documents\My Dropbox\pythonTesting"); sys.path.append("C:\Users\pneu€ //
// Error: Line 1.42: Syntax error //
我不确定我做错了什么,但这显然是我的问题。感谢您提前的帮助。
旁注-我的大部分开发都是在 OSX 上进行的,一切都在 OSX 上完美运行,而且我没有做任何特定于操作系统的事情,所以我很困惑为什么我在 Windows 上遇到这么多问题。
So I'm using sockets to send some output to maya. Unfortunately what I'm getting back from Maya doesn't make sense at all. I'm trying to quote a variable, while escaping the quote, but once it gets to Maya it fails. Here's the code:
currentDir = os.getcwd()
maya.send('python("import os; import sys; os.chdir(\''+currentDir+'\'); sys.path.append(\''+currentDir+'\'); import pythonExec; pythonExec.main()")')
Here's the output I get:
python("import os; import sys; os.chdir('C:\Users\pneumonic\Documents\My Dropbox\pythonTesting'); sys.path.append('C:\Users\pneumonic\Documents\My Dropbox\pythonTesting'); import pythonExec; pythonExec.main()");
import os; import sys; os.chdir('C:UserspneumonicDocumentsMy DropboxpythonTesting'); sys.path.append('C:UserspneumonicDocumentsMy DropboxpythonTesting'); import pythonExec; pythonExec.main()
# Error: line 1: [Error 2] The system cannot find the file specified: 'C:UserspneumonicDocumentsMy DropboxpythonTesting'
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# WindowsError: [Error 2] The system cannot find the file specified: 'C:UserspneumonicDocumentsMy DropboxpythonTesting' #
It's dropping every \ character in the path name. I've tried using a double quote instead of a single quote and the output I get from that is this:
python("import os; import sys; os.chdir("C:\Users\pneumonic\Documents\My Dropbox\pythonTesting"); sys.path.append("C:\Users\pneumonic\Documents\My Dropbox\pythonTesting"); import pythonExec; pythonExec.main()");
// Error: python("import os; import sys; os.chdir("C:\Users\pneumonic\Documents\My Dropbox\pythonTesting"); sys.path.append("C:\Users\pneu€ //
// Error: Line 1.42: Syntax error //
I'm not sure what it is I'm doing wrong, but it's obviously something on my part. Thanks for the help in advance.
sidenote- I do most of my development on OSX and everything works perfectly on there and I'm not doing anything that's os specific so I'm confused as to why I'm having so many issues with windows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终这是一个非常简单的修复:
不过我不相信答案。我从 http://forums.cgsociety 得到答案.org/showpost.php?p=6890246&postcount=2 来自 r4inm4ker。 :)
Ended up being a very simple fix:
I take no credit for the answer though. I got the answer from http://forums.cgsociety.org/showpost.php?p=6890246&postcount=2 from r4inm4ker. :)