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: 

linked assembly Bug? can't traverse over assy

4 REPLIES 4
Reply
Message 1 of 5
k.trucksees
730 Views, 4 Replies

linked assembly Bug? can't traverse over assy

Hi all, 

I tried to modify the traverse-sample to output all occurrences with joints. The Script worked fine while testing with small assemblies, but as soon as I tested it on a larger assy with sub-assemblies, the occurrences in linked assys won´t get detected. Here is my modified script, it gets 'root.occurrences.asList' and an 'empty list'.

 

def traverseAssembly(occurrences, occList):
    ''' outputs a list of occurrences that have joints
    '''
    occ = adsk.fusion.Occurrence.cast(None)
    for occ in occurrences:     
        try:
            if occ.joints.count:    # if the occurrence has joints, append the occ's name to the list
                occList.append(occ.name)
        except:
            print('Error')

        if occ.childOccurrences:    # if there are childs, repeat the function
            occList = traverseAssembly(occ.childOccurrences, occList)
        if len(occList):
            print(occList[-1])
    return occList

 

If I manually break the link, the script works fine. So I think there is an issue with the link of the components?

Does anybody know if that's a bug or is my code wrong?      

@goyals, can you help?

Thanks. 

Labels (5)
4 REPLIES 4
Message 2 of 5
BrianEkins
in reply to: k.trucksees

I modified your sample to remove the joints check so it now reports every occurrence.  In my simple test it works correctly regardless of whether there are referenced components or not.

 

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        des = adsk.fusion.Design.cast(app.activeProduct)
        root : adsk.fusion.Component = des.rootComponent
        traverseAssembly(root.occurrences, 0)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def traverseAssembly(occurrences, level):
    occ = adsk.fusion.Occurrence.cast(None)
    for occ in occurrences:     
        print(' ' * (level * 5) + occ.name)
        if occ.childOccurrences:    # if there are childs, repeat the function
            occList = traverseAssembly(occ.childOccurrences, level + 1)
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 5
k.trucksees
in reply to: BrianEkins

Thank you @BrianEkins for your Answer.

I tested your Code and it outputs all occurrences as expected, but I need all occurrences with joints as a list or dict.

So I added a line to append all occurrences to a global list:

def traverseAssembly(occurrences, level):
    global occList
    occ = adsk.fusion.Occurrence.cast(None)
    for occ in occurrences:  
        if not occ in occList:
            try:
                occList.append(occ)   
            except:
                print(occ.fullPathName+' - Error')
        #print(' ' * (level * 5) + occ.name)
        if occ.childOccurrences:    # if there are childs, repeat the function
            traverseAssembly(occ.childOccurrences, level + 1)

When I run this script, I get a list with all occurrences. So I added a loop to remove all occs without joints.

for occ in occList:
            if occ.joints.count == 0:
                occList.remove(occ)
        print(occList)

I tested this code with the GenevaDrive-model from the "Basic Training->06 Assemblies->GenevaDrive".

An Error occurs when the loop comes to "frame:0801-04P:1" : "RuntimeError 3 : object does not belong to the occurrence's component"

 

Why does this error occur? I mean this occurrence belongs to the assembly, doesn't it?

Message 4 of 5
JeromeBriot
in reply to: k.trucksees

Hello,

 

You can't remove item from a list while iterating over it.

 

Try this instead:

occList = [occ for occ in occList if occ.joints.count > 0]

 

You can also modify the first code:

if not occ in occList and occ.joints.count > 0:
Message 5 of 5
k.trucksees
in reply to: JeromeBriot

Thanks,
this helps a bit, but does not solve the problem with the RuntimeError that occurs at larger assemblies.

Even if I connect two blocks with a revolute joint and move one component in a sub-component this error occurs. The browser looks like:
-root
    -box1
    -sub1
         -box2
Errormsg: "RuntimeError 3 : object does not belong to the occurrence's component"

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