mrdoob / three.js

JavaScript 3D Library.
https://threejs.org/
MIT License
102.65k stars 35.37k forks source link

Printing debug view of scene graph? #10961

Closed donmccurdy closed 7 years ago

donmccurdy commented 7 years ago

Is there any existing feature for printing the scene tree to the console, e.g. for debugging? I'm finding it a bit time-consuming to expand node.children recursively in the JS Console. If not, I wanted to propose something along the lines of:

SceneUtils.log( node );
. <Scene>
├── <Object3D>
│   └── joint_1 <Bone>
│       └── joint_2 <Bone>
│           └── joint_3 <Bone>
├── fancy_character <SkinnedMesh>
├── <PointLight>
├── <AmbientLight>
└── bookshelf <Mesh>
takahirox commented 7 years ago

How about this one?

scene.traverse(function(obj) {
  var s = '';
  var obj2 = obj;
  while(obj2 !== scene) {
    s += '-';
    obj2 = obj2.parent;
  }
  console.log(s + obj.type + ' ' + obj.name);
});

If Object3D had depth property, the code would be much simpler...

WestLangley commented 7 years ago

A slight mod...

scene.traverse( function ( obj ) {

    var s = '|___';

    var obj2 = obj;

    while ( obj2 !== scene ) {

        s = '\t' + s;

        obj2 = obj2.parent;

    }

    console.log( s + obj.name + ' <' + obj.type + '>' );

} );
donmccurdy commented 7 years ago

Thanks, either of these works great. 🙂

Does this seem appropriate for e.g. SceneUtils, or should I just bookmark the snippet?

takahirox commented 7 years ago

As for me, bookmaking is good enough (just my opinion).

mrdoob commented 7 years ago

I think it's pretty handy! How about adding it to Object3D so we could do... scene.log() or mesh.log()...?

takahirox commented 7 years ago

Not bad. I wanna make PR.

What information should we display?

Anything else?

I'm thinking if we wanna pass the property list .log() displays, like this...

scene.log( [ 'position', 'quaternion' ] );

jostschmithals commented 7 years ago

I would like to suggest console.group() for outputting the scene graph. Here for the moment a very basic demo:

(function printGraph( obj ) {

    console.group( ' <' + obj.type + '> ' + obj.name );

    obj.children.forEach( printGraph );

    console.groupEnd();

} ( scene ) );  

If you open the dev tools in Chrome, you get the fully expanded tree, but you have also the possibility to collapse it partly to fit your needs.

Chrome, initial state (expanded):

scenegraph_chrome

Chrome, partly collapsed:

scenegraph_chrome_collapsed

A little downside: Firefox currently supports no collapsing, but I’ve tested it with FF Nightly, and in Nightly it works the same as in Chrome.

Current FF (52):

scenegraph_firefox_52

FF Nigthly:

scenegraph_firefox_nightly

donmccurdy commented 7 years ago

@jostschmithals that's a nice trick! along similar lines, if I add the %o placeholder, the console will print the entire tree expanded, while showing each individual node collapsed. You can expand e.g. Mesh to see its properties.

(function printGraph( obj ) {

    console.group( ' <%o> ' + obj.name, obj );

    obj.children.forEach( printGraph );

    console.groupEnd();

} ( scene ) );
screen shot 2017-03-09 at 10 50 50 am
jostschmithals commented 7 years ago

I just wanted to suggest this further enhancement too, but I think then it would be more eye-friendly to choose the order from your first post (to avoid nesting):

(function printGraph( obj ) {

    console.group(obj.name + ' <%o> ', obj);

    obj.children.forEach( printGraph );

    console.groupEnd();

}(scene));

scenegraph_doublecollapsing

jostschmithals commented 7 years ago

Unfortunately it seems that FF doesn't understand the %o-thing in the same manner :-(

WestLangley commented 7 years ago

The script I posted above appears to handle scenes with many children a bit better as it counts -- rather than repeats -- duplicate lines. Try this three.js example, which has 700+ child meshes.

|___ <Scene>
    |___ <Group>
        |___Bip01_Pelvis <Object3D>
        ...
        ...
    |___ <AmbientLight>
    |___ <PointLight>
725     |___ <Mesh>

... although it offsets the duplicates a bit.

jostschmithals commented 7 years ago

Indeed – this is not only „a bit”, but much more convenient in such a scenario (which I had not in mind)!

Would it perhaps make sense to have the possibility of switching between both types of implementation (combined in one method)? - I guess that the imaginable use cases can be very divergent, so that both implementations could be useful in different places.

makc commented 7 years ago

too bad I can't "favorite" issues on github

Mugen87 commented 7 years ago

Closing, see https://github.com/mrdoob/three.js/pull/10995#issuecomment-288256554

donmccurdy commented 7 years ago

For others interested, the three.js inspector Chrome extension also shows a nice interactive scene graph.

brainexcerpts commented 1 year ago

Chrome extension seems to be dead but the Three.js Developer Tools for Firefox is awesome and does far more than just displaying an interactive view of the scene tree (this extension adds a tab in Firefox dev tools (F12))