Using Environment Variables for Texture Paths in Maya

I don’t know how well known this fact is, but you can use environment variables in the path attribute in the file node:

In this case, MY_IMAGES is a variable that points to a location where my images reside and I can use it as a shorthand to that location on the file system (a network location or whatever). I’m on windows, but if you used Unix-like systems (or Houdini 😃) you will recognize dollar sign notation. To refer to a variable you don’t encapsulate the name with percentage signs %, you just lead with $.

In any case, this is particularly useful when your source images leave outside of your project or when you are packaging your scenes to be rendered somewhere else but you don’t exactly know how the file structure will look like on the destination. It might still be better to rely on good old workspace.mel and make all your paths relative to the project, but environment variables allow for some interesting possibilities.

At one point I used this feature to change between texture sets on the fly. You can redefine the variable in runtime using python and it will be valid if the rest of the path is correct (that is, if the files exist on the new path).

One caveat is that file nodes do not automatically reload, so you will have to handle that yourself. That’s easy enough and I would think there are good ways to do this, but one I found is to simply get all the texture nodes and re-set the fileTextureName attribute:

import maya.cmds as cmds

file_nodes = cmds.ls(textures=True)
for node in file_nodes:
    texture_path = cmds.getAttr(node + ".fileTextureName")
    cmds.setAttr(node + ".fileTextureName", texture_path, type="string")

That’s it.