linked assembly Bug? can't traverse over assy

linked assembly Bug? can't traverse over assy

Anonymous
Not applicable
1,190 Views
4 Replies
Message 1 of 5

linked assembly Bug? can't traverse over assy

Anonymous
Not applicable

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. 

0 Likes
1,191 Views
4 Replies
Replies (4)
Message 2 of 5

BrianEkins
Mentor
Mentor

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
0 Likes
Message 3 of 5

Anonymous
Not applicable

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?

0 Likes
Message 4 of 5

JeromeBriot
Mentor
Mentor

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:
0 Likes
Message 5 of 5

Anonymous
Not applicable

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"

0 Likes