# Doing a plot recipe that plots a varying number of meshes

**URL:** <https://discourse.julialang.org/t/doing-a-plot-recipe-that-plots-a-varying-number-of-meshes/111201>\
**Category:** General Usage\
**Tags:** glmakie\
**Created:** [March 5, 2024, 3:21pm UTC](https://discourse.julialang.org/t/doing-a-plot-recipe-that-plots-a-varying-number-of-meshes/111201 "2024-03-05T15:21:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Larbino1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/larbino1/32/37831_2.png) [@Larbino1](https://discourse.julialang.org/u/Larbino1)\
**Post date:** [March 5, 2024, 3:21pm UTC](https://discourse.julialang.org/t/doing-a-plot-recipe-that-plots-a-varying-number-of-meshes/111201/1 "2024-03-05T15:21:03Z")

</div>

I would like to create a recipe that can plot a variable number of meshes, supposing I have a list of GeometryBasics objects and a list of positions/quaternions.

I’m struggling to get it to work: this is my best attempt so far, and the position of the rectangle does not update:

```julia
using GeometryBasics
using GLMakie
import Makie
using Makie: Observable

###############################
# Utils
###############################

function translate_and_rotate_plot!(mesh, pos, quat)
    Makie.translation(mesh)[] = Makie.Vec3f(pos)
    Makie.rotation(mesh)[] = Makie.Quaternionf(quat...)
    nothing
end

Makie.@recipe(MultiMesh, meshes, positions, quaternions) do scene
    Makie.Theme(
        # Light comes from (0, 0, 15), i.e the sphere
        lightposition = Makie.Vec3f(15, 15, 15),
        # base light of the plot only illuminates red colors
        ambient = Makie.RGBf(0.3, 0, 0),
        camera = :cam3d!
    )
end

function Makie.plot!(plot::MultiMesh{Tuple{R, P, Q}}) where {R, P, Q}
    (;meshes, positions, quaternions) = plot
    
    meshplots = []

    function plotmeshes(meshes)
        empty!(meshplots)
        foreach(meshes) do geometry
            meshplot = Makie.mesh!(plot, geometry, linewidth=0.0)
            push!(meshplots, meshplot)
        end
    end

    function translatemeshes(positions, quaternions)
        for (mesh, p, q) in zip(meshplots, positions, quaternions)
            translate_and_rotate_plot!(mesh, p, q)
        end
    end

    Makie.Observables.onany(plotmeshes, meshes)
    Makie.Observables.onany(translatemeshes, positions, quaternions)
    
    plotmeshes(meshes[])
    translatemeshes(positions[], quaternions[])

    plot
end

meshes = Observable([Rect3(0., 0., 0., 1., 1., 1.)])
positions = Observable([Vec3f(0, 0, 0)])
quaternions = Observable([(1, 0, 0, 0)])

fig = Makie.Figure()
display(fig)
ax = Makie.LScene(fig[1, 1])
multimesh!(ax, meshes, positions, quaternions)

# This should move the cube, but doesn't
positions[] = [Vec3f(10, 0, 0)]

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [March 5, 2024, 3:37pm UTC](https://discourse.julialang.org/t/doing-a-plot-recipe-that-plots-a-varying-number-of-meshes/111201/2 "2024-03-05T15:37:40Z")

</div>

This is done in Meshes.jl: the `viz` recipe that takes any vector of geometries as input.
