jupyter-widgets / pythreejs

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

Push state of TrackballControls to Python? #197

Closed jpanetta closed 6 years ago

jpanetta commented 6 years ago

The target member variable of my TrackballControls Python object doesn't update when I use the panning gesture (though changes made to this variable in Python are pushed to JavaScript). I made the following two attempts in TrackballControls.update, but neither of these functions exist:

_this.model.set('target', _this.target);
_this.syncToModel(true);

How can I go about this (I'm trying to save and load the entire viewer state from my notebook)?

vidartf commented 6 years ago

Thanks, I opened a PR to fix this in #210. Are there any other attributes that need to be pushed back?

jpanetta commented 6 years ago

Great, thanks! I believe this is enough to restore my viewer state.

jpanetta commented 6 years ago

Hmm, when I actually test this, it still doesn't work for me: with the example below, I'm still reading the default (0.0, 0.0, 0.0) value from the target variable. By setting a breakpoint on the synchronization code you added in #210 I can see that this.obj.target holds the correct nonzero value--it's just somehow not making it to Python.

from pythreejs import *
import time

scene = Scene(children=[AmbientLight(intensity=0.5)])

vertices = BufferAttribute(array=np.array(
        [[-1.0, -1.0,  0.0],
         [ 1.0, -1.0,  0.0],
         [ 1.0,  1.0,  0.0]], dtype=np.float32), normalized=False)
index = BufferAttribute(array=np.array(
        [[0, 1, 2]], dtype=np.uint16).ravel(), normalized=False)
geom = BufferGeometry(attributes={
        'position': vertices,
        'index': index,
    })

surf=Mesh(geometry=geom, material=MeshLambertMaterial(color='red'))
scene.add(surf)

c = PerspectiveCamera(position=[0, 0, 1], up=[0, 0, 1])
controls = OrbitControls(controlling=c)
Renderer(camera=c, scene=scene, controls=[controls])

# Drag with the right mouse button to change the target, then run:

controls.target
vidartf commented 6 years ago

Went a little quick! New attempt in #214. This time I also tested the code, and it works locally. Can you confirm?

jpanetta commented 6 years ago

Yes, this change fixes things for me too. Thanks again!