Last active
August 30, 2019 22:17
-
-
Save Onefabis/c3c8f5e04262f6340eba17f80616f2a6 to your computer and use it in GitHub Desktop.
Fast replace textures paths for maya in case textures folders was changed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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