Jump to content

Using python for convert file sequence path


Andrea

Recommended Posts

Hello! I am creating a python script that search for all the textures used in the project and copies them in a folder $HIP/textures. After that, it converts the path to $HIP/textures/[texture_name]. It works fine until there isn't a variable like $F in the string. If this is the case, it copies just the first frame and not the full sequence. 

What I am using to collect all the textures path is a line from a code I've found: 

files = glob.glob(hou.expandString(re.sub(r'\$F\d', "*", parm.unexpandedString())))

Being a python newbie, I am not really sure to correctly understand what it does in order to fix this issue. 

I got more or less that re.sub replace the first argument (\$F\d) with the second argument ("*") inside the text of the third argument. Is all the expandString and unexpandedString that confuses me. Seems like that this function just returns the string value at the current frame? What if I want to get all the textures/files? 

 

Thanks in advance for any help/suggestions

Link to comment
Share on other sites

Do you have examples where this does not work?

The regular expression replaces occurrences of $F4, etc with *. The * is a wildcard for globbing for matching files in the filesystem. expandString does all the Houdini magic to turn the expression into a complete path. 

If your path just contains file.$F.png, the regular expression above will not match and then expandString will substitute the frame number, i.e. you only get the first frame. Try to use r'\$F\d?' instead. 

Edited by Activate
Link to comment
Share on other sites

Hello! Thank you guys. 

So using the question mark  r'\$F\d?' as @Activate suggested, did the trick to collect the sequence of textures and copy them to the destination folder.

Using rawValue() returns the path I want. For one moment I think the script worked and it returned also the right string. In the happyness of the moment I started to change some other lines and.. is not working anymore as expected. It doesn't return the correct string with $F4 as before. 

Anyway right now I have added a line that searches for the last / and uses it for strip off the texture name. Now I would like to improve it and understand it better. I post it here as it is right now but please be aware that it copies all the referenced files and not just the textures.

import re, glob
import hou, os, shutil

### SET and CREATE PATH FOR TEXTURES ####
folder_name = "textures"
hip_path = hou.getenv("HIP")
backpath = hip_path + "/" + folder_name #change this name for change the folder name
if not os.path.exists(backpath):
    os.makedirs(backpath)
    print "created directory for textures"

### for DEBUG PURPOSES ###
# print backpath


### SINCE I SCRIPTED THIS AND I DONT TRUST MYSELF; BETTER DO A BACKUP HIPFILE ####
hou.hipFile.save()
hou.hipFile.saveAndIncrementFileName()


#### MAIN FUNCTION
def BackupAllContent(inputpath):
    # look for all the nodes
    subch = hou.node("/").allSubChildren()
    # start loop for all the nodes
    for child in subch:
        # takes all the parameters of the node
        parms_in_nodes = child.parms()
        # loop over all the parameters
        for parm in parms_in_nodes:
            # IF the parameter is a type string
            if parm.parmTemplate().type() == hou.parmTemplateType.String:
                syc_test = ""
                try:
                    ##nodes with something inside else are skipped
                    syc_test = glob.glob(hou.expandString(re.sub(r'\$F\d?', "*", parm.unexpandedString())))[0]
                except:
                    pass
                if os.path.exists(syc_test) and parm.eval() != "*":
                    ###print parm ## DEBUG parameters and nodes with link to external geo
                    files = glob.glob(hou.expandString(re.sub(r'\$F\d?', "*", parm.unexpandedString())))
                    for fls in files:
                        if os.path.exists(os.path.join(backpath, os.path.basename(fls))):
                            print os.path.basename(fls) + " is already there" ## DEBUG textures name
                        else:
                            shutil.copy(fls,backpath)
                            print os.path.basename(fls) + " has been copied"
                    raw_texturename = parm.rawValue()
                    texturename_variable = raw_texturename[raw_texturename.rfind("/")+1:] 
                    new_texture_path = os.path.join("$HIP/", folder_name, texturename_variable) ## merges this names/directories in one string based on operator system syntax
                    new_texture_path = new_texture_path.replace('\\','/') ##since I am on windows I have to convert \\ to /
                    parm.set(new_texture_path) #set new path
                    
                    print "#################################################"
                    print "new texture path would be " + new_texture_path
                    print "raw value is " + parm.rawValue()
                    print "unexpandedString is " + parm.unexpandedString()
                    print "re.sub is " + re.sub(r'\$F\d', "*", parm.unexpandedString())
                    print "expanded string is " + hou.expandString(re.sub(r'\$F\d?', "*", parm.unexpandedString()))
                    print "texturename_variable is " + texturename_variable
                    print "#################################################"


### RUN CODE ##
BackupAllContent(backpath)

 

Edited by Andrea
mistake
Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...