Cloning a parented empty

Hi, I’ve attached a simple babylon file exported from blender. In this file, there is a Cube with an empty plain axes child (test_empty) at its center and another empty (test1_empty) plain axes at 2metres from the cube in the x-direction. Therefore there are only 2 objects: a Cube with an empty as a child and an empty itself.

In my script, I call ImportMesh which subsequently clones all the objects and disposes the source like so:

BABYLON.SceneLoader.ImportMesh("", "scenes/", "test.babylon", this.scene, function (newMeshes, particleSystems, skeletons) {
	var clonedObj = {};
	for ( var i = 0; i < newMeshes.length; i++){
		clonedObj = newMeshes[i].clone("obj_"+newMeshes[i].name);
		newMeshes[i].dispose();
		newMeshes[i] = null;
	}
	for ( var j = 0; j < _this.scene.meshes.length; j++){
		console.log(_this.scene.meshes[j].name);
	}
}

So I expected 3 objects in scene but its showing me 4 instead: obj_test1_empty, obj_Cube.test_empty, obj_Cube and obj_test_empty ???

obj_Cube.test_empty is showing the correct obj_Cube parent but obj_test_empty is showing null. As a result, in my app I’m getting a bloated scene as I have multiple empties parented to a mesh. Is this a bug or did I do smthg wrong ?

test.zip (703 Bytes)

So I’ve done a little more sleuthing work and not entirely sure where the bug is. Clone in source is working correctly but Importmesh is importing a cube that has no children and an empty that has a parent. Yet, during the cloning process, babylon sees the cube as having descendents and correctly clones the children. And test_empty gets cloned again as a separate entity, creating the extra object.

It seems like clone should check if mesh has a parentID before cloning but I do not know if it will break other use cases. Can’t repro in PG as this is strictly a case due to the xml file from Blender export, hmm…what to do now?

Why could we not get the file on the playground through rawgit?

Please create a repro in the playground (doc: Using External Assets - Babylon.js Documentation)
It will be easier to help then

Ah, thks didn’t know about loading external assets to PG. :slight_smile:
PG: https://www.babylonjs-playground.com/#8LFTCH#21
Pls open console, thks.

https://www.babylonjs-playground.com/#E21DTX

? does this help?

erm, no…the parent child relationship should be preserved by the cloning.

By default you clone with children so the children of cube is cloned twice (once for cube and once for itself).

When cloning the children we do not account for parent as it becomes a standalone entity.

In you case you should not clone the children but only root objects: https://www.babylonjs-playground.com/#8LFTCH#24

yh, this is the same workaround I’m using right now. What I don’t like about it is the need for another data structure to hold meshes which are not needed and consuming resources just for cleanup. Which got me into thinking that cloning itself could be optimized to cleanup the children in its for loop. But I couldn’t repro with native babylon empty meshes or transform node. Guess I don’t have a choice.

Sorry for posting in the wrong forum, it should have been in the Questions. To any mod, pls feel free to move it and mark this thread as solved. Thanks @sebavan and @Pryme8 for help, cheers !