简体   繁体   English

如何在Python中使用os.getcwd()正确向Maya发送命令

[英]How can I properly send a command to Maya using os.getcwd() in Python

So I'm using sockets to send some output to maya. 所以我正在使用套接字将一些输出发送到maya。 Unfortunately what I'm getting back from Maya doesn't make sense at all. 不幸的是,我从Maya那里得到的回报根本没有任何意义。 I'm trying to quote a variable, while escaping the quote, but once it gets to Maya it fails. 我试图在转义引用时引用一个变量,但是一旦它到达Maya,它就会失败。 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. sidenote-我在OSX上进行了大部分开发工作,并且在那里都能正常工作,并且我没有做任何特定于操作系统的事情,所以我对为什么Windows遇到很多问题感到困惑。

Ended up being a very simple fix: 最终是一个非常简单的解决方案:

path = os.getcwd()
convertPath = path.replace('\\','/')

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. 我从r4inm4ker的http://forums.cgsociety.org/showpost.php?p=6890246&postcount=2获得了答案。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM