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: 

python script with a mistake that causes Fusion 360 to crash

1 REPLY 1
Reply
Message 1 of 2
mojobility
706 Views, 1 Reply

python script with a mistake that causes Fusion 360 to crash

Search for the two lines in the following script commented with the word CRASH.  I expect that better error checking of the API parameters could allow Fusion 360 to report this as an error instead of crashing.  Although Fusion 360 offered a dialog box to report the crash it would only allow a few lines of text for the description and wouldn't accept me pasting in this script and I am too lazy to try and boil this down to the minimal lines of code needed to reproduce this issue.

 

 

import adsk.core, adsk.fusion, traceback

def asInches(val):
    return val*2.54

def toInches(val):
    return val/2.54

# global vars
app = adsk.core.Application.get()
ui  = app.userInterface
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
rootComponent = design.rootComponent
rootFeatures = rootComponent.features
identityMatrix = adsk.core.Matrix3D.create()

def dump(obj):
   for attr in dir(obj):
       if hasattr( obj, attr 😞
           print( "obj.%s = %s" % (attr, getattr(obj, attr)))

def printILine3d(line):
    print ("origin:{:f},{:f},{:f} dir:{:f},{:f},{:f}".format(line.origin.x,line.origin.y,line.origin.z,line.direction.x,line.direction.y,line.direction.z))
    return

def Vector(x,y,z):
    return adsk.core.Vector3D.create(x,y,z)

def Point(x,y,z):
    return adsk.core.Point3D.create(x,y,z)

def assign3(left3, right3):
    left3.x = right3.x
    left3.y = right3.y
    left3.z = right3.z
    return
    
def notZeroVec3D(vector):
    if vector.x!=0.0:
        return True
    if vector.y!=0.0:
        return True
    if vector.z!=0.0:
        return True
    return False

def sqrt(value):
    return value**0.5

sketchLine_component=None
sketchLine_sketch=None
sketchLine_name=None
sketchLine_lines=None
sketchLine_arcs=None
sketchLine_start_point = Point(0.0, 0.0, 0.0)
sketchLine_point0 = Point(0.0, 0.0, 0.0)
sketchLine_point1 = Point(0.0, 0.0, 0.0)

def sketchLineSetName(sketch_name):
    global sketchLine_name
    sketchLine_name = sketch_name
    return

def sketchLineFillet(line0, line1, radius):
    sketchLine_arcs.addFillet(line0, line0.endSketchPoint.geometry, line1, line1.startSketchPoint.geometry, radius)
    return
    
def sketchLineStart(sketch_name, sketch_component, sketch_plane, sketch_start_point):
    global sketchLine_component
    global sketchLine_sketch
    global sketchLine_name
    global sketchLine_lines
    global sketchLine_arcs
    global sketchLine_start_point
    global sketchLine_point0
    global sketchLine_point1
    sketchLine_component = sketch_component
    sketchLine_sketch = sketch_component.sketches.add(sketch_plane)
    sketchLine_name = sketch_name
    sketchLine_sketch.name = "sketch_"+sketch_name
    sketchLine_lines = sketchLine_sketch.sketchCurves.sketchLines
    sketchLine_arcs = sketchLine_sketch.sketchCurves.sketchArcs
    assign3(sketchLine_start_point, sketch_start_point)
    assign3(sketchLine_point1, sketch_start_point)
    return    
        
def sketchLineNext(sketch_point):
    global sketchLine_lines
    global sketchLine_point0
    global sketchLine_point1
    assign3(sketchLine_point0, sketchLine_point1)
    assign3(sketchLine_point1, sketch_point)
    return sketchLine_lines.addByTwoPoints(sketchLine_point0, sketchLine_point1)

def sketchLineClose():
    global sketchLine_lines
    global sketchLine_start_point
    global sketchLine_point0
    global sketchLine_point1
    assign3(sketchLine_point0, sketchLine_point1)
    assign3(sketchLine_point1, sketchLine_start_point)
    return sketchLine_lines.addByTwoPoints(sketchLine_point0, sketchLine_point1)
    
def sketchLineExtrude(extrude_distance, extrude_material=None):
    global sketchLine_component
    global sketchLine_sketch
    global sketchLine_name
    profile = sketchLine_sketch.profiles[0]
    extrudes = sketchLine_component.features.extrudeFeatures
    extInput = extrudes.createInput(profile, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
    extInput.setDistanceExtent(False, adsk.core.ValueInput.createByReal(extrude_distance))
    extrude = extrudes.add(extInput)
    extrude.name = "extrude_"+sketchLine_name
    newbody = extrude.bodies.item(0)
    newbody.name = "body_"+sketchLine_name
    if extrude_material!=None:
        newbody.appearance = extrude_material
    return newbody

def sketchLineExtrudeCut(extrude_distance):
    global sketchLine_component
    global sketchLine_sketch
    global sketchLine_name
    profile = sketchLine_sketch.profiles[0]
    extrudes = sketchLine_component.features.extrudeFeatures
    extInput = extrudes.createInput(profile, adsk.fusion.FeatureOperations.CutFeatureOperation)
    extInput.setDistanceExtent(False, adsk.core.ValueInput.createByReal(extrude_distance))
    extrude = extrudes.add(extInput)
    extrude.name = "extrude_cut_"+sketchLine_name
    return
    
def degreesToRadians(degrees):
    radians = degrees * 3.141592653589793 / 180.0
    return radians

def inputDegrees(degrees):
    return adsk.core.ValueInput.createByReal(degreesToRadians(degrees))

def inputValStr(valStr):
    return adsk.core.ValueInput.createByString(valStr)

 
def run(context):
#    ui = None
    try:
         
#        i = app.favoriteAppearances.count
#        while i>0:
#            i -= 1
#            print("{} = {}".format(app.favoriteAppearances.item(i).name,app.favoriteAppearances.item(i).id))
        material_oak = app.favoriteAppearances.itemByName("Oak - Semigloss")
        
        design.fusionUnitsManager.distanceDisplayUnits = adsk.fusion.DistanceUnits.InchDistanceUnits

        seatOccurrence = rootComponent.occurrences.addNewComponent(identityMatrix)
        seatComponent = seatOccurrence.component
        seatComponent.name = "component_seat"

        # design parameters
        seat_thickness = asInches(0.75)
        pocket_thickness = asInches(0.45)
        seat_back_width = asInches(12.0)
        seat_front_width = asInches(14.0)
        front_radius = asInches(1.0)
        back_radius = asInches(0.5)
        seat_depth = asInches(13.0)
        handle_width = asInches(4.5)
        handle_depth = asInches(1.25)
        handle_front_offset = asInches(2.5)
        rib_width = asInches(0.5)
        rib_radius = asInches(0.25)
        perimeter_width = asInches(0.5)
        num_ribs = 3
        
        # seat profile
        my_point = Point(-seat_back_width*0.5, seat_depth, 0.0)                            # start at left back
        sketchLineStart("seat", seatComponent, seatComponent.xYConstructionPlane, my_point)
        my_point.x = -seat_front_width*0.5; my_point.y = 0; l0 = sketchLineNext(my_point)
        my_point.x *= -1.0; l1 = sketchLineNext(my_point)
        my_point.x = seat_back_width*0.5; my_point.y = seat_depth; l2 = sketchLineNext(my_point)
        l3 = sketchLineClose()
        sketchLineFillet(l0, l1, front_radius)
        sketchLineFillet(l1, l2, front_radius)
        sketchLineFillet(l2, l3, back_radius)
        sketchLineFillet(l3, l0, back_radius)
        body_seat = sketchLineExtrude(-seat_thickness, material_oak)
        
        pocket_front_width = (seat_front_width - num_ribs*rib_width - 2.0*perimeter_width) / (num_ribs+1)
        pocket_back_width = (seat_back_width - num_ribs*rib_width - 2.0*perimeter_width) / (num_ribs+1)
        pocket_back_x = -seat_back_width*0.5 + perimeter_width
        pocket_back_y = seat_depth - perimeter_width
        pocket_front_x = -seat_front_width*0.5 + perimeter_width
        pocket_front_y = perimeter_width

        # pocket 1
        my_point = Point(pocket_back_x, pocket_back_y, 0.0)                            # start at left back
        sketchLineStart("seat_pocket1", seatComponent, seatComponent.xYConstructionPlane, my_point)
        my_point.x = pocket_front_x; my_point.y = pocket_front_y; sketchLineNext(my_point) # will CRASH below without l0 = sketchLineNext(my_point)
        my_point.x += pocket_front_width; l1 = sketchLineNext(my_point)
        my_point.x = pocket_back_x+pocket_back_width; my_point.y = pocket_back_y; l2 = sketchLineNext(my_point)
        l3 = sketchLineClose()
        sketchLineFillet(l0, l1, front_radius) # CRASH occurs inside this function when attempting to add a fillet with the stale line reference l0 to the previous sketch
        sketchLineFillet(l1, l2, rib_radius)
        sketchLineFillet(l2, l3, rib_radius)
        sketchLineFillet(l3, l0, back_radius)
        sketchLineExtrudeCut(-pocket_thickness)
      
        # pocket 2
        pocket_back_x += pocket_back_width + rib_width
        pocket_front_x += pocket_front_width + rib_width
        my_point = Point(pocket_back_x, pocket_back_y, 0.0)                            # start at left back
        sketchLineStart("seat_pocket2", seatComponent, seatComponent.xYConstructionPlane, my_point)
        my_point.x = pocket_front_x; my_point.y = pocket_front_y; l0 = sketchLineNext(my_point)
        my_point.x += pocket_front_width; l1 = sketchLineNext(my_point)
        my_point.x = pocket_back_x+pocket_back_width; my_point.y = pocket_back_y; l2 = sketchLineNext(my_point)
        l3 = sketchLineClose()
        sketchLineFillet(l0, l1, rib_radius)
        sketchLineFillet(l1, l2, rib_radius)
        sketchLineFillet(l2, l3, rib_radius)
        sketchLineFillet(l3, l0, rib_radius)
        sketchLineExtrudeCut(-pocket_thickness)

        # pocket 3
        pocket_back_x += pocket_back_width + rib_width
        pocket_front_x += pocket_front_width + rib_width
        my_point = Point(pocket_back_x, pocket_back_y, 0.0)                            # start at left back
        sketchLineStart("seat_pocket2", seatComponent, seatComponent.xYConstructionPlane, my_point)
        my_point.x = pocket_front_x; my_point.y = pocket_front_y; l0 = sketchLineNext(my_point)
        my_point.x += pocket_front_width; l1 = sketchLineNext(my_point)
        my_point.x = pocket_back_x+pocket_back_width; my_point.y = pocket_back_y; l2 = sketchLineNext(my_point)
        l3 = sketchLineClose()
        sketchLineFillet(l0, l1, rib_radius)
        sketchLineFillet(l1, l2, rib_radius)
        sketchLineFillet(l2, l3, rib_radius)
        sketchLineFillet(l3, l0, rib_radius)
        sketchLineExtrudeCut(-pocket_thickness)
        
        # pocket 4
        pocket_back_x += pocket_back_width + rib_width
        pocket_front_x += pocket_front_width + rib_width
        my_point = Point(pocket_back_x, pocket_back_y, 0.0)                            # start at left back
        sketchLineStart("seat_pocket2", seatComponent, seatComponent.xYConstructionPlane, my_point)
        my_point.x = pocket_front_x; my_point.y = pocket_front_y; l0 = sketchLineNext(my_point)
        my_point.x += pocket_front_width; l1 = sketchLineNext(my_point)
        my_point.x = pocket_back_x+pocket_back_width; my_point.y = pocket_back_y; l2 = sketchLineNext(my_point)
        l3 = sketchLineClose()
        sketchLineFillet(l0, l1, rib_radius)
        sketchLineFillet(l1, l2, front_radius)
        sketchLineFillet(l2, l3, back_radius)
        sketchLineFillet(l3, l0, rib_radius)
        sketchLineExtrudeCut(-pocket_thickness)
       
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

Tags (1)
1 REPLY 1
Message 2 of 2
liujac
in reply to: mojobility

Hi,

 

Thanks for reporting this issue. I logged a defect in our internal system. We will fix it.

 

Jack

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