How to clear a plot from a scene with other elements in GLMakie

Well, I’m new to using GLMakie. I wrote a script where I want to capture mouse events and plot the points using the scatter! function. In this same scene, I would like to insert a button with the ability to clear the points that were drawn (that is reset the scene!). I couldn’t achieve the desired result using the empty! command. If anyone can help, I’d greatly appreciate it. Below is the sample code.

using GLMakie
GLMakie.activate!() # hide
function gui()
     scene = Scene(camera = campixel!,size=(800,600))
     points = Observable(Point2f[])
     scatter!(scene, points, color = :gray)
     btn = Button(scene, label = "Clean")
on(btn.clicks) do _
    empty!(scene.plots)
    points = Observable(Point2f[])
    scatter!(scene, points, color = :gray)
    println(scene.plots)
    println(points)
end
on(events(scene).mousebutton) do event
    if event.button == Mouse.left
        if event.action == Mouse.press 
            mp = events(scene).mouseposition[]
            push!(points[], mp)
            notify(points)
        end
    end
end
scene
end
1 Like

Quite close, but you need empty!(scene)

using GLMakie
function gui()
    scene = Scene(camera=campixel!, size=(800, 600))
    points = Observable(Point2f[])
    scatter!(scene, points, color=:gray)
    btn = Button(scene, label="Clean")
    on(btn.clicks) do _
        empty!(scene)
        points = Observable(Point2f[])
        scatter!(scene, points, color=:gray)
    end
    on(events(scene).mousebutton) do event
        if event.button == Mouse.left
            if event.action == Mouse.press
                mp = events(scene).mouseposition[]
                push!(points[], mp)
                notify(points)
            end
        end
    end
    scene
end
gui()

First of all, thank you very much for the prompt help. The problem with the proposed solution is that the entire scene will be cleared, including the button I created. I would have to recreate the button, but in this case, it seems I will encounter an error with the message: Can only use scenes with PixelCamera as topscene.

I think what you actually want is this. You do not need to delete your scatter, you can just update points with an empty array (you could also do empty!(points[]) and then notify(points)).

If you really want to delete the scatter completely for some reason, it’s better to do scat = scatter!(...) and then delete!(scene, scat).

using GLMakie
function gui()
    scene = Scene(camera=campixel!, size=(800, 600))
    points = Observable(Point2f[])
    scatter!(scene, points, color=:gray)
    btn = Button(scene, label="Clean")
    on(btn.clicks) do _
        points[] = []
    end
    on(events(scene).mousebutton) do event
        if event.button == Mouse.left
            if event.action == Mouse.press
                mp = events(scene).mouseposition[]
                push!(points[], mp)
                notify(points)
            end
        end
    end
    scene
end
gui()
1 Like

Perfect! That was exactly what I needed! Thank you very much!