Skip to content

Instantly share code, notes, and snippets.

@Onefabis
Last active August 30, 2019 22:17
Show Gist options
  • Save Onefabis/c3c8f5e04262f6340eba17f80616f2a6 to your computer and use it in GitHub Desktop.
Save Onefabis/c3c8f5e04262f6340eba17f80616f2a6 to your computer and use it in GitHub Desktop.
Fast replace textures paths for maya in case textures folders was changed
import os.path
import maya.cmds as mc
'''
you can call script from this command at the end of the code
fastTextureReplacePath()
it will search based on the path of your scene, i.e., it will seek inside subfolders in the folder where your scene is
but if you run the script such this
fastTextureReplacePath( 'C:/Project1/Character1/textures/' )
it will start to seek the textures from this specified folder as a root, i.e. it will seek for your textures inside of subfolders of this root folder
'''
def fastTextureReplacePath( pathToSearch=None):
files = mc.ls( typ='file' )
for f in files:
texturePath = mc.getAttr( f + '.fileTextureName' )
if texturePath:
if not os.path.isfile(texturePath):
dir = mc.file( q=1, exn=1 ).rsplit( '/', 1 )[0]
if pathToSearch:
dir = pathToSearch
fileName = texturePath.rsplit( '/', 1 )[-1]
allPaths = [ x[0].replace('\\', '/') + '/' + fileName for x in os.walk(dir) if fileName in x[-1] ]
if allPaths:
finalPath = allPaths[0]
if len(allPaths)>1:
matchedPaths = [ len( set( k.split('/') ) & set( texturePath.split('/') ) ) for k in allPaths ]
finalPath = allPaths[ matchedPaths.index( max( matchedPaths ) ) ]
mc.setAttr( f + '.fileTextureName', finalPath, type='string' )
fastTextureReplacePath()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment