Importing STL file as a mesh script error

Importing STL file as a mesh script error

benC5F9W
Explorer Explorer
495 Views
3 Replies
Message 1 of 4

Importing STL file as a mesh script error

benC5F9W
Explorer
Explorer

I have a script that automatically imports an STL file as a mesh into fusion using the meshBodies.add() function. When I run this script I am getting a weird error that I have never seen before: 

 

"RuntimeError: 2 : InternalValidationError : insertProcessor.execute(pInsertRequest.get())"

 

What is especially weird is that this script works perfectly fine if I use the selectFiles() function at the end of the script to prompt the user to select an STL file. This error only arises when I input the path to the file location of the STL file. This is the ideal way to use this as I don't want to have to select the file everytime. 

 

If anyone can help me figure out what this error means and why it is only showing up when I put the file location in my script it would be much appreciated. 

 

- Ben

 

Here is the code:

 

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


_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface

        # import stl file
        path = r'Downloads\powder.stl'

        #Line below can be used to prompt user to select file instead of using file path
        #paths = selectFiles('select STL file')

        # new doc
        _app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        des  :adsk.fusion.Design = _app.activeProduct
        des.designType = adsk.fusion.DesignTypes.ParametricDesignType
        root :adsk.fusion.Component = des.rootComponent

        # baseFeature
        baseFeatures = root.features.baseFeatures
        baseFeature = baseFeatures.add()

        baseFeature.startEdit()
        
        # import obj files
        meshs = importMesh(path ,root.meshBodies, baseFeature)


        baseFeature.finishEdit()

        # finish
        _ui.messageBox('Done')

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


def importMesh(
    path :list,
    meshBodies :adsk.fusion.MeshBodies,
    baseFeature :adsk.fusion.BaseFeature) -> list:

    unitCm = adsk.fusion.MeshUnits.CentimeterMeshUnit

    mesh = meshBodies.add(path, unitCm, baseFeature)
    
    return mesh


def selectFiles(
    msg :str):

    fileDlg = _ui.createFileDialog()
    fileDlg.isMultiSelectEnabled = True
    fileDlg.title = msg
    fileDlg.filter = '*.stl'
    
    dlgResult = fileDlg.showOpen()
    if dlgResult == adsk.core.DialogResults.DialogOK:
        return fileDlg.filenames
    

 

0 Likes
496 Views
3 Replies
Replies (3)
Message 2 of 4

kandennti
Mentor
Mentor

Hi @benC5F9W -San.

 

How about using the MeshBodies.add method?

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-ebb740dc-6214-4097-affb-a9bfc7fd7e43 

 

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

_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface

        # import stl file
        path = r'Downloads\powder.stl'

        #Line below can be used to prompt user to select file instead of using file path
        #paths = selectFiles('select STL file')

        # new doc
        _app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        des  :adsk.fusion.Design = _app.activeProduct
        des.designType = adsk.fusion.DesignTypes.ParametricDesignType
        root :adsk.fusion.Component = des.rootComponent

        # import stl
        meshs: adsk.fusion.MeshBodyList = root.meshBodies.add(
            path,
            adsk.fusion.MeshUnits.MillimeterMeshUnit,
        )

        # finish
        _ui.messageBox('Done')

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

 

 

 

0 Likes
Message 3 of 4

benC5F9W
Explorer
Explorer

That actually is the method I am using, it is just inside my importMesh()  function (lines 45-54). 

 

The MeshBodies.add() method is raising this error when trying to automatically import:

 

"RuntimeError: 2 : InternalValidationError : insertProcessor.execute(pInsertRequest.get())"

 

I am not sure what is causing this as all of my parameters for MeshBodies.add() seem correct.

0 Likes
Message 4 of 4

kandennti
Mentor
Mentor

@benC5F9W -San.

 

I was mistaken.
Perhaps I specified the file path incorrectly.

If you are on windows, it probably looks like this.

path = r'C:\Users\<username>\Downloads\powder.stl'
0 Likes