# Cannot change the init\_value of look\_at kwarg from Makie.Camera3D

**URL:** https://discourse.julialang.org/t/cannot-change-the-init-value-of-look-at-kwarg-from-makie-camera3d/118250
**Category:** Visualization
**Tags:** camera
**Created:** [August 16, 2024, 4:30am UTC](https://discourse.julialang.org/t/cannot-change-the-init-value-of-look-at-kwarg-from-makie-camera3d/118250 "2024-08-16T04:30:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![xczphysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xczphysics/32/211370_2.png) [@xczphysics](https://discourse.julialang.org/u/xczphysics)
#### Post date: [August 16, 2024, 4:30am UTC](https://discourse.julialang.org/t/cannot-change-the-init-value-of-look-at-kwarg-from-makie-camera3d/118250/1 "2024-08-16T04:30:05Z")

</div>

This is the code modified (only 1 line) from [Cameras | Makie](https://docs.makie.org/v0.21/explanations/cameras#Example-Visualizing-the-cameras-view-box)

HAVE tried some other walkarounds, none works, either…

```julia
using GeometryBasics, LinearAlgebra

function frustum_snapshot(cam)
    r = Rect3f(Point3f(-1, -1, -1), Vec3f(2, 2, 2))
    rect_ps = coordinates(r) .|> Point3f
    insert!(rect_ps, 13, Point3f(1, -1, 1)) # fix bad line

    inv_pv = inv(cam.projectionview[])
    return map(rect_ps) do p
        p = inv_pv * to_ndim(Point4f, p, 1)
        return p[Vec(1,2,3)] / p[4]
    end
end

ex = Point3f(1,0,0)
ey = Point3f(0,1,0)
ez = Point3f(0,0,1)

fig = Figure()
scene = LScene(fig[1, 1])
cc = Makie.Camera3D(scene.scene, 
    projectiontype = Makie.Perspective, 
    lookat = [0,0,0]) # --------------> not work, HAVE tried some other walkarounds, none works, either.

linesegments!(scene, Rect3f(Point3f(-1), Vec3f(2)), color = :black)
linesegments!(scene,
    [-ex, ex, -ey, ey, -ez, ez],
    color = [:red, :red, :green, :green, :blue, :blue]
)
center!(scene.scene)

cam = scene.scene.camera
eyeposition = cc.eyeposition
lookat = cc.lookat
frustum = map(pv -> frustum_snapshot(cam), cam.projectionview)

scene = LScene(fig[1, 2])
_cc = Makie.Camera3D(scene.scene, projectiontype = Makie.Orthographic)
lines!(scene, frustum, color = :blue, linestyle = :dot)
scatter!(scene, eyeposition, color = :black)
scatter!(scene, lookat, color = :black)

linesegments!(scene,
    [-ex, ex, -ey, ey, -ez, ez],
    color = [:red, :red, :green, :green, :blue, :blue]
)
linesegments!(scene, Rect3f(Point3f(-1), Vec3f(2)), color = :black)

fig

```

---

<div class="post-metadata">

### Author: ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)
#### Post date: [August 16, 2024, 6:37pm UTC](https://discourse.julialang.org/t/cannot-change-the-init-value-of-look-at-kwarg-from-makie-camera3d/118250/2 "2024-08-16T18:37:01Z")

</div>

In this case I think the method you’re using to construct `cc` doesn’t forward the `lookat` attribute.

You can set the value by setting `cc.lookat[] = Vec3f(0, 0, 0)` though! Though it only takes effect when you interact with the scene.

---

<div class="post-metadata">

### Author: ![xczphysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xczphysics/32/211370_2.png) [@xczphysics](https://discourse.julialang.org/u/xczphysics)
#### Post date: [August 17, 2024, 5:54am UTC](https://discourse.julialang.org/t/cannot-change-the-init-value-of-look-at-kwarg-from-makie-camera3d/118250/3 "2024-08-17T05:54:31Z")

</div>

Yep, modifying the built-in observable `cc.lookat[]` will work, when interacting with EPRL afterwards. But this can be done only after the script finishing running, so it is not that ‘automatic’ for involving unnecessary interaction.

I found that the problem seems to be that Makie updates `cc.lookat[]` for the overall geometric/gravitational center of all objects drawn.

Therefore, even if the default value of `cc.lookat[]` was previously set, it will still be automatically reset in the `fig` line.

**It seems that all other 3D scenes involving `LScene` have this feature or issue.** For example: [LScene | Makie](https://docs.makie.org/v0.21/reference/blocks/lscene#lscene) has the same issue:

```julia
using GLMakie

fig = Figure()
pl = PointLight(Point3f(0), RGBf(20, 20, 20))
al = AmbientLight(RGBf(0.2, 0.2, 0.2))
ax = LScene(fig[1, 1], show_axis=false, scenekw = (lights = [pl, al], backgroundcolor=:black, clear=true))
# now you can plot into lscene like you're used to
p = meshscatter!(ax, randn(300, 3), color=:gray)

cc = Makie.Camera3D(ax.scene)
println(cc.lookat[])
on(events(fig.scene).window_open) do event
    if event
        println(cc.lookat[])
    end
end

fig

```

---

<div class="post-metadata">

### Author: ![xczphysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xczphysics/32/211370_2.png) [@xczphysics](https://discourse.julialang.org/u/xczphysics)
#### Post date: [August 17, 2024, 6:04am UTC](https://discourse.julialang.org/t/cannot-change-the-init-value-of-look-at-kwarg-from-makie-camera3d/118250/4 "2024-08-17T06:04:51Z")

</div>

I found a temporary solution: **add a paragraph before the ‘fig’ line** :

```julia
on(events(fig.scene).window_open) do event
    if event
        cc.lookat[] = [0, 0, 0]
    end
end

```

The complete code is as follows:

```julia
using GeometryBasics, LinearAlgebra

function frustum_snapshot(cam)
    r = Rect3f(Point3f(-1, -1, -1), Vec3f(2, 2, 2))
    rect_ps = coordinates(r) .|> Point3f
    insert!(rect_ps, 13, Point3f(1, -1, 1)) # fix bad line

    inv_pv = inv(cam.projectionview[])
    return map(rect_ps) do p
        p = inv_pv * to_ndim(Point4f, p, 1)
        return p[Vec(1,2,3)] / p[4]
    end
end

ex = Point3f(1,0,0)
ey = Point3f(0,1,0)
ez = Point3f(0,0,1)

fig = Figure()
scene = LScene(fig[1, 1])
cc = Makie.Camera3D(scene.scene, 
    projectiontype = Makie.Perspective, 
    lookat = [0,0,0]) # --------------> not work, HAVE tried some other walkarounds, none works, either.

linesegments!(scene, Rect3f(Point3f(-1), Vec3f(2)), color = :black)
linesegments!(scene,
    [-ex, ex, -ey, ey, -ez, ez],
    color = [:red, :red, :green, :green, :blue, :blue]
)
center!(scene.scene)

cam = scene.scene.camera
eyeposition = cc.eyeposition
lookat = cc.lookat
frustum = map(pv -> frustum_snapshot(cam), cam.projectionview)

scene = LScene(fig[1, 2])
_cc = Makie.Camera3D(scene.scene, projectiontype = Makie.Orthographic)
lines!(scene, frustum, color = :blue, linestyle = :dot)
scatter!(scene, eyeposition, color = :black)
scatter!(scene, lookat, color = :black)

linesegments!(scene,
    [-ex, ex, -ey, ey, -ez, ez],
    color = [:red, :red, :green, :green, :blue, :blue]
)
linesegments!(scene, Rect3f(Point3f(-1), Vec3f(2)), color = :black)

on(events(fig.scene).window_open) do event
    if event
        cc.lookat[] = [0, 0, 0]
    end
end

fig

```

---

<div class="post-metadata">

### Author: ![xczphysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xczphysics/32/211370_2.png) [@xczphysics](https://discourse.julialang.org/u/xczphysics)
#### Post date: [August 20, 2024, 4:54pm UTC](https://discourse.julialang.org/t/cannot-change-the-init-value-of-look-at-kwarg-from-makie-camera3d/118250/5 "2024-08-20T16:54:17Z")

</div>

**It seems that other :kwargs of Camera3D cannot be modified from the defaults as well, by testing** :

```julia
cc.eyeposition[] = Point3f(0.75f0)
println(cc.eyeposition[])
on(events(fig.scene).window_open) do event
    if event
        println(cc.eyeposition[])
    end
end

```

before the ‘fig’ line.

**So I have to add some magics like** :

```julia
on(events(fig.scene).window_open) do event
    if event
        cc.lookat[] = [0, 0, 0]
        cc.eyeposition[] = Point3f(0.75f0)
    end
end

```

before the ‘fig’ line,

**to fix some bugs like** :

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/e/1e61d9cb822fe88facf7f3d2a25e7f587388191f.jpeg)

---

<div class="post-metadata">

### Author: ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)
#### Post date: [November 20, 2024, 2:21pm UTC](https://discourse.julialang.org/t/cannot-change-the-init-value-of-look-at-kwarg-from-makie-camera3d/118250/6 "2024-11-20T14:21:18Z")

</div>

thanks @xczphysics for figuring this magic out! very useful.

it would be nice if there were an “official” way to set the initial view of a 3D axis for all Makie backends. the `window_open` trick you outlined above works well for WGLMakie, but for GLmakie triggering on the event does not work as the window is already open, and so hence to write code that works for both, you have to duplicate what’s inside the do-block:

```julia
function init_3d()
    cc.lookat[] = [0, 0, 0]
    cc.eyeposition[] = Point3f(0.75f0)
end
on(e->e && init_3d(), events(fig.scene).window_open)
init_3d()

```
