Jump to content

createInstance with children


Recommended Posts


Is there a simple way to create instances of a mesh with its children (and grandchildren) meshes ?

I have a tree made of a hierarchy of custom meshes : treeGround (juste a square ground for texture like tree roots, dirt and grass), a trunk wich has treeGround as parent, a branch wich has trunk as parent, and other branches wich are instances of the first branch and also have trunk as parent (and are moved and rotated around the trunk).

When i try to create instance of treeGround i get only the ground, not the tree, same if i try to instanciate the trunk i dont get the branches.

So is there a way to create an instance of the whole hierarchy ? Would cloning instead of instancing be better ?

Link to comment
Share on other sites

This can help hopefully : 

 public createInstanceAsset(name:string, newname?:string) : BABYLON.AbstractMesh {
        var model : BABYLON.Mesh = this.assets[name];
        var childrens = model.getDescendants();
        if (!newname) {
            newname = name+'_instance';
        var mesh = model.createInstance(newname);
        for (let c of childrens) {
            var child = <BABYLON.Mesh> c;
            var inst = child.createInstance('');
            inst.parent = mesh;
        return mesh;


Link to comment
Share on other sites

Thanks Temechon, that's the idea, I just wanted to know i there a was already a buld-in Mesh method or a parameter of createInstance to do this.

It might be better to use getChildren instead of getDescendants and make a recursive call to the function to keep the same hirearchy and pass the mesh as parameter instead of the name.

Now i'm trying to make this work but in my case I get an empty array when I do tree.getChildren() or tree.getDescendants(), no idea why :(

Link to comment
Share on other sites


Here's a playground scene I found which helped me both in instancing and cloning children. It doesn't matter that the example is cloning children in a skeleton hierarchy - just modify the code as needed. However, I highly recommend that you export a .babylon file from Blender in which the parent child relationship is already set up ( check the hierarchy in Blender), then using clone instead of instance will automatically import both parent and children without any additional code. This saves me LOADS of time.


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...