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

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:

``````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)]
``````

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