PDA

View Full Version : python exec() in Maya 2009


Xeash
03-22-2009, 09:59 PM
Hi,
I have scripted python file, test.py with code:

Skeleton={

'Name':'Main',
'Pos':(0,0),
'child_0':{
'Name':"001",
'Pos':(0,100),
'child_0':{
'Name':"002",
'Pos':(0,100),
},
'child_1':{
'Name':"003",
'Pos':(200,100),
},
},
};

I have python 2.5.1 installed on my PC/win XP system....
And when I try to run code in outer interpreter:

def loadFile(dirToFile):
f=open(dirToFile,'r')
data=f.read()
f.close()
return data
a=loadFile('g:\\test\\test.py')
exec(a)

...code works perfectly. 'Skeleton' dictionary is placed in globals() and so on.
BUT! I don't understand why if I use the same code in Maya 2009 it returns the syntax error:

# Error: ('invalid syntax', ('<string>', 1, 11, 'Skeleton={\r\n'))
# Traceback (most recent call last):
# File "<maya console>", line 2, in <module>
# File "<string>", line 1
# Skeleton={
#
# ^
# SyntaxError: invalid syntax

Why is that? I don't know where I am supposed to find problem 'root' even. Any advice?

Thanks anyway for listening :)

Chadrik
03-23-2009, 01:20 AM
it looks like your file is saved with windows newline characters: \r\n

It may be that Maya's python is built without universal newline support. try forcing the file to be opened with universal newline support, like this:


open(dirToFile, 'rU' )

see this for more info: http://docs.python.org/library/functions.html#open

This is one of the many frustrating aspects of working with python on windows, where it is the "odd man out" on many standards. unix/linux/osx use just \n to represent a newline, whereas windows uses \r\n, and python, being born of unix, must be compiled with a flag that tells it to acknowledge window's method as valid newline delimters.

-chad

Xeash
03-23-2009, 05:45 AM
BIG Thanks, Chadrik. It works perfectly :thumbsup:
I'm very-very frustrated about this, but nothing can be done.... Now I will try to keep this fact in mind.
Thanks again!

CGTalk Moderation
03-23-2009, 05:45 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.