Processing 3.5.4 Python mode save result as SVG

Hi, I have some code, and the result I need save to SVG, but after all, I get PNG image, but SVG not, maybe someone can tell me, where is the problem

add_library('svg')
save_frame = False
w, h = 800, 800

length_depr = .5
max_depth = 11
min_length = 30

def draw_line(p, v, l, d, md):
    if save_frame:
        beginRecord(svg_output)
    if (d > md):
        return
    
    if (l < min_length):
        l = min_length
    
    
    start_point = p
    end_point = (p[0] + v[0] * l, p[1] + v[1] * l)
    
    #line(start_point[0], start_point[1], end_point[0], end_point[1])
    strokeWeight(4 - d/3)
    lr = -40
    hr = 40
    curve(start_point[0] + random(lr, hr), start_point[1] + random(lr, hr), start_point[0], start_point[1], end_point[0], end_point[1], end_point[0] + random(lr, hr), end_point[1] + random(lr, hr))
    
    v_one = v.copy()
    v_one.rotate(random(.1, .35) * PI)
    draw_line(end_point, v_one, l * length_depr, d + 1, md)
    
    v_two = v.copy()
    v_two.rotate(random(1.65, 1.85) * PI)
    draw_line(end_point, v_two, l * length_depr, d + 1, md)
    if save_frame:
        endRecord()
        exit()
    
    
              
def setup():
    global svg_output
    size(w, h)
    pixelDensity(2)
    
    background(255) #background color
    stroke(0)
    strokeWeight(1)
    noFill()
    
    
    stroke(0) #stroke color
    strokeWeight(1)
    noFill()
    tint(255,127)
    v = PVector(0.0, -1.0)
    draw_line((w/2, h), v, h/3, 0, 10)

    
    save("result/ex.png")
    svg_output = createGraphics(w, h, SVG, "result/file.svg")
    
def keyPressed():
    global save_frame
    if key == 'e':
        save_frame = True
1 Like

Welcome @edipii

I think you’re overcomplicating things here. There’s no animation, so no need for a keyPressed() function to capture a specific instance in time. It’s probably easiest just to add SVG and filename arguments to the size() line (instead of using the record approach). For example –

add_library('svg')

def draw_line():
    line(0, 0, 100, 100)

def setup():
    size(800,800, SVG, 'a.svg')

def draw():
    strokeWeight(5)
    draw_line()
    exit()
2 Likes

Thank you, it’s worked for me

Many thanks from me

2 Likes