GLMakie & Observables with GeometryBasis or user defined types

Hey out there!

I have a question concerning Makie and the use of Observables. Here’s what I liked to do:

using GLMakie, GeometryBasics
mutable struct foo{T}
    x::Vector{T}
    r::T 
end

function makefig(foo::foo)
    x = Observable(Point2f(foo.a, foo.b))
    r = Observable(foo.r)
    fig, ax, s = poly(Circle(x, r), color=:pink)

    fig, ax, x, r
end

Now I want to change the Center and the radius of the circle to change as Im changing x, r like that:

fooo = foo([1.0, 1.0], 1.0)
fig, ax, x, r = makefig(fooo)
x[] = [1.0, 1.5]
r[] = 0.5

But this does not work because there is no method for Circle from GeometryBasics. However if I add a new method for my case like this it again does not update.

Now my question would be how to make this work or whether there is an easier way where I could define own mutable structs as observables and by mutating them the plot changes.

Thanks for any help!

It helps if you give the exact code that you run: if I run your code I get type foo has no field a. Also it helps to have the exact error message. Fixing the previous error, the error message becomes no method matching (Circle)(::Observable{Point{2, Float32}}, ::Observable{Float64}).

Indeed there is no method to make a Circle from observable values. But you can use the @lift macro to make an observable Circle from observable center and radius. This seems to work for me:

using GLMakie
using GeometryBasics

mutable struct foo{T}
    x::Vector{T}
    r::T 
end

function makefig(foo::foo)
    x = Observable(Point2f(foo.x[1], foo.x[2]))
    r = Observable(foo.r)
    c = @lift(Circle($x, $r))
    fig, ax, s = poly(c, color=:pink)

    fig, ax, x, r
end

fooo = foo([1.0, 1.0], 1.0)
fig, ax, x, r = makefig(fooo)
x[] = [1.0, 1.5]
r[] = 0.5
fig
1 Like

Ah Im an idiot, should have rerun the whole thing than I would have seen that I copied some old code.
I appreciate very much that you helped even with my stupid mistake! Thanks!

Yes that’s work for me aswell! Now I also unterstand how to use the @lift macro! Thanks a lot!

1 Like