jupyter-widgets / pythreejs

A Jupyter - Three.js bridge
https://pythreejs.readthedocs.io
Other
951 stars 188 forks source link

interactive widgets and pythreejs #21

Closed oroszl closed 9 years ago

oroszl commented 9 years ago

Hi Guys! I have some (maybe simple) questions regarding the usage of pythreejs. I shall open a new issue for each. Here comes the first: How can I use Interactive widgets and pythreejs combined ? If I simply try something like:

rnd=Rendnerer(... my setting...)

@interact(x=(-3,3,1))
def do_something(x):
      rnd.scene=make_my_scene(x)
      display(rnd)

I get a strange behavior. Every time I change the slider a new rendnering is displayed below my graph (webgl window). Altering the slider again I get yet another copy below the previous two.. Some properties of these graphs are linked.. like zooming and viewpoint. However they are showing different scenes (each reflecting their own slider position). What is the proper way to update the widgets returned by the Renderer ? Using the above method produces no such doubling of output in case of simple matplotlib figures.. however I guess matplotlib figures are quite different than webgl stuff.. (am I right ?? ) Sorry for my obvious misconceptions but I am relatively new to ipython and a complete analphabet regarding javascript. cheers laszlo

jasongrout commented 9 years ago

You could just change individual attributes in the scene to modify, and it should update as well. You shouldn't display rnd again. Here is an example:

ball = Mesh(geometry=SphereGeometry(radius=3), material=LambertMaterial(color='red'), position=[2,5,0])
scene = Scene(children=[ball, AmbientLight(color=0x777777)])
c = PerspectiveCamera(position=[0,5,5], up=[0,0,1], children=[DirectionalLight(color='white', 
                                                                             position=[3,5,1], 
                                                                             intensity=0.5)])
renderer = Renderer(camera=c, scene = scene, controls=[OrbitControls(controlling=c)])
display(renderer)
@interact(x=(-3.0,3.0))
def f(x):
    ball.position=[x,x,x]
oroszl commented 9 years ago

Thanks, your example works like a charm.. :) I guess i still need to wrap my head around how I would like to organize my Mesh-es and Scenes. I think I can close this issue.