jupyter-widgets / pythreejs

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

Using one camera to light 2 scenes doesn't work #340

Closed jimbogill closed 3 years ago

jimbogill commented 3 years ago

Hi,

When I run the following code in a Jupyter Notebook, the first sphere is only lit by AmbientLight:

from pythreejs import *

# There is one camera and one control
camera = PerspectiveCamera(position=[0, -80, 40], fov=20,
                           children=[DirectionalLight(color='white', position=[10, 10, 10], intensity=0.5)])
control = OrbitControls(controlling=camera)
def display_sphere():
    mesh = Mesh(SphereGeometry(center = [0, 0, 0], radius = 15), material=MeshPhongMaterial(color='green'))
    scene = Scene(children=[mesh, AmbientLight(color='white'), camera])
    render = Renderer(camera=camera, scene=scene, controls=[control])
    display(render)
display_sphere()
display_sphere()

It works fine if the 2 scenes have different cameras. But I want to be able to view 2 slightly different objects with exactly the same light and view direction. It also works fine if I make AmbientLight a child of the scene and don't make the camera a child of the scene. But this gives a different behavior. Thanks, James

vidartf commented 3 years ago

You cannot have a single object be a part of multiple scenes, unfortunately. This is both to do with how pythreejs organizes the hierarchy as well as how threejs itself handles things. Your best bet is probably to create a util function that copies and/or links all the properties of one light/camera with another.