Python Script - Open Specific File In Folder

Python Script - Open Specific File In Folder

isocam
Collaborator Collaborator
210 Views
1 Reply
Message 1 of 2

Python Script - Open Specific File In Folder

isocam
Collaborator
Collaborator

Can anybody help?

 

If I have a folder, In "PROJECT FILES" called "TEST FOLDER" and a document, inside the "TEST FOLDER" called "TEST PART".

 

How, using a Python script, do I open the document called "TEST PART"?

 

Many thanks in advance!

 

Darren

0 Likes
211 Views
1 Reply
Reply (1)
Message 2 of 2

kandennti
Mentor
Mentor

Hi @isocam -San.

 

We have created a simple sample script.


For a simple sample, we are not looking for deep folders.
Also, Fusion360's data panel allows files with the same name to be created in the same folder. Therefore, there is a possibility that the desired file cannot be opened.

# Fusion360API Python script

import traceback
import adsk.core as core

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface

        projectName = "PROJECT FILES"
        folderName = "TEST FOLDER"
        fileName = "TEST PART"        

        # Project
        proj: core.DataProject = None
        for x in app.data.dataProjects.asArray():
            if x.name == projectName:
                proj = x
                break

        if not proj:
            ui.messageBox('No Project')
            return

        # Folder
        folder: core.DataFolder = None
        for x in proj.rootFolder.dataFolders.asArray():
            if x.name == folderName:
                folder = x
                break

        if not folder:
            ui.messageBox('No Folder')
            return

        # File
        file: core.DataFile = None
        for x in folder.dataFiles.asArray():
            if x.name == fileName:
                file = x
                break

        if not file:
            ui.messageBox('No File')
            return

        # Open
        app.documents.open(file, True)
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 


I have uploaded here the add-in you asked about in your private message.
https://github.com/kantoku-code/Fusion360_OpenDocumentType-Sample 

0 Likes