- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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? 🙂
Solved! Go to Solution.