Mimic os.walk

The topic of being able to archive soft-imported clips used in a timeline, batch, whatever in the original format and not duplicated has come up many times. Basically a way to “collect files”. I thought it’d be interesting to start working on a script to do this, or at least attempt to.

That said, I’ve run into a stumbling block: How to drill down into an unknown level of sub-folders until you find either a sequence, clip or batchgroup? If this was on a normal filesystem you could easily use os.walk and let it do it’s thing but, as far as I can tell, there isn’t a way to do that within the Flame.

I feel like there’s some clever way to write some sort of loop that can continue to call itself and keep running again when a new folder is found within the current folder.

Some simple code I’ve been testing with:

def dump_entries(selection):
    import flame

    for libraries in flame.project.current_project.current_workspace.libraries:
        print ("- ", libraries.name)

        for folder in libraries.folders:
            print (" +-- ", folder.name)

            if has_subfolder(folder):
                for i in range(len(folder.folders)):
                    print ("   +-- ", folder.folders[i].name)

                    if has_subfolder(folder.folders[i]):
                        for x in range(len(folder.folders[i].folders)):
                            print ("      +--  ", folder.folders[i].folders[x].name)

def has_subfolder(selection):
    import flame

    if len(selection.folders) == 0:
        return False
    else:
        return True

Does anyone have any ideas or suggestions of what else to look at? My python skills are pretty “throw a dart at the board” style so when it comes to more complex structures and whatnot I’m pretty lost.

Any help would be greatly appreciated, thanks.

1 Like

Recursion is your friend! :slight_smile:
By calling the function from itself again you can traverse through an unknown level of subfolders.

Here is a little example to build a list of all clips. I’ve modified some names slightly to stick to some Autodesk terminology (e.g. clips instead of entries etc.) to make things more understandable. Hope this helps. :slight_smile:

import flame

clips = []

def build_clip_list(current_folder):
    """recursive search for entries"""
    global clips
    for folder in current_folder:
        if has_subfolder(folder):
            build_clip_list(folder.folders)

        for clip in folder.clips:
            clips.append(clip)
    

def collect_clips():
    """function that returns all clips as a list"""
    global clips
    clips = []

    for library in flame.project.current_project.current_workspace.libraries:
        build_clip_list(library.folders)

    return clips


def has_subfolder(folder):
    """check if the given folder has any subfolders"""
    if len(folder.folders) == 0:
        return False
    else:
        return True


print collect_clips()

This could be improved by running set on the list at the end or implementing another mechanism to avoid duplicate entries in the list.

3 Likes

Oh man, so simple! Love it. I didn’t realize you could call a function within itself…

Thank you so much @claussteinmassl !

You’re welcome! :slight_smile:

Since we’re here…do you know if it’s possible to get the sequence range from a file path? Currently I can only seem to get collected_clips[i].versions[0].tracks[0].segments[0].file_path to return the file path of the first file.

I.e. that will currently return somefile_0000.exr and not somefile_[0000-0120].exr which is what .mio files hold as well as what’s shown in the media panel under clip information.

I don’t know of a direct implementation. Not in Flame and not in Python in general. However there are projects like this, which can be a great starting point if you don’t want to reinvent the wheel:

Knowing the file path to the first frame, you could parse the directory to get the entire sequence.

Oh nice one, thank you.

Cool, I didn’t know this one, thanks @claussteinmassl
@ kyleobley you can also have a look at the clique module, specifically for image sequences :

1 Like

Just used this for finding all sequences. Thanks a lot @claussteinmassl !!!

1 Like