fileTextureName auto suffix replace by expression

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!

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)