Texture path

35 views
Skip to first unread message

DGFA DAG's GRAPHIC & FOTO ART

unread,
Jul 25, 2021, 12:19:28 PM7/25/21
to Python Programming for Autodesk Maya
Hi,
when I want to change the texture path I struggle on one point.
Let say the path is now 
project/create/Maya/sourceimages/room/texture.exr 
There could be a deeper sub folder structure like
project/create/Maya/sourceimages/light/exr/light.exr etc.

How can replace the path just to sourceimages and preserve all after that?

Justin made a awesome YouTube blog, but I have no clue how to solve procedurally this path problem.

Thank you!

Justin Israel

unread,
Jul 26, 2021, 3:55:58 PM7/26/21
to python_in...@googlegroups.com
On Mon, Jul 26, 2021 at 4:19 AM DGFA DAG's GRAPHIC & FOTO ART <gerom...@gmail.com> wrote:
Hi,
when I want to change the texture path I struggle on one point.
Let say the path is now 
project/create/Maya/sourceimages/room/texture.exr 
There could be a deeper sub folder structure like
project/create/Maya/sourceimages/light/exr/light.exr etc.

How can replace the path just to sourceimages and preserve all after that?

You may need to provide some more details about your goal, but this sounds like it might just be a python string formatting problem. 
Assuming you already know how to get and set the texture filepath, and have the texture path value as a variable:
path = 'project/create/Maya/sourceimages/room/texture.exr'
you should be able to use one of the many possible approaches to extracting the substring.

First you have to start with some known prefix you want to preserve. 
prefix = 'project/create/Maya/sourceimages/'
new_prefix = 'path/to/new/sourceimages/'
You could use some of the built in string methods to replace the prefix:
path.replace(prefix, new_prefix, 1)
# 'path/to/new/sourceimages/light/exr/light.exr'

new_prefix + path.partition(prefix)[-1]
# 'path/to/new/sourceimages/light/exr/light.exr'
If you want more control over the substitution, such as being able to only replace it if it actually starts with your prefix, 
you could use a regular expression:
import re
re.sub(r'^' + prefix, new_prefix, path)
# 'path/to/new/sourceimages/light/exr/light.exr' 

re.sub(r'^' + prefix, new_prefix, 'dont_replace/'+path)
# 'dont_replace/project/create/Maya/sourceimages/light/exr/light.exr'

Justin


Justin made a awesome YouTube blog, but I have no clue how to solve procedurally this path problem.

Thank you!

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/a72d98b3-63cf-465d-abaa-4b99e1951f97n%40googlegroups.com.

DGFA DAG's GRAPHIC & FOTO ART

unread,
Jul 28, 2021, 1:14:49 PM7/28/21
to Python Programming for Autodesk Maya

Hi,
thank you for support.

I solved it in this way:

def relinkBtnCmd(self, *args):

        #get the path specified in the text field using a query
        basePath = cmds.textField(self.txtFieldFolderLocation, query=True, text=True);
        index = cmds.textField(self.txtFieldOldFolderLocation, query=True, text=True);

        # list all file nodes
        fileNodes = cmds.ls(type="file")

        # repeat for each node
        for node in fileNodes:
            #get the current path
            currPath = cmds.getAttr(node + '.fileTextureName')

            #split the path name at the last folder
            pathSplit = currPath.split(os.sep) #get positon of last '/' '\'
            filename = pathSplit[-1] # split filename
            #rel = os.path.relpath(currPath, basePath)
            rel = os.path.relpath(currPath, index)

            #combine basePath and filename to create a new path
            #newPath = "%s/%s" %(basePath, filename)
            newPath = os.path.join(basePath, rel)

            # set the path in the texture node
            cmds.setAttr(node + '.fileTextureName', newPath, type='string')
Reply all
Reply to author
Forward
0 new messages