Get Folder Name Using Python

Get Folder Name Using Python

isocam
Collaborator Collaborator
1,083 Views
1 Reply
Message 1 of 2

Get Folder Name Using Python

isocam
Collaborator
Collaborator

Can anybody help?

 

I have the following Python code in a script file that I am working on:

 

def hasTargetFolder(targetName: str, doc: adsk.fusion.FusionDocument) -> bool:
df: adsk.core.DataFolder = doc.dataFile.parentFolder

FolderNames = []

while True:
if df.isRoot:
break
if not df.parentFolder:
break

FolderNames.append(df.name)

df = df.parentFolder

if targetName in FolderNames:
return True

return False

 

Can anybody update this code so that it returns the name of the folder?

 

Please see the attached picture.

 

I need it to return "Purchased Parts", "Reference Parts" or "Project Files"

 

Many thanks in advance!

 

Darren

0 Likes
Accepted solutions (1)
1,084 Views
1 Reply
Reply (1)
Message 2 of 2

Rushikesh.kadam
Autodesk
Autodesk
Accepted solution

@isocam below is a sample script to demonstrate it. I changed the function to include only one parameter. Also, the method includes a list 'targetFolder' consisting of all the folder names to be looked for. If a file is present under any of these folders it returns the folder name else False.

import  adsk.core, adsk.fusion, adsk.cam, traceback

def hasTargetFolder(doc: adsk.fusion.FusionDocument):
    df: adsk.core.DataFolder = doc.dataFile.parentFolder
    targetFolders = ["Purchased Parts", "Reference Parts",  "Project Files"]

    while True:
        if df.isRoot:
            break
        if not df.parentFolder:
            break
        df = df.parentFolder            

    if df.name in targetFolders:
        return df.name

    return False

def run(context):
    ui = None
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui  = app.userInterface
        design = adsk.fusion.Design.cast(app.activeProduct)
        document = design.parentDocument
        print(hasTargetFolder(document))
        

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

 

A suggestion to share code on the forum. Paste the code using the "Use the insert/Edif code sample" option and select the language type. This will make sure that all the indentations are maintained.

Rushikeshkadam_0-1652947854559.png

 

Hope it helps.

 

------------------------------------------------------------------------------------------------------------------------------

If my reply was helpful, please click the "Accept as Solution" button. It helps others get the answer quickly! A "Like" is always welcomed




Rushikesh Kadam
Senior QA Engineer
Quality Assurance
Autodesk, Inc.


0 Likes