jupyter-widgets / pythreejs

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

Can't update the radius of a SphereGeometry geometry #184

Closed JuanCab closed 6 years ago

JuanCab commented 6 years ago

I was playing with the examples at https://github.com/jupyter-widgets/pythreejs/blob/master/examples/Examples.ipynb because I was unable to dynamically change the radius of a sphere in my notebook when I discovered that the sample code doesn't work for me.

Using pythreejs-1.0.0, when I run the following:

from pythreejs import *
from IPython.display import display
import time, math

ball = Mesh(geometry=SphereGeometry(radius=1, widthSegments=32, heightSegments=16), 
            material=MeshLambertMaterial(color='red'),
            position=[2, 1, 0])

c = PerspectiveCamera(position=[0, 5, 5], up=[0, 1, 0],
                      children=[DirectionalLight(color='white', position=[3, 5, 1], intensity=0.5)])

scene = Scene(children=[ball, c, AmbientLight(color='#777777')])

renderer = Renderer(camera=c, 
                    scene=scene, 
                    controls=[OrbitControls(controlling=c)])
display(renderer)
ball.material.color = '#4400dd'
for i in range(1, 150, 2):
    ball.geometry.radius = i / 100.
    ball.position = [math.cos(i / 10.), math.sin(i / 50.), i / 100.]
    time.sleep(.05)

I get an animation of a blue color ball moving in circles, but the radius of the ball doesn't update after being initially set to 1, despite the fact that ball.geometry.radius is being updated.

vidartf commented 6 years ago

The radius is only meant for initial geometry creation. I guess it cold be made readonly. To resize the ball change the scale of the Mesh object instead.

JuanCab commented 6 years ago

Thank you, changing the scale of the Mesh did the trick! However, the example notebook needs to be fixed.

vidartf commented 6 years ago

Agreed! Todos:

vidartf commented 6 years ago

There might be some other issues with making it read only, so I'm dropping that, and instead refer to the three.js docs:

Each of the contructor parameters is accessible as a property of the same name. Any modification of these properties after instantiation does not change the geometry.

Will fix the example now.