Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Import and Modify STL with Python Scripting

arjun_vermaWKZX3
Observer

Import and Modify STL with Python Scripting

arjun_vermaWKZX3
Observer
Observer

Hi all, 

 

I am new to Fusion360 and am looking for some guidance. I have a collection of over 200 STL files which contain the geometry of various irregularly shaped blood vessels (1 inlet, 1 outlet). I need to process them by trimming 5mm off of the inlet and outlet, and then adding a 50mm long cylinder to each end. What are the best ways to get started on this task using Python Scripting?

 

Any help is much appreciated! Thanks.

0 Likes
Reply
274 Views
1 Reply
Reply (1)

rohit_maneYV8C9
Autodesk
Autodesk

Hello @arjun_vermaWKZX3 ,

 

you can find more details about Fusion API here Fusion API (Application Programming Interface) 


You can use the attached script to import multiple STL files into Fusion. However, since these files have irregular shapes, you may need to convert them back into solid bodies for easier editing, as working with mesh files can be quite challenging.

 

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

def run(context):
    try:
        global ui
        app = adsk.core.Application.get()
        ui  = app.userInterface

        if (not ui.activeWorkspace.id == "FusionSolidEnvironment"):
            modelWS = ui.workspaces.itemById("FusionSolidEnvironment")
            modelWS.activate() 

        opFolder = GetFolderDialog('Fusion - Select folder location.',os.path.expanduser('~/Documents'))

        doc = app.activeDocument
        products = doc.products
        design = adsk.fusion.Design.cast(products.itemByProductType("DesignProductType"))
        rootComp = design.rootComponent
   
        design.designType = adsk.fusion.DesignTypes.ParametricDesignType
        for root, dirs, files in os.walk(opFolder):
            for file in files:
                fileName, ext = os.path.splitext(file)
                if ext.lower() in '.stl':
                    tempStl = os.path.join(root, file)
                    baseFeatures = rootComp.features.baseFeatures.add()
                    baseFeatures.startEdit()
                    rootComp.meshBodies.add(tempStl,adsk.fusion.MeshUnits.MillimeterMeshUnit,baseFeatures)
                    baseFeatures.finishEdit()

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


def GetFolderDialog(title = "" , initialDirectory= ""):
    # Set styles of file dialog.
    FolderDlg = ui.createFolderDialog()
    if title != "" : FolderDlg.title = title 
    if initialDirectory != "" : FolderDlg.initialDirectory = initialDirectory

    # Show File dialog
    dlgResult = FolderDlg.showDialog()
    if dlgResult == adsk.core.DialogResults.DialogOK:
        FolderPath = FolderDlg.folder
        if not os.path.isdir(os.path.dirname(FolderPath)):
            ui.messageBox("Selectes folder is not exist")
            return None
    else:
        return None

    return FolderPath

 

 

 

 

Hope it helps.

 

0 Likes