strange StopIteration issue, caused by old version of SWIG?

strange StopIteration issue, caused by old version of SWIG?

JesusFreke
Advocate Advocate
1,239 Views
6 Replies
Message 1 of 7

strange StopIteration issue, caused by old version of SWIG?

JesusFreke
Advocate
Advocate

This is a bit of a strange one. I was trying to generate a python generator function that yields the visible bodies from an occurrence and its child occurrences. But the script was failing with a StopIteration error that I couldn't explain.

 

import adsk.core
import adsk.fusion
import traceback

app = adsk.core.Application.get()
root = app.activeProduct.rootComponent

def box(x, y, z, *, name="Box"):
brep = adsk.fusion.TemporaryBRepManager.get()
box_body = brep.createBox(adsk.core.OrientedBoundingBox3D.create(
adsk.core.Point3D.create(x/2, y/2, z/2),
adsk.core.Vector3D.create(1, 0, 0),
adsk.core.Vector3D.create(0, 1, 0),
x, y, z))
occurrence = root.occurrences.addNewComponent(adsk.core.Matrix3D.create())
occurrence.component.name = name
base_feature = occurrence.component.features.baseFeatures.add()
base_feature.startEdit()
occurrence.component.bRepBodies.add(box_body, base_feature)
base_feature.finishEdit()
return occurrence

def get_bodies(occurrence):
for i in range(0, occurrence.bRepBodies.count):
body = occurrence.bRepBodies.item(i)
yield body
for i in range(0, occurrence.childOccurrences.count):
yield from get_bodies(occurrence.childOccurrences.item(i))

def run(context):
try:
first = box(1, 1, 1, name="first")
second = box(1, 1, 1, name="second")
second.moveToComponent(first)
bodies = list(get_bodies(first))
app.userInterface.messageBox("bodies: %d" % len(bodies))
except:
print(traceback.format_exc())
app.userInterface.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

Which fails with the following exception

 

StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "generator_test.py", line 28, in get_bodies
yield from get_bodies(occurrence.childOccurrences.item(i))
SystemError: <built-in function delete_Occurrence> returned a result with an error set

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "generator_test.py", line 35, in run
bodies = list(get_bodies(first))
SystemError: <built-in function delete_BRepBody> returned a result with an error set

 

After doing a bit of googling, I suspect this is an issue with the version of swig used to generate the python api. See, e.g. https://github.com/swig/swig/pull/560. 

 

Based on the headers at webdeploy\production\<guid>\Api\Python\packages\adsk\core.py, it is using swig version 3.0.2, which predates the fix mentioned in the above bug.

 

Is there any chance you could update the swig version used to generate the python wrappers? 🙂

 

0 Likes
Accepted solutions (1)
1,240 Views
6 Replies
Replies (6)
Message 2 of 7

JesusFreke
Advocate
Advocate

Also, is this the best way to report api issues like this? If there's a different preferred way, I'll be happy to redirect my bug reports 🙂

0 Likes
Message 3 of 7

goyals
Autodesk
Autodesk

Fusion using 3.0.2_a version of SWIG at present. There is no immediate plan to upgrade the version but I have created a task UP-40906 to track it in our internal bug tracking system and will update you if there is any update.

 

Posting any issue to forum is a good way in my opinion because others can also share their observations but you can contact me directly also at shyamsunder.goyal@autodesk.com if you have a specific API issue.



Shyam Goyal
Sr. Software Dev. Manager
0 Likes
Message 4 of 7

JesusFreke
Advocate
Advocate

Sounds good, thanks! I can stick with lists for now, but it would be nice to be able to use generators 🙂

 

 

0 Likes
Message 5 of 7

JesusFreke
Advocate
Advocate

With the upgrade to Python 3.7, I was hoping SWIG might have been upgraded too. Unfortunately, it looks like it's still on 3.0.2, which is over 5 years old at this point, and I can still reproduce the StopIteration issue with the above script.

0 Likes
Message 6 of 7

goyals
Autodesk
Autodesk

Upgrading to Python was looking more important than SWIG considering that there are not many issues reported so far with SWIG but we will definitely consider it in near future.



Shyam Goyal
Sr. Software Dev. Manager
Message 7 of 7

JesusFreke
Advocate
Advocate
Accepted solution

I saw in another post that the version of SWIG used had been updated for the latest version of Fusion 360. I just tried this script again and confirmed that the StopIteration issue is no longer present. Thanks!

0 Likes