Hi @corbin2 ,
I believe the source of the problem is to reference from the sketch an object that is in the future in the timeline.
I tried to redefine the plane in case there is an exception accessing it, but once it was assigned Fusion generate a warning (highlighting the sketch in yellow) asking to re-define the sketch plane. So, the problem remained the same.
The new solution is to define a construction plane which is at the same level of the top face of the body.
Here is the snipped code:
import adsk
import adsk.core
import adsk.fusion
import traceback
app = adsk.core.Application.get()
def run(context) -> None:
doc = app.activeDocument
des: adsk.fusion.Design = adsk.fusion.Design.cast(doc.products.itemByProductType('DesignProductType'))
root = des.rootComponent
try:
sketch: adsk.fusion.Sketch = root.sketches[1]
try:
app.log(f'{sketch.referencePlane=}')
except RuntimeError:
target_body = root.occurrences[0].component.bRepBodies[0].createForAssemblyContext(root.occurrences[0])
parent_comp = sketch.parentComponent
cons_plane_inp = parent_comp.constructionPlanes.createInput()
cons_plane_inp.setByOffset(
parent_comp.xYConstructionPlane,
adsk.core.ValueInput.createByReal(target_body.boundingBox.maxPoint.z))
cons_plane = parent_comp.constructionPlanes.add(cons_plane_inp)
sketch.referencePlane = cons_plane
app.log('referencePlane redefined!')
except Exception:
app.log('Failed:\n{}'.format(traceback.format_exc()))
adsk.terminate()
The first run, it catches the exception, and then redefine the sketch plane.
An in the following runs, it just can access the reference plane and print it to the Text Command window.
In my opinion, both body and sketch plane should be parametric. That way there is not restriction on which one is created before and both will have the same definition.
I hope this can help.
Regards,
Jorge Jaramillo