How to get root of the GLTF file in SceneLoader.Append() and rename it

Hi,

So we are rendering a list of models that are already been converted in blob now when loading these models we have to rename each mesh root and add them in in a JSON so that we can do further operations on it.

the challenge is that scene returns all the meshes in the callback and we just need the root of each mesh that should be mapped with the URL passed. The callback is async and takes one parameter i.e. scene and we cannot pass the string which we want to use as the name of the root mesh

    data.map((modelData) => {
            var url = URL.createObjectURL(modelData.modelPath)
            console.log(modelData.modelPath) // Blob URL of model
            console.log(modelData.newName) // New name for the root
            SceneLoader.Append(
              '',
              url,
              scene,
              function (scene) {
                console.log(scene.meshes)
                scene.meshes.map((mesh) => {
                    // ToDo: Get root of the mesh which are loaded and rename it -> modelData.newName
                })
              },
              undefined,
              undefined,
              '.glb'
            )
          })

regards,
Nipun David

Hey!

Before calling append, you can store the number of scene.meshes. It will give you the index of your root mesh after the append :wink:

1 Like

Thanks, this forum never lets me down :smiley:

Oo BTW we found a way around - One of my colleagues also asked the same question so we ended up having two solutions and we are using something like this

SceneLoader.ImportMesh(
          '',
          '',
          url,
          scene,
          function (newMeshes) {
            newMeshes[0].name = modelData.modelOptionName
          },
          null,
          null,
          '.glb'
        )
      })

Thanks a lot @Deltakosh

we also wanted to get the root of the mesh which is clicked by the user - for now we are traversing all the way to the parent till we reach root - I believe there must be some optimized way for this - any thoughts on that - here is the sample snippet

export function selectMesh(_3DObject) {
  let root = getParent(_3DObject);
  return root;
}

function getParent(_3DObject) {
  let child = _3DObject;
  let parent;
  do {
    if (child.parent === null) {
      parent = _3DObject;
      break;
    } else {
      parent = child.parent
      child = parent;
    }
  } while (child.parent !== null);
  return parent
}

regards,
Nipun David

1 Like