K3D-tools / K3D-jupyter

K3D lets you create 3D plots backed by WebGL with high-level API (surfaces, isosurfaces, voxels, mesh, cloud points, vtk objects, volume renderer, colormaps, etc). The primary aim of K3D-jupyter is to be easy for use as stand alone package like matplotlib, but also to allow interoperation with existing libraries as VTK.
MIT License
921 stars 123 forks source link

Callback on camera change? #317

Closed ghost closed 2 years ago

ghost commented 2 years ago

From the docs:

Is is synchronized between frontend and backend automatically. Below there is an example of camera manipulation in Python backend.

Is there any way to execute your own python code on camera changed events? I have been searching through the code a bit:

https://github.com/K3D-tools/K3D-jupyter/blob/dab98020eef3a7c92e04d462342810d2d324ab77/js/src/providers/threejs/initializers/Canvas.js#L21

https://github.com/K3D-tools/K3D-jupyter/blob/539c53cab580d55b8841bb87589ab3d4cf95bdb0/js/src/k3d.js#L355

and it seems to revolve around this K3D.events.CAMERA_CHANGE event, but I can not find where k3d.plot.camera is updated on the python side

artur-trzesiok commented 2 years ago

hi @MironLeon

K3D is build over ipywidgets. Ipywidgets are build over traitlets :) - so...

import k3d
import ipywidgets

out = ipywidgets.Output()
plot = k3d.plot()
plot.display()

def on_camera_change(c):
    with out:
        print(c)

plot.observe(on_camera_change, 'camera')
out

Please look at line: plot.observe(on_camera_change, 'camera')

btw you can obvserve any attribute of plot and any of objects iny K3D! :)

ghost commented 2 years ago

Thanks! Didn't think it would be that easy