# Makie: programmatic control of camera

**URL:** <https://discourse.julialang.org/t/makie-programmatic-control-of-camera/90949>\
**Category:** Visualization\
**Tags:** makie, glmakie\
**Created:** [November 28, 2022, 1:48pm UTC](https://discourse.julialang.org/t/makie-programmatic-control-of-camera/90949 "2022-11-28T13:48:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [November 28, 2022, 1:48pm UTC](https://discourse.julialang.org/t/makie-programmatic-control-of-camera/90949/1 "2022-11-28T13:48:42Z")

</div>

I’m trying a 3d visualization and would like to write code to control the view position & angle, and render individual frames that I can assemble into a movie. I expected something like the following to work:

```julia
julia> using GLMakie

julia> fig = Figure()

julia> ax = Axis3(fig[1, 1],
           xlabel = "x",
           ylabel = "y",
           zlabel = "z",
           aspect = (1, 1, 1)
       );

julia> scatter!(ax, rand(50), rand(50), rand(50))
Scatter{Tuple{Vector{Point{3, Float32}}}}

julia> cam = Camera3D(fig.scene);

julia> cam.eyeposition[]
3-element Vec{3, Float32} with indices SOneTo(3):
 3.0
 3.0
 3.0

julia> cam.eyeposition[] = (3, 3, 0)
(3, 3, 0)

```

but the scene did not update when I changed that property. Neither does `update_cam!(ax.scene, (3, 3, 0))` or anything else I’ve tried. Any tips?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 28, 2022, 2:05pm UTC](https://discourse.julialang.org/t/makie-programmatic-control-of-camera/90949/2 "2022-11-28T14:05:18Z")

</div>

An `Axis3` is not meant to be rotated around by manipulating the camera, it has `azimuth` and `elevation` properties for that. It is also not a free-view axis, because it was made to always fit into the rectangle it occupies in the layout. So usually you would use a standalone `Scene` or an `LScene` put into a layout of a `Figure` to get a free camera.

I’m not a 100% sure how to change that scene’s camera correctly, though.

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [November 28, 2022, 2:08pm UTC](https://discourse.julialang.org/t/makie-programmatic-control-of-camera/90949/3 "2022-11-28T14:08:24Z")

</div>

There was a bit of fiddling with cameras in this thread [Makie: Linking cameras in multiple 3d plots - #6 by Thomberger](https://discourse.julialang.org/t/makie-linking-cameras-in-multiple-3d-plots/83309/6). I hope it helps. Maybe try to act on `lookat` as well.

For 3D axes are you sure `update_cam!` takes a tuple ? It seems it can take another camera, or `update_cam!(scene::Scene, eyeposition, lookat, up = Vec3f(0, 0, 1))`

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [November 28, 2022, 2:50pm UTC](https://discourse.julialang.org/t/makie-programmatic-control-of-camera/90949/4 "2022-11-28T14:50:37Z")

</div>

Just for completeness, here is a little MWE that shows how to access the camera:

```julia
using GLMakie

fig, lscene, plot_obj = meshscatter(rand(Point3f, 10))

# if you want to directly access the camera:
cam3d = Makie.cameracontrols(lscene)

# API with a bit more of convenience:
zoom!(lscene.scene, 1.05)
rotate_cam!(lscene.scene, Vec3f(0, 0, 0.2))
translate_cam!(lscene.scene, Vec3f(0, 0, 1))

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 28, 2022, 3:04pm UTC](https://discourse.julialang.org/t/makie-programmatic-control-of-camera/90949/5 "2022-11-28T15:04:48Z")

</div>

The drawback off the free-view axis using `Scene` is that it doesn’t look good from all angles. But we could in principle improve it as well I think (it’s a very old piece of code so it hasn’t made use of a lot of features we added in the meantime that could help).

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [November 28, 2022, 7:02pm UTC](https://discourse.julialang.org/t/makie-programmatic-control-of-camera/90949/6 "2022-11-28T19:02:34Z")

</div>

Two other questions:

- Does the `Axis3` solution also allow a zoom? For me the best is `viewmode=:fit`, but there’s a bit more of a border around it than I like.
- Can I fly in with Axis3, or I should I use the older plain-`Scene` interface for that?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 28, 2022, 8:24pm UTC](https://discourse.julialang.org/t/makie-programmatic-control-of-camera/90949/7 "2022-11-28T20:24:14Z")

</div>

> It is also not a free-view axis, because it was made to always fit into the rectangle it occupies in the layout.

That’s what I tried to convey, you cannot zoom in because then all the axis decorations would break. In my mind you can’t really have both? Full freedom of camera movement and nice axis that behaves well in a layout that is.

Also, the viewmode `:stretch` fills the rectangle the most, but it ignores axis aspects.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [November 28, 2022, 9:25pm UTC](https://discourse.julialang.org/t/makie-programmatic-control-of-camera/90949/8 "2022-11-28T21:25:12Z")

</div>

Gotcha, thanks. I think I’m going to want to fly in, so I will probably use the classic `scene`-based solution in [Makie: programmatic control of camera - #4 by sdanisch](https://discourse.julialang.org/t/makie-programmatic-control-of-camera/90949/4).

One other obstacle I’ve noticed is that `record` doesn’t seem to follow `cam3d.eyeposition[] = newpos` updates that occur in the `do`-block. `display(fig)` or `display(lscene)` doesn’t seem to fix it. However, outside of `record` those take the intended effect.

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [November 29, 2022, 11:08am UTC](https://discourse.julialang.org/t/makie-programmatic-control-of-camera/90949/9 "2022-11-29T11:08:20Z")

</div>

`cam.eyeposition` etc aren’t directly connected to updates of the camera matrices. You have to call `update_cam!(scene, cam)` for that (or `update_cam(scene, cam, eyeposition, lookat[, up])`).
