Fixing Texture Paths with PyMEL

I have run into many situations where I need to change all of the texture paths in a Maya file. Depending on the size of your scene, this can be a pretty exhausting task if you try it manually. Fortunately, we can solve this problem quite easily with a few lines of code.

For example, say you have a Maya scene with texture paths pointing all over the place. All you need to do is collect the texture files into your project’s sourceimages folder and then run this script. All of your textures should now load! I will explain each line below.

import pymel.core as pm
import os
files = pm.general.ls(type='file')
for f in files:
    fullName = f.getAttr('fileTextureName')
    baseName = os.path.basename(fullName)
    f.setAttr('fileTextureName', 'sourceimages\\'+baseName, type='string')

Lines 1-2 simply import the pymel.core module and the os module, both of which contain functions that we need to call in the script.

Line 3 asks PyMEL for a list of all file nodes in the scene.

Line 4 loops through each file node

Line 5 gets the current file node’s texture path.

Line 6 strips the texture path off and stores just the texture’s file name

Line 7 sets the current file node’s texture path to point to that texture but now inside of the relative path to the sourceimages directory.

As you can see, this example points the texture paths to the current projects sourceimages directory. To make the paths point to a different directory, replace the ‘sourceimages\\’ string with some other path of your choosing.

Quick and easy!

Leave a Reply

Your email address will not be published. Required fields are marked *