question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Check for removal of children in the traverse method

See original GitHub issue
Description of the problem

Removing children while using the traverse function will result in an error. I propose a second check to make sure that l = children inside the loop, in case a node is removed. Original:

traverse: function ( callback ) {

			callback( this );

			var children = this.children;

			for ( var i = 0, l = children.length; i < l; i ++ ) {

				children[ i ].traverse( callback );

			}

		}

My proposal:

traverse: function ( callback ) {

			callback( this );

			var children = this.children;

			for ( var i = 0, l = children.length; i < l; i ++ ) {
                                if(l != children.length){
                                l=children.length;
                                i--;
                                }
				children[ i ].traverse( callback );

			}

		}

There might be a better way to do it, but that change got my code working.

Three.js version
  • Dev
  • r94
Browser
  • All of them
  • Chrome
  • Firefox
  • Internet Explorer
OS
  • All of them
  • Windows
  • macOS
  • Linux
  • Android
  • iOS
Hardware Requirements (graphics card, VR Device, …)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
WestLangleycommented, Jul 21, 2018

In general, Object3D.traverse() assumes that the structure of the scene graph does not change.

This should be documented.

@looeee IMHO, that is not necessary. It is reasonable to expect users to understand the ramifications of modifying a graph while traversing it. Same with an array. Save yourself some work. 😃

@jkulanko If you need further help, please use the forum.

Closing, as this is a user error.

0reactions
jkulankocommented, Jul 21, 2018

@aardgoose I’m going to have to look into that because that seems like it might be a very good solution to my problem, thanks!

@looeee That wouldn’t quite work for me, because of my unique situation. I am receiving an individual part asynchronously from a backserver which contains a mesh and a line. Each of the parts is a leaf node to a larger tree structure, and therefore needs to take into account the matrices of its parent (or many parents). So I want to keep each mesh and line segments together in a single object as that represents one leaf.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Traversing the DOM children of children - Stack Overflow
Being that it the children property returns a list, you can access the individual nodes in the collection by using either the item()...
Read more >
Check for Children Sum Property in a Binary Tree
Given a binary tree, the task is to check for every node, its value is equal to the sum of values of its...
Read more >
jQuery Traversing Descendants - W3Schools
With jQuery you can traverse down the DOM tree to find descendants of an element. A descendant is a child, grandchild, great-grandchild, and...
Read more >
Methods of Depth First Traversal and Their Applications
If the node (19) has a left node, we'll traverse it; it does, and its left node (12) has a left node (7)...
Read more >
Operations on Binary Search Tree's
There are several methods of traversing trees. Among them are the inorder, preorder and postorder traversal of nodes. Each of these traversal algorithms...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found