Copying .fbx Files With Python


When dealing with a lot of .fbx files we some times have to move them or back them up. Maybe a mocap shot was delivered and you want to move them off a network and on to your local drive to process or maybe it’s the end of a production and you want to back up all files from one drive to another – either way I thought I would share this quick script I wrote.

Some of the things that need to be defined within the script below are the strings for sourceroot and destroot, I will draw attention to them using the note ” – UPDATE ME TO SUITE YOUR NEEDS”:

You can run the script by typing in the MotionBuilder Python console CopyFBXFiles(folder)
ie. CopyFBXFiles("NavigaitonMocap"). NavigaitonMocap = The Root Folder We Want To Process 
def CopyFBXFiles(folder):
    import sys,os
    from shutil import copy
    ##List For File Counter
    i = [0, 0]
    ##The Drive And Path Where Your Project Is - UPDATE ME TO SUITE YOUR NEEDS
    sourceroot = r"D:\MyProject"
    ##The Full Path Of The Mocap Shoot
    sourcefolder = (sourceroot + "\\" + folder)
    ##The Desitination Drive And Root Folder We Will Copy To - UPDATE ME TO SUITE YOUR NEEDS
    destroot = r"D:\MyBackUpFiles"
    ##The Full Path Of Where Will Will Copy To Including The Sub Dir = shootnumber
    destfolder = (destroot + "\\" + folder)
    ##Setup Target Folder
    if not os.path.exists(destfolder):
        ##Make Target Folder If None Is Found
    ##Go Find All .fbx files
    path = os.path.join(sourcefolder, "targetdirectory")
    for path, subdirs, files in os.walk(sourcefolder):
        ##Filter Out Folders We Wish To Skip (MotionBuilder Auto Back Ups And Media Folders) - YOU CAN REMOVE IF YOU WISH TO GET ALL .fbx Files
        lFBM = ".fbm"
        lBck = ".bck"
        if lFBM in path or lBck in path:
        ##Deal With The Folders That Were Not Filtered Out
            for name in files:
                ##Split The Extention From The File
                extension = os.path.splitext(name)[1]
                ##Define The Extention We Are Looking for
                if extension == ".fbx":
                    ##Source Files Full Path And Name
                    src = (os.path.join(path, name))
                    destfile = (os.path.join(destfolder, name))
                    ##Check Destination Folder To See If File Already Exists
                    if os.path.isfile(destfile):
                        print destfile, " - Already Exists"
                    ##File Does Not Exist Within The Destination Folder
                        ##Copy Source File To Our Destination Folder
##                        copy(src, destfolder)
                        print "copying: ",src
                        ##Count File
                        for i in enumerate(files):
    ##Print Total Number Of Counted .fbx Files Copied                
    print "Number of FBX Files Copied:", i[0]

That’s about it. This was a quick script that was fun to make and it can be formatted to search for any file type, by replacing the ‘ if extension == “.fbx” ‘ with ‘ if extension == “.psd” ‘ the script will search the desired folder for any file that has the extension “.psd” .

I hope this helps.

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.