Running a Python Script On Every MotionBuilder File Within A Directory

It’s been awhile since I have posted, We are approaching launch of my next game tittle and now that I have a free moment I thought I would demonstrate how to run a python script on multiple “.fbx” files. All the “.fbx” files for this demo will exist within the same directory.


##Set Your Root Dir Here
root = r"D:\lookaround"

'''
Below is a simple example on how to search a folder (the "root" variable listed above) for ".fbx" files
'''
##Creat A Function That Will Search The Root Dir for .fbx Files
def ListFolderContent():
import sys,os
path = os.path.join(root, "targetdirectory")
for path, subdirs, files in os.walk(root):
for name in files:
##Set A Variable To Isolate Each Files Extension
extension = os.path.splitext(name)[1]
##Define What To Do With Files That Have The Extention ".fbx"
if extension == ".fbx":
lfbxfile = os.path.join(path, name)
'''
This is where you state what you want to do to the .fbx file found.
For this example I will just print out the file's full path
'''
##Print The Path And File Name
print lfbxfile
##Define What To Do With Files Found That Have Another Extension
else:
pass
##Print Out The Total Number Of ".fbx" Files Found
print "Number of FBX Files Found:", len( list ( enumerate(files) ) )

##Run Function
ListFolderContent()

The above script prints out the following for me:

>>> 
D:\lookaround\lookaround001.fbx
D:\lookaround\lookaround002.fbx
D:\lookaround\lookaround003.fbx
D:\lookaround\lookaround004.fbx
Number of FBX Files Found: 4
>>> 

By changing the variable root (found on line #2) to the drive and directory you want to search you should be able to get a list of your own.

The above example print the file name if the existence of “.fbx” is found:

##Print The Path And File Name
print lfbxfile

That can be changed to anything you wish to do to the “.fbx” file, for instance if you wanted to open the fbx file and print out the name of every take (!!!I would not recommend doing this if you have a lot of “.fbx” files or if the file size are really big!!!):

The full script would look like this:

##Set Your Root Dir   
root = r"D:\lookaround"

##A Simple Function That Will Open A File Within MotionBuilder And Print Out the Take Names
def OpenFileAndListTakes(files):
    from pyfbsdk import FBApplication, FBSystem
    ##Open .fbx file
    FBApplication().FileOpen(files)
    ##Pring File Name
    print files
    ##Print Take Names
    for i in  FBSystem().Scene.Takes:
        print i.Name
    
'''
Below is a simple example on how to search a folder (the "root" variable listed above) for ".fbx" files
'''
##Creat A Function That Will Search The Root Dir for .fbx Files    
def ListFolderContent():
    import sys,os
    path = os.path.join(root, "targetdirectory")
    for path, subdirs, files in os.walk(root):
        ##Added Variables For Folders That We Want to Omit
        '''
        MotionBuilder Can Automatically Create (Backups And Media) folders, by skipping them it will allow us to not process unwanted data
        '''
        lFBM = ".fbm"
        lBck = ".bck"
        ##If The Folder Or Sub Folder Contains ".fbm" or ".bck" Within It
        if lFBM in path or lBck in path:
            pass
        ##If The Folder Or Sub Folder Does Not Contain ".fbm" or ".bck" Within It
        else:
            for name in files:
                ##Set A Variable To Isolate Each Files Extention
                extension = os.path.splitext(name)[1]
                ##Define What To Do With Files That Have The Extention ".fbx"
                if extension == ".fbx":
                    lfbxfile = os.path.join(path, name)
                    '''
                    This is where you state what you want to do to the .fbx file found.
                    '''
                    OpenFileAndListTakes(lfbxfile)
                ##Define What To Do With Files Found That Have Another Extention    
                else:
                    pass 

##Run Function
ListFolderContent()

The above script printed the following out for me:

>>> 
D:\lookaround\lookaround001.fbx
Take 001
Take 002
Take 003
Take 004
>>> 
D:\lookaround\lookaround002.fbx
Take 001
Take 002
Take 003
Take 004
>>> 
D:\lookaround\lookaround003.fbx
Take 001
Take 002
Take 003
Take 004
>>> 
D:\lookaround\lookaround004.fbx
Take 001
Take 002
Take 003
Take 004
>>> 

This was a lot of fun to figure out, with this type of things your could get a count for every .fbx file you have or you could copy every .fbx file from one location to another, you could pretty much do any process you can think of within MotionBuilder and Python.

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.