Total project backup

Total project backup

Anonymous
Not applicable
1,613 Views
6 Replies
Message 1 of 7

Total project backup

Anonymous
Not applicable

In another thread someone from Fusion team said it would be possible after this recent update to export documents in *.f3d format using scripts.

 

Can someone help me get started with writing a script to take the entire contents of a project and export it for backup?

 

I'd like to be able to do nightly backups of my open source projects since I cannot stop people from deleting things.

 

Thanks

Luke

0 Likes
Accepted solutions (1)
1,614 Views
6 Replies
Replies (6)
Message 2 of 7

prainsberry
Autodesk
Autodesk

Hi Luke,

 

In the help under, Programming Interface->Samples->General->Miscellaneous: "Export to Other Formats" to see the syntax for exporting to an f3d

 

To access the data in your projects you can use Data, DataProjects, DataFolders and DataFiles to iterate through the files.

 

Need to write a little recursion script to iterate through all the files and folders in a project dumping them all into some directory.

 

Did you want a little more kickstart on the code or is this all you were looking for?  I could possibly put something together in the next day or so?



Patrick Rainsberry
Developer Advocate, Fusion 360
0 Likes
Message 3 of 7

ekinsb
Alumni
Alumni

There's still a bit missing from the API to fully support what you want to do.  The API does support exporting to various formats, including f3d but it's only possible to export a document that's already open in Fusion.  The ability to open from A360 is still coming.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 4 of 7

prainsberry
Autodesk
Autodesk

Ohh.. Sorry I mis-spoke. I was mistaken. 



Patrick Rainsberry
Developer Advocate, Fusion 360
0 Likes
Message 5 of 7

ekinsb
Alumni
Alumni
Accepted solution

Turns out I mispoke.  I lost track of which version this functionality was going into and it happens there was a problem with the new help where these functions were left out.  I've corrected it and will push an updated version of the help later this week.  In the meantime I went ahead and created a sample Python script that does what you want.  It needs some more work, especially around where it saves the files but it demonstrates the basic workflow.

 

import adsk.core, adsk.fusion, traceback

def main():
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        data = app.data
        for project in data.dataProjects:
            processFolder(project.rootFolder)
            
        ui.messageBox('Finished saving files.')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


# Recursive function to process the contents of the folder.
def processFolder(folder):
    ui = None
    try:        
        app = adsk.core.Application.get()
        docs = app.documents
        ui  = app.userInterface
        
        ui.messageBox('Processing folder: ' + folder.name)
    
        for file in folder.dataFiles:
            ui.messageBox('Processing file: ' + file.name)

            # Try opening the document but gracefully fail and 
            # assume the document isn't a Fusion document.            
            try:
                doc = docs.open(file, True)
            except:
                doc = None

            if doc:            
                # Find the Design product in the document.
                for prod in doc.products:
                    if prod.objectType == adsk.fusion.Design.classType():
                        des = prod
                        break
                    
                if des:
                    # Get the ExportManager from the active design.
                    exportMgr = des.exportManager
        
                    # Create a FusionArchiveExportOptions object and do the export.
                    fusionArchiveOptions = exportMgr.createFusionArchiveExportOptions('C:\\Temp\\' + doc.name + ".f3d")
                    res = exportMgr.execute(fusionArchiveOptions)
                                
                    doc.close(False)
                    
        for subFolder in folder.dataFolders:
            processFolder(subFolder)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
        
        
main()

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 6 of 7

Anonymous
Not applicable

Thank you Brian! I'll give it a try.

 

Any chance we can zip it into an archive as well, or will I need some extra jiggery pokery for that?

0 Likes
Message 7 of 7

ekinsb
Alumni
Alumni

I just checked and it looks like Python has native support to create and read zip files, (https://docs.python.org/2/library/zipfile.html), so it should be relatively easy to do.  As I said before the sample just demonstrates the basic workflow and is missing any kind of real logic for the output.  Right now it's just using the name of the file from A360 and writing it to the temp file.  You would probably want folders for different projects and A360 folders and possibly even doing a bit more in defining the filename because it's possible to have more than one file with the same name in the same folder.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes