Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Attribute Error(name) while adding bodies from a collection to a component.

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
Anonymous
217 Views, 2 Replies

Attribute Error(name) while adding bodies from a collection to a component.

This is a pretty simple script (or so I thought..), based on  a sample script to copy bodies.
So I create a collection of all bodies from all components, to then add them to a component I create from the API.

I get this error: 
2020-02-24 12_37_31-Autodesk Fusion 360 (Personal - Not for Commercial Use).png

Here is the script:

# The goal is to copy bodies from a full assembly into a new component, so that it will be possible
# to export as STL from Fusion with a common origin (and axis)

# Beginning on 23/2/2020 @18:59 by BG

# The original sample script seems pretty close to what is needed. I need to be able to select the component I want (must be empty),
# instead of the root. 
# the script creat a duplicate, have to remove it.

import adsk.core, adsk.fusion, traceback

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

        # Get active design
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
 
        # Get root component in this design
        rootComp = design.rootComponent
        # Create a collection to store our bodies        
        sourceBodies = adsk.core.ObjectCollection.create()

        occs = rootComp.occurrences 
        # add all bodies from all components in the collection
        for occurence in occs:
            for body in occurence.bRepBodies: 
                sourceBodies.add(body)
                adsk.doEvents()      
        

        newComp = occs.addNewComponent(adsk.core.Matrix3D.create())                
        newComp.component.name = rootComp.name + " for STL Export"
        adsk.doEvents() 

        newComp.features.copyPasteBodies.add(sourceBodies)

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

Thanks for you help, and best regards.
Bernard

2 REPLIES 2
Message 2 of 3
kandennti
in reply to: Anonymous

Hi @Anonymous .

 

The return value of addNewComponent was Occurrence.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-8c7da023-bfd1-4816-9a57-a90dfcf3acd4 


Therefore, it worked correctly in this way.

・・・
        for occurence in occs:
            for body in occurence.bRepBodies: 
                sourceBodies.add(body)
                # adsk.doEvents()

        newOcc = occs.addNewComponent(adsk.core.Matrix3D.create())                
        newOcc.component.name = rootComp.name + " for STL Export"
        # adsk.doEvents()

        newOcc.component.features.copyPasteBodies.add(sourceBodies)
・・・

 

It's a confusing method name ...

Message 3 of 3
Anonymous
in reply to: kandennti

ha! 🙂 I knew it had to be easy, but would never have guessed on my own. Thanks!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report