## Camera manipulation¶

An example of camera motion following an object. Blue line is a camera path and the object is moving on the red line.

In [9]:
import k3d
import numpy as np
from numpy import sin,cos,pi
from ipywidgets import interact, IntSlider

In [10]:
plot = k3d.plot()

plt_arrow = k3d.vectors([0,0,0],[1,1,1])
plot += plt_arrow

N = 100
cam_traj = [np.array([-sin(phi), -cos(phi), phi*0.15],dtype=np.float32) for phi in np.linspace(0,2*pi,N)]
object_traj = [np.array([sin(phi),-1+cos(phi)+sin(2*phi),0.03*phi],dtype=np.float32) for phi in np.linspace(0,2*pi,N)]

plot += plt_cam_traj
plot += plt_object_traj
plot += plt_object_pos
plot += plt_cam_pos

In [11]:
plot

In [12]:
plot.camera_auto_fit = False

In [13]:
@interact(ith=IntSlider(value=80,min=0,max=N-1))
def g(ith):
cam_pos = cam_traj[ith]
object_pos = object_traj[ith]
plot.camera = cam_pos.tolist()+\
object_pos.tolist()+\
[0,0,1]

cam_dir = np.array(object_pos,dtype=np.float32) - cam_pos

plt_cam_pos.positions = [cam_pos]
plt_object_pos.positions = [object_pos]

plt_arrow.origins = cam_pos
plt_arrow.vectors = cam_dir

In [15]:
plot.camera_reset()

In [ ]: