PDA

View Full Version : How can I properly send a command to Maya using os.getcwd() in Python


ArrantSquid
03-01-2011, 05:48 AM
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 backslash \ 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, and I originally thought it was something I was doing - which it may be - but this works perfectly on OSX and I'm not using any non-standard libraries to do any of this. It seems idiotic that Python/Maya doesn't understand the same command from OS to OS to escape a character.

r4inm4ker
03-01-2011, 06:36 AM
I would suggest you to convert the windows path to "universal path" first,i.e:


path = 'C:\\Users\\pneumonic\\Documents\\My Dropbox\\pythonTesting'
convertPath = path.replace('\\','/')


or use '/' from the start insted of '\' .

Hope that helps.

ArrantSquid
03-01-2011, 07:11 AM
You rock hard! I can't believe I didn't think about that. Although the longer you look at things, the more likely you're going to keep doing the same thing. :) Thanks again!

dbsmith
03-01-2011, 07:26 PM
Better yet, use pythons os.path functions:
http://docs.python.org/library/os.path.html

such as:
os.path.normpath, os.path.abspath, os.path.join

This uses less assumptions about the system platform you're working on and how they deal with /s in their paths.

ArrantSquid
03-02-2011, 03:17 AM
@dbsmith: I actually tried all 3 of those methods (even double combining them to see if something was going amiss) and just retried them and it errors out still. It seems to keep stripping out the \ characters.

CGTalk Moderation
03-02-2011, 03:17 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.