jupyter-widgets / pythreejs

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

Appending objects to scene.children #26

Closed nlooije closed 6 years ago

nlooije commented 9 years ago

per this TODO, to add objects to scene.children from a linked function it is not possible to simply do:

scene.children.append(object)

instead according to the example notebook, we use:

scene.children = list(scene.children) + [point]

First, why is this as i don't understand why? and, second, I would propose to allow the use of the first method as it is typical Python usage.

SylvainCorlay commented 9 years ago

It is not so much a problem with pythreejs as it is a problem with traitlets.

Trait attributes don't send notifications when the values of sub-elements change, but if you re-assign the whole list, a notification will be sent.

One first thing that would make it less confusing fur users is to make children a tuple instead of a list. Since it is un-mutable, there is simply no append method.

Besides, there are already some experiments with eventful list and dicts implementation that would work with traitlets.

nlooije commented 9 years ago

Ok, this clarifies a lot already about the reassignment of the list. Thanks! Is it possible for me to see these experiments? I am very interested

SylvainCorlay commented 9 years ago

Sure, you can check out the eventful.py file in ipywidgets . Although there is a rework of the traitlets API in progress, and paving the way to descriptors other than TraitType enabling finer-grained events it definitely something we have in mind.

nlooije commented 9 years ago

Sorry if it is a bit off-topic but is it possible to use such an eventfullist to define a dynamic list of objects in the scene which can be displayed in a widget? I am trying to generate a widget with objects every time i add an object to the scene using a picker with a 'dblclick' event.

SylvainCorlay commented 9 years ago

I guess you could make such a feature work without using the eventfullist. However, it would not be as efficient.

nlooije commented 9 years ago

Do you have any hints how i would go about doing that? efficiency is not that important at the moment

SylvainCorlay commented 9 years ago

I don't think PyThreejs's scene propagates click events to the backend, but it could be interesting to add this to Pythreejs.In the meantime, you can use a button on the side to test the rest. Then have the handler for the event create the new items of the scene and "inefficiently" append it to the scene.

nlooije commented 9 years ago

Coming back to the reassignment 'feature': I would expect scene.children = list(scene.children) (i.e. making a simple copy) to also update the scene but this doesn't seem to happen. Is it because this is a shallow copy? The use case is that i remove some objects from the existing scene.children list using scene.children.remove(obj) in a loop and then want to update the scene with the above command.

vidartf commented 7 years ago

FYI: While the traitlets list issue is still relevant, the current master now exposes the add and remove functions on Object3D (and its subclasses), mirroring the relevant functions on the three.js API. These accept one or more objects, allowing this code to be cleaner. Referring back to the initial example, it would be: scene.add(object). Note that the old way should still work.