Looking for a for loop to search through nodemap and find ID of model based on name

Using the Sketchfab API examples and documentation Ive found the following code
api.getSceneGraph(function (err, result) {

    if (err) {
      console.log('Error getting nodes');
    } // get the id from that log

    for (let i = 0 ; i < result.length;i++) {
            console.log('Model Number' + i + result[i].name);

This returns all of the relevant information that I need including the material and object ID along with its name. But how do I get an array of the models/objects in the scene?

In the following code I am able to get exactly that with the material number
api.getMaterialList( function( err, materials ) {

  for (let i = 0 ; i < materials.length;i++) {
  if (materials[i].name == "Material__01_Red") {
  materialNumber_Group_01 = i;
            console.log('Material Number' + i + materials[i].name);
var materialToUpdate = materials[3];
var materialToCopy = materials[materialNumber_Group_01];

materialToCopy.stateSetID = materialToUpdate.stateSetID;

api.setMaterial( materialToCopy, function() {
    console.log( 'Material updated' );
} );

} );

Hi @MichaelMSheridan,
There are two ways to get the models from the scene: getSceneGraph and getNodeMap. If you’re looking for a model based on its name, I recommend using getNodeMap. It gives you a flat list, which is a lot easier to navigate than the scenegraph.
However, getNodeMap gives you a map, not an array. If you want to find an item from this map by its name, you can do this:

api.getNodeMap(function(err, nodes) {
    const helmet = Object.values(nodes).find(item => item.name === "Helmet")

This gives you the node with the name “Helmet”, or null if there isn’t any.
I actually cover this briefly in (shameless plug) my upcoming API Livestream: Introduction to the Sketchfab Viewer API with Klaas Nienhuis - YouTube

1 Like