Maya 2014 - Python - PIL library issue

193 views
Skip to first unread message

francois bonnard

unread,
Mar 3, 2015, 11:13:41 AM3/3/15
to python_in...@googlegroups.com
Hey folks

I am running Maya 2014 os windows 8.1 64 bits.

I have dowloaded python PIL from this URL and have installed the folder with the files in ...maya2014/python/Lib/site-packages/

Here is my script :

import os
import maya.cmds as cmds
from PIL import Image

#change the path here
path = "/Users/francesco/Desktop/Géosésame/Maya/sourceimages/selectionMaya"


#list the items in the folder
dir = os.listdir(path)

junk = [".mayaSwatches", ".DS_Store", ".picasa.ini"]


for texture in dir:
    if texture not in junk:
        #Load Image in Pillow
        img = Image.open(texture)
#create a nurbsPlane for each texture
        mesh = cmds.nurbsPlane(name = texture)
        #create a shader
        shader=cmds.shadingNode("blinn",asShader=True)
        #a file texture node
        name = texture.split(".")[0]
        file_node=cmds.shadingNode("file", name = "file_" + name, asTexture=True)
        #assign a texture
        cmds.setAttr( "file_" + name +'.fileTextureName', path +'/'+ texture, type="string")

        # a shading group
        shading_group= cmds.sets(renderable=True,noSurfaceShader=True,empty=True)
        #connect shader to sg surface shader
        cmds.connectAttr('%s.outColor' %shader ,'%s.surfaceShader' %shading_group)
        #connect file texture node to shader's color
        cmds.connectAttr('%s.outColor' %file_node, '%s.color' %shader)
        cmds.select(mesh)
        cmds.hyperShade( assign=shader)

Here is the issue :

# Error: line 1: IOError: file C:\Program Files\Autodesk\Maya2014\Python\lib\site-packages\PIL\Image.py line 1952: 2 # 

Obviously the issue is on this line : 

img = Image.open(texture)

What Do I miss ?

Thanls for the help

Fransua


Chris Lesage

unread,
Mar 3, 2015, 11:27:55 AM3/3/15
to python_in...@googlegroups.com
My first guess would be the accent codes in the name in your path:
path = "/Users/francesco/Desktop/Géosésame/Maya/sourceimages/selectionMaya"

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/a7e7c289-b5e4-4fd1-a7c2-c20b511b074d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

francois bonnard

unread,
Mar 3, 2015, 11:35:49 AM3/3/15
to python_in...@googlegroups.com
Nope. I have tried also with a path without accent
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Chris Lesage

unread,
Mar 3, 2015, 11:38:17 AM3/3/15
to python_in...@googlegroups.com
Ah yes, I did a test, and I could see an accented path too. Try to see what it is seeing when it fails. You can catch the exception with IOError:

try:
    img = Image.open(texture)
except IOError:
    print 'cannot open', img

Maybe there are non image files in that directory? You might have to add more arguments to junk.


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/bf7994c5-8580-4f35-86f9-d2d582527bff%40googlegroups.com.

francois bonnard

unread,
Mar 3, 2015, 11:43:45 AM3/3/15
to python_in...@googlegroups.com

I got this message : # Error: line 1: unindent does not match any outer indentation level # 

I guess I made a wrong indent somewhere ?

import os
import maya.cmds as cmds
from PIL import Image

#change the path here
path = "/Users/francesco/Desktop/selectionMaya"


#list the items in the folder
dir = os.listdir(path)

junk = [".mayaSwatches", ".DS_Store", ".picasa.ini"]





for texture in dir:
    if texture not in junk:
try:
img = Image.open(texture)
except IOError:
print 'cannot open', img


#Get Ratio
        #img.size[0] -> width
        #img.size[1] -> height
        #ratio = img.size[0]/img.size[1]

francois bonnard

unread,
Mar 3, 2015, 11:45:07 AM3/3/15
to python_in...@googlegroups.com
better : cannot open# Error: line 1: NameError: file <maya console> line 18: name 'img' is not defined # 

with this code

import os
import maya.cmds as cmds
from PIL import Image

#change the path here
path = "/Users/francesco/Desktop/selectionMaya"

#list the items in the folder
dir = os.listdir(path)

junk = [".mayaSwatches", ".DS_Store", ".picasa.ini"]

for texture in dir:
    if texture not in junk:
try:
img = Image.open(texture)
except IOError:
print 'cannot open', img
#Get Ratio
        #img.size[0] -> width
        #img.size[1] -> height
        #ratio = img.size[0]/img.size[1]

        #create a nurbsPlane for each texture
        mesh = cmds.nurbsPlane(name = texture)
        #create a shader
        shader=cmds.shadingNode("blinn",asShader=True)
        #a file texture node
        name = texture.split(".")[0]
        file_node=cmds.shadingNode("file", name = "file_" + name, asTexture=True)
        #assign a texture
        cmds.setAttr( "file_" + name +'.fileTextureName', path +'/'+ texture, type="string")

        # a shading group
        shading_group= cmds.sets(renderable=True,noSurfaceShader=True,empty=True)
        #connect shader to sg surface shader
        cmds.connectAttr('%s.outColor' %shader ,'%s.surfaceShader' %shading_group)
        #connect file texture node to shader's color
        cmds.connectAttr('%s.outColor' %file_node, '%s.color' %shader)
        cmds.select(mesh)
        cmds.hyperShade( assign=shader)

francois bonnard

unread,
Mar 3, 2015, 11:56:23 AM3/3/15
to python_in...@googlegroups.com
I wonder if i need to install also python 2.6 on my computer ?

Ricardo Viana

unread,
Mar 3, 2015, 11:59:15 AM3/3/15
to python_in...@googlegroups.com
You say you’re on Windows right?
the path 

"/Users/francesco/Desktop/Géosésame/Maya/sourceimages/selectionMaya

is a Mac one.

maybe it should be “C://something"


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Chris Lesage

unread,
Mar 3, 2015, 12:02:53 PM3/3/15
to python_in...@googlegroups.com
Hi Francois,

You have some spaces and some tabs in your code that has an indentation error. The try/except lines are tabs. That is why.


francois bonnard

unread,
Mar 3, 2015, 12:03:06 PM3/3/15
to python_in...@googlegroups.com
Nope. If I point on an non existing file, here is the msg :

# Error: line 1: WindowsError: file <maya console> line 10: 3 # 

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/n6pOGdlhJjs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/ED9A4F7C-BC57-40AE-B983-BF5CA20F9EBD%40gmail.com.

francois bonnard

unread,
Mar 3, 2015, 12:18:34 PM3/3/15
to python_in...@googlegroups.com
Stop me if i am wrong, but i guess i need to install last version of python and setup a variable in a maya.env file ?

Justin Israel

unread,
Mar 3, 2015, 1:24:48 PM3/3/15
to python_in...@googlegroups.com

I will stop you.
A standalone python has nothing to do with the problem as you are using the embedded python version within Maya.
You seem to be bounding between PIL issues and standard python user syntax errors. My suggestion would be to simplify and confirm you can open a basic image in PIL. Just try to successful open an absolute path to one of your images, on a single line in your script editor. Maybe you are trying to open a format not supported by PIL.
In your most recent user error, when you catch an exception from failing to open a file, you forget to "continue" and allow the rest of the following code to operate on the "img" variable that was never defined.


francois bonnard

unread,
Mar 3, 2015, 2:19:36 PM3/3/15
to python_in...@googlegroups.com
Thanks Justin. Probably you are right. I will try tomorrow os.path.isfile(texture) to check if there is trouble with the path.

francois bonnard

unread,
Mar 4, 2015, 4:50:49 AM3/4/15
to python_in...@googlegroups.com
import os
import maya.cmds as cmds
from PIL import Image
path = "C:/Users/francesco/Desktop/selectionMaya"
dir = os.listdir(path)
junk = [".mayaSwatches", ".DS_Store", ".picasa.ini"]
for texture in dir:
    if texture not in junk:
        print texture   

with this script I got :  
a.jpg
b.jpg
c.JPG
d.JPG
e.JPG
f.JPG
g.JPG
h.jpg
i.jpg
j.jpg
k.JPG
l.JPG
m.JPG
n.JPG

If I replace "print texture" with "print os.path.isfile(texture)" I got 14 times "false"

I am lost, guys !

Chris Lesage

unread,
Mar 4, 2015, 5:04:39 AM3/4/15
to python_in...@googlegroups.com
Here is a relevant Stack Overflow Question:

It seems you need to test the full path with os.path.isfile() but listdir() is returning just filenames.



Marcus Ottosson

unread,
Mar 4, 2015, 5:06:17 AM3/4/15
to python_in...@googlegroups.com

Ok, slow down a little. Let’s take this one step at a time.

os.path.isfile takes a path as input. If that path is not absolute - i.e. does not have the root/drive-letter included - then it will assume that the string you gave it is relative to the current working directory, which typically is the directory from which you run the script.

For example, if you are here:

c:\

And you pass a filename, such as a.jpg to os.path.isfile, then os.path.isfile will assume that you meant:

c:\a.jpg

To instead have it look inside your selectionMaya directory, you will have to pass the full path instead.

>>> import os
>>> full_path = os.path.join("C:/Users/francesco/Desktop/selectionMaya", "a.jpg")
>>> print os.path.isfile(full_path)
True

francois bonnard

unread,
Mar 4, 2015, 5:43:40 AM3/4/15
to python_in...@googlegroups.com
the trick was : 

path = "C:/Users/francesco/Desktop/selectionMaya"
myFile = "C:/Users/francesco/Desktop/selectionMaya/"
dir = os.listdir(path)

junk = [".mayaSwatches", ".DS_Store", ".picasa.ini"]

for texture in dir:
    if texture not in junk:
img = Image.open(os.path.join(myFile,texture))

Thanks guys !!!!

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/n6pOGdlhJjs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages