PyMel- Getting texture file from a material

So, my first inelegant-yet-functional PyMel script is done and working. The core of it is in this little snippet here, that digs through a mesh's inputs until it finds the texture node on the surface shader:

#Pass through the mesh's shape and return a texture name. 
import pymel as pm

def getTextureFile(geo):

    sg = []

    #Get the shading group from the selected mesh
    raw_sg = pm.listConnections(geo, type='shadingEngine')

    #Rapidly check for duplicates in the list- I do this by checking each item 
    #against an empty list. 
    #If the item is not in the empty list I append it. 
    
    for element in raw_sg:
        if element not in sg:
            sg.append(element)

    #Get some info
    sgInfo = pm.listConnections(sg[0], type='materialInfo')
    fileNode = pm.listConnections(sgInfo[0], type='file')
    textureFile = pm.getAttr(fileNode[0].fileTextureName)



    print 'This is the file', str(textureFile)
    return str(textureFile)
It still leaves a lot to be desired, for instance it doesn't know what to do if a mesh doesn't actually have a texture! But, provided it does, using the win32com module I can then send the texture file information onto Photoshop and Alienbrain and open files/check out texture chains etc etc.

EDIT:
------------------------------------------------------------------------------------------------------------
Thanks to Daydreamer who pointed out the missing pymel reference at the top of the snippet, as well as other issues with the script which I am working through...

After a happy weekend of actually reading part of the official tutorial, I have a slightly better understanding of how to access a mesh's attributes and nodes using PyMel. Here is a snippet of the above snippet that does pretty much the same thing, only using the pymel connections, input and output commands. The intention is only to test the functionality.
    
    import pymel as pm

    geo = pm.ls(sl=True)[0].getShape()

    #Get the shading group from the selected mesh
    sg = geo.outputs(type='shadingEngine')

    #Work through the node to get the file texture name
    sgInfo = sg[0].connections(type='materialInfo')
    
    #It falls apart here if you have no file node! Oops...
    fileNode = sgInfo[0].connections(type='file')

    #Get the file texture name attribute's value
    textureFile = pm.getAttr(fileNode[0].fileTextureName)

    print 'This is the file', str(textureFile)

Getting Maya to talk to Photoshop

Sadly, at work I am not blessed with the latest and greatest Maya versions, and I have to be content with Maya 8.5. That also means I have to be content with Python 2.4.2 in Maya... eh. Its enough to do some cool stuff.

It has been a little pet project of mine to get Photoshop and Maya to talk to each other and share texture information (like texture file locations, etc etc) and after a bit of fussing around, and a total failure to get comTypes to work (missing ctypes module in py 2.4) I have been able to get the win32com module to work within the Maya Python 2.4.2 scripting environment.

I don't think its going to be a two way street, but it does mean I can open a model's source PSD file(s) in Photoshop from within Maya. Kinda a neat little workflow thing.

If anyone wants to share a better method give me a shout!

-Pete

Python, windows and easy install...

Hey peeps, been pretty busy through project crunch but thought I'd post something I found useful.

If you are scripting with python on a regular basis and want to take advantage of additional modules you are eventually going to come up against the ironically named easy_install.

I script in Python, why should I care about easy install? That's an excellent question, me. Easy install, once set up, can automatically download and install modules on request from the command prompt. Just type in the name of the module you want and easy install will download and set it up for you.

The only caveat is installing the installer, which presents all sorts of hiccups if you have never poked around at your system's environment variables before.

If you are having any issues with getting it running on windows, this blog was of immense help to me. I gotta say, even with the pain of getting it set-up (wasn't so bad when explained well) its well worth it!

-Pete

---
Edit:
I noticed that the easy install link was linking to a page that, while helpful, wasn't actually the page about easy install. Fixed!