jupyter-widgets / pythreejs

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

Nothing rendered in jupyter notebook. #334

Closed bingjeff closed 3 years ago

bingjeff commented 3 years ago

Using pipenv I roughly have installed jupyter and pythreejs using python 3.7. A snippet of the versioning:

pythreejs==2.2.0

Widgets seem to work fine, but I have nothing rendered when I run the simple sphere example, just an empty output cell.

Checking the javascript console there is only a few warnings about threejs not being able to invert singular matrices. There is a healthy print that it rendered: THREE.WebGLRenderer 97

jupyter nbextension list shows everything enabled:

Known nbextensions: config dir: /home/bingjeff/.local/share/virtualenvs/primitive_hulls-T7x0kADv/etc/jupyter/nbconfig notebook section jupyter-datawidgets/extension enabled

Should be reproducible with this repo: https://github.com/bingjeff/primitive_hulls

Anything else I should be doing?

vidartf commented 3 years ago

I would try to use the browser console to inspect the cell outputs, and see if either and img or canvas element is there. It might be that the images is either too small too see, or just plain white.

bingjeff commented 3 years ago

There is a canvas element, but presumably it isn't evaluated. I am not up on the canvas elements to know if the mismatch between style-width/height and width/height size means anything. I will try and set a larger size for the widget and see if it does anything.

<div class="output_wrapper">
<div class="out_prompt_overlay prompt" title="click to scroll output; double click to hide"></div>
<div class="output">
  <div class="output_area">
    <div class="run_this_cell"></div>
    <div class="prompt"></div>
    <div class="output_subarea jupyter-widgets-view" dir="auto">
      <div class="jupyter-widget jupyter-threejs" style="">
        <canvas width="320" height="320" style="width: 200px; height: 200px;"></canvas>
      </div>
    </div>
  </div>
</div>
bingjeff commented 3 years ago

Still nothing. Canvas changes size, but nothing is rendered.

This is the code I am running:

from pythreejs import *
import numpy as np
from IPython.display import display
from ipywidgets import HTML, Text, Output, VBox
from traitlets import link, dlink

ball = Mesh(geometry=SphereGeometry(radius=1, widthSegments=32, heightSegments=24), 
            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,
                    width=800,
                    height=800,
                    controls=[OrbitControls(controlling=c)])
display(renderer)
bingjeff commented 3 years ago

Stepped through the javascript in Renderable.js and things appear to be fine. Is it possible that there is something problematic in the camera setup? This is the only call that appears to cause the warning on a division by zero. Perhaps the camera is not fully initialized and therefore there is nothing that is actually being rendered?

bingjeff commented 3 years ago

And solved-ish.

At least in my setup the default scale for the Object3d inherited objects is all zero, which messes up the projection matrices.

Solved by adding scale arguments to everything:

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

c = PerspectiveCamera(scale=[1,1,1], position=[0, 5, 5], up=[0, 1, 0],
                      children=[DirectionalLight(color='white', position=[3, 5, 1], intensity=0.5)])
scene = Scene(scale=[1,1,1], children=[ball, c, AmbientLight(color='#777777')])
vidartf commented 3 years ago

That seems weird. I'm not able to reproduce that for version 2.2.0 (I didn't try using pipenv though). Is there any chance something is bleeding into your env from somewhere else?

bingjeff commented 3 years ago

Tried it on two different computers, running two different OS's (osx and debian). Same issue for both.

Only thought is that there is a transient dependency somewhere that is different. The nice thing about pipenv is the lockfile makes that explicit.

If I were guessing I would suspect changes to threejs? When googling I found some other references suggesting changes to how 0 scaling is handled. Is it possible my install is using a newer version of threejs?

On Tue, Sep 8, 2020, 9:58 AM Vidar Tonaas Fauske notifications@github.com wrote:

That seems weird. I'm not able to reproduce that for version 2.2.0 (I didn't try using pipenv though). Is there any chance something is bleeding into your env from somewhere else?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jupyter-widgets/pythreejs/issues/334#issuecomment-689010608, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA3SLWX35IAJZ7FWGXJBD5DSEZPDPANCNFSM4Q3JTHAQ .

vidartf commented 3 years ago

For the nbextension three is bundled with the extension, and for lab it is locked to a major version (an older one that has not been changed in quite a while), so that should not be an issue unless you are getting it from a non-official source.

vidartf commented 3 years ago

The nice thing about pipenv is the lockfile makes that explicit.

Could you try with a fresh install with latest version of all packages? Would you be able to share that lockfile or requirements.txt for repro?

bingjeff commented 3 years ago

I can't see anything that isn't up-to-date if you want to try you should be able to just download my pipfile. Here is the lock-file:

https://github.com/bingjeff/primitive_hulls/blob/master/Pipfile.lock

jpanetta commented 3 years ago

I'm also having this problem with a fresh install in a VM. It definitely looks like one of the dependencies changed and broke things (but not threejs itself as Vidar said; that's locked at r97 here and r107 in the fork I've been working on--and both are now broken in the same way).

nicolas-rabault commented 3 years ago

I think I have the same problem, and the scale tricks seems to fix it also. I'm an epic noob in JS but in my chrome JS console I have some red things :

Capture d’écran 2020-09-09 à 18 19 56

Here is my project : https://github.com/Luos-io/Examples/tree/master/Projects/Bike_alarm/Connected_bike

Also a friend recently reinstall things and says me that he have the same kind of issue with mathplotlib.

jpanetta commented 3 years ago

All right, I was finally able to figure out what broke things (at least for me): the recent update of traitlets to version 5.0. Despite the release notes claiming the update shouldn't require any code changes for ipywidgets, clearly something is incompatible.

Doing a pip3 install traitlets==4.3.3 seems to get things working again, at least for now.

vidartf commented 3 years ago

It's hard to see exactly what could cause such an error from the diff (https://github.com/ipython/traitlets/compare/4.3.x). If you are able to narrow it down some more, please comment further 👍

jasongrout commented 3 years ago

Traitlets 5 drops support for python 3.6 - do you think that might be the problem?

jpanetta commented 3 years ago

@vidartf if I understand correctly, the diffs in your link show just a few changes from the 4.3.3 branch that weren't merged into master. Doing the comparison the other way around shows considerably more changes in version 5.0 that I worry could be relevant. I'll try looking into it some more, but I agree it is not obvious what's to blame.

@jasongrout I am using Python 3.8.2, so I don't think that's the problem.

vidartf commented 3 years ago

@vidartf if I understand correctly, the diffs in your link show just a few changes from the 4.3.3 branch that weren't merged into master. Doing the comparison the other way around shows considerably more changes in version 5.0 [...]

Aha 😄 Yes, there are some changes to the logic for defaults. I wonder if any of our trait declarations or types end up changing behavior because of that. I'll run some quick tests.

jiangzhongshi commented 3 years ago

Just for some more information: I have the same error today and I am able to get it reproduced with

import pythreejs as p3s

cam = p3s.PerspectiveCamera(position=[0, 0, 1], lookAt=[0, 0, 0], fov=30,
                                     aspect=1, children=[])
scene = p3s.Scene(children=[cam], background='#ffffff')

mean = (1,0,0)
cam.lookAt(mean)
cam.exec_three_obj_method('updateProjectionMatrix')

and the more complete error message is here https://github.com/skoch9/meshplot/issues/24

The same workaround of @jpanetta helped. Thanks Julian!