Multiple mesh plots in Makie.jl scene from within a function

Viewed 414

How can I plot two meshes in Makie within a function?

From the REPL, I can do the following and get a plot with two cubes:

using Makie
A = FRect3D(Vec3f0(0), Vec3f0(1))
B = FRect3D(Vec3f0(1), Vec3f0(1))
s = Scene()
mesh(A, color = :red)
mesh!(B, color = :blue)

Two cubes from REPL

However, I can't seem to get it to work when inside a function call:

My attempt:

using Makie

function draw_cubes()
    A = FRect3D(Vec3f0(0), Vec3f0(1))
    B = FRect3D(Vec3f0(1), Vec3f0(1))
    mesh(A, color = :red)
    mesh!(B, color = :blue)
end

s=Scene()
draw_cubes()

leaves me with a blank scene, and returns

Combined{AbstractPlotting.mesh,Tuple{GeometryBasics.Mesh{3,Float32,GeometryBasics.Ngon{3,Float32,3,GeometryBasics.PointMeta{3,Float32,Point{3,Float32},(:uv, :normals),Tuple{Vec{2,Float32},Vec{3,Float32}}}},GeometryBasics.FaceView{GeometryBasics.Ngon{3,Float32,3,GeometryBasics.PointMeta{3,Float32,Point{3,Float32},(:uv, :normals),Tuple{Vec{2,Float32},Vec{3,Float32}}}},GeometryBasics.PointMeta{3,Float32,Point{3,Float32},(:uv, :normals),Tuple{Vec{2,Float32},Vec{3,Float32}}},GeometryBasics.NgonFace{3,GeometryBasics.OffsetInteger{-1,UInt32}},StructArrays.StructArray{GeometryBasics.PointMeta{3,Float32,Point{3,Float32},(:uv, :normals),Tuple{Vec{2,Float32},Vec{3,Float32}}},1,NamedTuple{(:position, :uv, :normals),Tuple{Array{Point{3,Float32},1},Array{Vec{2,Float32},1},Array{Vec{3,Float32},1}}},Int64},Array{GeometryBasics.NgonFace{3,GeometryBasics.OffsetInteger{-1,UInt32}},1}}}}}

Commenting out the mesh! line successfully plots the first cube just fine. I just can't get the second.

I've also tried passing the scene to the function, declaring the scene in the function (or using global s), with no luck.

Non-Makie solutions (so long as they are interactive plots), are fine as answers too... but I would like to figure out where I went wrong.

(I'm using Julia Version 1.5.3 (2020-11-09); Makie v0.12.0)

1 Answers

Passing the scene as an argument to the function seems to work, actually... it was not working when I was using mesh as the first call (giving a bunch of Warning: No strict ticks found and an ERROR: MethodError: no method matching _default(::Observable{Scene}, ::GLMakie.GLAbstraction.Style{:default}, ::Dict{Symbol,Any}))

However,

function draw_cube_scene(s::Scene)
        A = FRect3D(Vec3f0(0), Vec3f0(1))
        B = FRect3D(Vec3f0(1), Vec3f0(1))
        mesh!(s, A, color = :red)
        mesh!(s, B, color = :blue)
        s
    end

Does seem to work.

Related