jupyter-widgets / pythreejs

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

BufferGeometry/Attribute Lifecycle - Strange Behavior #256

Open jjennings955 opened 5 years ago

jjennings955 commented 5 years ago

The attributes dictionary of a BufferGeometry seems to be populated in a strange/unintuitive way.

It appears that it isn't available (in some cases) until you run a new cell in jupyter (and "run all" doesn't count).

I attached a gist with a minimal-ish example.

Example

vidartf commented 5 years ago

This issue is inherent to the way that pythreejs works: It is only a remote wrapper around three.js. This means that three.js has the code responsible for filling out the buffer attributes, and pythreejs knows nothing about it until three.js tells it the results. As such, trying to use the buffer attributes of a geometry created with from_geometry is not a recommended pattern. Instead, you can create the buffer geometries directly (e.g. copy pasting what you get with from_geometry into your code). If you only want a buffer geometry without access to the attributes, you can use IcosahedronBufferGeometry etc.

jjennings955 commented 5 years ago

It seems like if certain attributes are going to be undefined until some action is taken on the threejs side, some asynchronous pattern (like Futures/Promises) should be used.

vidartf commented 5 years ago

@jjennings955 There is a guide for asynchronous widgets here: https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20Asynchronous.html

Any suggestions for how to help integrate pythreejs with such a setup are very welcome 😃

jjennings955 commented 5 years ago

The code is a bit too complex for me to make sense of. There are a lot of callbacks internally in ipywidgets, so it should be possible to hook into one of those and recognize when the attributes have been created.

Something like buffer_geometry.observe(some_callback, 'attributes') would be great if it worked (but it doesn't seem to).

Unfortunately right now, there's really no way to know when these attributes have actually been created.