# fileTextureName auto suffix replace by expression

**URL:** https://forum.highend3d.com/t/filetexturename-auto-suffix-replace-by-expression/151155
**Category:** Maya
**Tags:** maya-boards, mel
**Created:** [January 13, 2017, 6:26am UTC](https://forum.highend3d.com/t/filetexturename-auto-suffix-replace-by-expression/151155 "2017-01-13T06:26:01Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![shurik\_spb](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/s/2acd7d/32.png) [@shurik\_spb](https://forum.highend3d.com/u/shurik_spb)
#### Post date: [January 13, 2017, 6:26am UTC](https://forum.highend3d.com/t/filetexturename-auto-suffix-replace-by-expression/151155/1 "2017-01-13T06:26:02Z")

</div>

Hi. Is there a way in maya to automatically switch file image name suffixes or prefixes depending on situation.  
Say, I have the few file nodes connected to a shader: for roughness, albedo, height, normal etc. The files themselves have their own naming convention: brick\_height, brick\_albedo, brick\_roughness etc.

It could be usefull to change only one texture at a time, when the other file nodes take common part of filepath and replace only one particular part of it.  
Leave “C:\textures\bricks\brick\_\*\*\*.exr” and change only symbols in \*\*\* part with a \_albedo,\_roughness etc.

I have simple expression

```
string $A=`getAttr -x file1.ftn`;
string $Disp = `substitute "Albedo" $A "Displacement"`;
setAttr -type "string" file.fileTextureName $Disp;

```

But the last line evaluating only once, I need some condiition when it happens everytime I change Albedo texture in particular filenode.

For the record. I’ve seen few recomendations to use File texture manager or built-in File path editor, but it’s not what I need, and that is why…

In FTM I need more operations to make changes even for one texture set.

1. launch FTM
2. press analyze
3. chose particular texture checkbox
4. set new target directory.
5. manually copy and paste Old String
6. manually copy and paste New String
7. press set path

But I need an automation to change the whole texture set by replacing the only one path in single filenode.

Thanks!

---

<div class="post-metadata">

### Author: ![myara](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@myara](https://forum.highend3d.com/u/myara)
#### Post date: [November 27, 2017, 8:51am UTC](https://forum.highend3d.com/t/filetexturename-auto-suffix-replace-by-expression/151155/2 "2017-11-27T08:51:43Z")

</div>

You are talking about scripts, not expressions.

To do this you would need a name convention to know what to replace.  
For example if all your textures ends with \_ and the type of texture like:  
brick\_albedo  
and you want to change it to  
wood\_albedo  
it would be very easy to write a script that searches for that last “\_” and replace everything before it.  
Something like this in Python:

```
import pymel.core as pm

#Set your new name
name = 'brick'

#Get Project Path
ws = pm.workspace(q=1, rd=1)

#Get All Files
allFiles = pm.ls(type="file")
#Iterate Files
for f in allFiles:
    fpath = f.fileTextureName.get().replace('\\','/')
    # Convert Full Path to Relative Paths
    if fpath.startswith(ws):
        fpath = fpath[len(ws):]
        
    # Replace the part of the file that is before the last "_"
    newfPath = fpath[:fpath.rfind('/')+1] + name + fpath[fpath.rfind('_'):]
    
    # Set new path to file
    f.fileTextureName.set( newfPath )
    
    # Change name of file replacing everything before the last "_"   
    newname = name + f[f.rfind('_'):]
    pm.rename(f, newname)

```
