Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object3D: Add removeAll(). #20478

Merged
merged 1 commit into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/api/en/core/Object3D.html
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ <h3>[method:this remove]( [param:Object3D object], ... )</h3>
Removes *object* as child of this object. An arbitrary number of objects may be removed.
</p>

<h3>[method:this removeAll]()</h3>
<p>
Removes all child objects.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removeAll(). Remove all what?

Does it remove the object? The object's children? The children of the children?

And how are they disposed?

This perhaps should be an app-level method, not a library method.

Copy link
Owner

@mrdoob mrdoob Oct 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removes all children, but doesn't traverse the hierarchy.
I've found myself needing this pretty often, but yes... the dispose situation can be confusing...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think it belongs in the library. The proper implementation is app-specific.

But at a minimum, I think the name should be changed to something else.

Copy link
Collaborator Author

@Mugen87 Mugen87 Oct 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we should probably change remove() to removeChild(), too. Or event to removeChildButNotChildrenOfChild()...

TBH, I think the name (and the method itself) is totally fine. Improving the documentation to provide more clarity is of course always a good idea.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But at a minimum, I think the name should be changed to something else.

Neither Mugen87 nor I are native english speakers. Any help with naming is always welcome 🙏

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to go with .clear() which is what I used in ui.js.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

@Mugen87 Mugen87 Oct 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also breaks the TS build:

src/cameras/CubeCamera.d.ts(16,2): error TS2416: Property 'clear' in type 'CubeCamera' is not assignable to the same property in base type 'Object3D'.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I'll think about this.

</p>

<h3>[method:this rotateOnAxis]( [param:Vector3 axis], [param:Float angle] )</h3>
<p>
axis -- A normalized vector in object space. <br />
Expand Down
5 changes: 5 additions & 0 deletions src/core/Object3D.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ export class Object3D extends EventDispatcher {
*/
remove( ...object: Object3D[] ): this;

/**
* Removes all child objects.
*/
removeAll(): this;

/**
* Adds object as a child of this, while maintaining the object's world transform.
*/
Expand Down
19 changes: 19 additions & 0 deletions src/core/Object3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,25 @@ Object3D.prototype = Object.assign( Object.create( EventDispatcher.prototype ),

},

removeAll: function () {

for ( let i = 0; i < this.children.length; i ++ ) {

const object = this.children[ i ];

object.parent = null;

object.dispatchEvent( _removedEvent );

}

this.children.length = 0;

return this;


},

attach: function ( object ) {

// adds object as a child of this, while maintaining the object's world transform
Expand Down
9 changes: 8 additions & 1 deletion test/unit/src/core/Object3D.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export default QUnit.module( 'Core', () => {

} );

QUnit.test( "add/remove", ( assert ) => {
QUnit.test( "add/remove/removeAll", ( assert ) => {

var a = new Object3D();
var child1 = new Object3D();
Expand Down Expand Up @@ -328,6 +328,13 @@ export default QUnit.module( 'Core', () => {
assert.strictEqual( a.children[ 0 ], child2, "The second one is now the parent's child again" );
assert.strictEqual( child1.children.length, 0, "The first one no longer has any children" );

a.add( child1 );
assert.strictEqual( a.children.length, 2, "The first child was added to the parent" );
a.removeAll();
assert.strictEqual( a.children.length, 0, "All childrens were removed" );
assert.strictEqual( child1.parent, null, "First child has no parent" );
assert.strictEqual( child2.parent, null, "Second child has no parent" );

} );

QUnit.test( "getObjectById/getObjectByName/getObjectByProperty", ( assert ) => {
Expand Down