Runtime Error: Unable to add polyline to document (Sierpinski Triangle)

Hi!
I am trying to program a recursive function in Python, that will create the Sierpinski Triangle, however, it seems that I ran into a problem… Whenever I try to increase the number of divisions to 3 or greater, Rhino displays the error message: 1. Solution exception:Unable to add polyline to document, unfortunately, I cannot locate the problem.

This is my code:

import rhinoscriptsyntax as rs

def SierpinskiSplit(input_tri):
    pts = rs.DivideCurve(input_tri,3)
    midpoint0 = (pts[0] + pts[1])/2
    midpoint1 = (pts[1] + pts[2])/2
    midpoint2 = (pts[2] + pts[0])/2
    triangle = rs.AddPolyline((midpoint0, midpoint1, midpoint2, midpoint0))
    return triangle


input_tri_pts = rs.CurvePoints(input_tri)
curr_tri = []
sierpinskiTriangle = [input_tri, SierpinskiSplit(input_tri)]
input_tri = [input_tri]

for i in range(0, divisions - 1):
    for elem in input_tri:
        input_tri_pts = rs.CurvePoints(elem)
        tri1 = rs.AddPolyline((input_tri_pts[0],(input_tri_pts[0]+input_tri_pts[1])/2, (input_tri_pts[2]+input_tri_pts[0])/2, input_tri_pts[0]))
        tri2 = rs.AddPolyline(((input_tri_pts[0]+input_tri_pts[1])/2,input_tri_pts[1], (input_tri_pts[1]+input_tri_pts[2])/2, (input_tri_pts[0]+input_tri_pts[1])/2))
        tri3 = rs.AddPolyline(((input_tri_pts[1]+input_tri_pts[2])/2, input_tri_pts[2],(input_tri_pts[2]+input_tri_pts[0])/2, (input_tri_pts[1]+input_tri_pts[2])/2))
        curr_tri.append(tri1)
        curr_tri.append(tri2)
        curr_tri.append(tri3)
    for tri in curr_tri:
        sierpinskiTriangle.append(SierpinskiSplit(tri))
    input_tri = curr_tri
    
a = sierpinskiTriangle

Sierpinski_Triangle_Problem.gh (4.5 KB)

Any help would be appreciated!
Thanks,
Michalina

You were really not far, just the initialization of curr_tri needed to be in the outer for loop to flush the previous values:

input_tri_pts = rs.CurvePoints(input_tri)
sierpinskiTriangle = [input_tri, SierpinskiSplit(input_tri)]
input_tri = [input_tri]

for i in range(0, divisions - 1):
    curr_tri = []  # <-- This was in the wrong place
    for elem in input_tri:
        input_tri_pts = rs.CurvePoints(elem)
        tri1 = rs.AddPolyline((input_tri_pts[0],(input_tri_pts[0]+input_tri_pts[1])/2, (input_tri_pts[2]+input_tri_pts[0])/2, input_tri_pts[0]))
        tri2 = rs.AddPolyline(((input_tri_pts[0]+input_tri_pts[1])/2,input_tri_pts[1], (input_tri_pts[1]+input_tri_pts[2])/2, (input_tri_pts[0]+input_tri_pts[1])/2))
        tri3 = rs.AddPolyline(((input_tri_pts[1]+input_tri_pts[2])/2, input_tri_pts[2],(input_tri_pts[2]+input_tri_pts[0])/2, (input_tri_pts[1]+input_tri_pts[2])/2))
        curr_tri.append(tri1)
        curr_tri.append(tri2)
        curr_tri.append(tri3)
    for tri in curr_tri:
        sierpinskiTriangle.append(SierpinskiSplit(tri))
    input_tri = curr_tri

Without this the list was growing exponentially.


Sierpinski_Triangle_Problem_fixed.gh (4.9 KB)

2 Likes

@Jacques_Gaudin
Thank you so much for your help! I guess I didn’t notice this little mistake, so next time I’ll double-check the logic of my code :upside_down_face:
Well, I get on with programming further fractals!

No worries, that can happen! Just keep it as simple as possible and use print statements to debug and check what the script is actually doing. Otherwise great work!