#!/usr/bin/env python # coding: utf-8 # ## 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[ ]: import k3d import numpy as np from numpy import sin,cos,pi from ipywidgets import interact, IntSlider # In[ ]: 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)] plt_cam_traj = k3d.line(cam_traj ,shader='simple') plt_object_traj = k3d.line(object_traj, color=0xff0000,shader='mesh') plt_cam_pos = k3d.points([[0, -1, 0]],point_size=0.1,shader='mesh') plt_object_pos = k3d.points([[0, -1, 0]],color=0xff0000,point_size=0.1,shader='mesh') plot += plt_cam_traj plot += plt_object_traj plot += plt_object_pos plot += plt_cam_pos # In[ ]: plot # In[ ]: plot.camera_auto_fit = False # In[ ]: @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[ ]: plot.camera_reset() # In[ ]: