Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

Mesh Merge-Feature in API?

Anonymous
1,023 Views
3 Replies
Message 1 of 4

Mesh Merge-Feature in API?

Anonymous
Not applicable

Hi all,

I want to use the MeshMerge-Command in a Script, but can't find anything about it.Feature.PNG

Can someone tell me where I find it in the API and how it is used?

Thanks

0 Likes
Accepted solutions (2)
1,024 Views
3 Replies
Replies (3)
Message 2 of 4

kandennti
Mentor
Mentor
Accepted solution

Hi @Anonymous .

 

I don't think the API provides MeshWorkspace functionality.
The only way left is to use TextCommands.

 

Go into MeshWorkspace, prepare two or more mesh bodies and run the following script
(I opened the script in Edit beforehand and ran it from VSCode, because MeshWorkspace doesn't allow you to call the script.)

 _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface

        # Workspace check
        if _ui.activeWorkspace.id != 'MeshEnvironment':
            _ui.messageBox('Please, Switch to MeshWorkspace')
            return

        # MeshBody Count check
        des :adsk.fusion.Design = _app.activeProduct
        root :adsk.fusion.Component = des.rootComponent
        if root.meshBodies.count < 2:
            _ui.messageBox('A minimum of 2 MeshBody is required')
            return

        # select MeshBodies
        sels :adsk.core.Selections = _ui.activeSelections
        sels.clear()
        for mesh in root.meshBodies:
            sels.add(mesh)

        # exec textcommands
        txtCmds = [
            u'Commands.Start MeshCombineCommand',
            u'NuCommands.CommitCmd'
        ]
        for cmd in txtCmds:
            _app.executeTextCommand(cmd)


        _ui.messageBox('Done')

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

 

2.png

 

 

Learn more about TextCommands.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/use-textcommands/m-p/9645688 

 

Not related to Mesh, but @thomasa88  provided some useful information.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/access-section-analysis/m-p/9700491#M11052 

 

It is very difficult to develop with TextCommands because of the lack of information.
There is no guarantee that you will reach the goal, and even if you do, there is no guarantee that it will continue to work in the future.

 

If you have something that works well, we hope that you will share the information with us.

0 Likes
Message 3 of 4

Anonymous
Not applicable
Accepted solution

here is the merge-function I made.

The strange is, that it only works if the comp==root, because I found no way to put meshBodies in any other component else root.

An other issue is if the meshes have more than 50k faces fusion will stop merging the bodies.

 

def fmesh_merge(comp:adsk.fusion.Component):
    """executes a TextCommand to merge the Fusion-meshBodies in the given Component"""
    #todo: check if meshbody.faces.count > 50000, if so reduce faces!
    sel = ui.activeSelections
    sel.clear()
    for mesh in comp.meshBodies:
        sel.add(mesh)
    if len(sel)==0:
        for mesh in root.meshBodies:
            sel.add(mesh)
    txtCmds = [u'Commands.Start MeshCombineCommand', u'NuCommands.CommitCmd']
    
    for cmd in txtCmds:
        ui.palettes.itemById('TextCommands').writeText(
            'Merging '+str(haz_comp.meshBodies.count)
            +' Bodies: '+app.executeTextCommand(cmd)
        )
    

 

0 Likes
Message 4 of 4

kandennti
Mentor
Mentor

@Anonymous .

 

In order to select the Occurrence element, we need to get the proxy. (There is an explanation at the very end.)

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-88A4DB43-CFDD-4CFF-B124-7EE67915A07A 

 

For this reason, we have created this sample.

#Fusion360API Python script
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
        des :adsk.fusion.Design = _app.activeProduct
        root :adsk.fusion.Component = des.rootComponent

        # root
        execMeshbodiesMerge(root)

        # occ
        execMeshbodiesMerge(root.allOccurrences[0])

        des.activateRootComponent()

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

def execMeshbodiesMerge(
    root_occ :adsk.core.Base
    ):

    app :adsk.core.Application = adsk.core.Application.get()
    ui :adsk.core.UserInterface = app.userInterface
    des :adsk.fusion.Design = _app.activeProduct
    root :adsk.fusion.Component = des.rootComponent

    # selections
    sels :adsk.core.Selections = ui.activeSelections
    sels.clear()

    # get meshbodies
    meshBodyList = []
    if root_occ == root:
        # root
        root_occ.parentDesign.activateRootComponent()
        meshBodyList = [mb for mb in root_occ.meshBodies]
    else:
        # occ -proxy
        # https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-88A4DB43-CFDD-4CFF-B124-7EE67915A07A
        root_occ.activate()
        meshBodyList = [
            mb.createForAssemblyContext(root_occ) for mb in root_occ.component.meshBodies]

    # select meshbodies
    [sels.add(mb) for mb in meshBodyList]

    # exec textcommands
    txtCmds = [u'Commands.Start MeshCombineCommand', u'NuCommands.CommitCmd']
    [app.executeTextCommand(cmd) for cmd in txtCmds]

1.png

0 Likes