unable to access the object3D within the current scene

Currently facing an issue where I am unable to retrieve Object3D from the scene, despite the mesh objects being displayed within the scene. Strangely, the scene.children array does not reflect this.

Take a look at the screenshot here

Here is the code snippet:

function init(){
loadJSON(function(response) {
    // Parse JSON string into object
    var jsonData = JSON.parse(response);
    materialType = ['MeshBasicMaterial','MesLambertMaterial','MeshPhongMaterial', 'MeshStandardMaterial'];

    for (var i =0; i< jsonData.materials.length; i++){
        var matName = jsonData.materials[i].name;
        materialArray[matName] = new THREE.MeshLambertMaterial();
        materialArray[matName].name = matName;
    }
    var loader = new THREE.OBJLoader();

    for(var i = 0; i < jsonData.models.length;i++){
        parentGroup = new THREE.Object3D();
        parentGroup.name = jsonData.models[i].name;
        for(var j = 0; j < jsonData.models[i].children.length;j++){
            childName = jsonData.models[i].children[j].name;
            matType = jsonData.models[i].children[j].material;

            loader.load( "models/" + childName, makeHandler( childName , materialArray[matType],parentGroup), false ); 

        }
        scene.add(parentGroup);
    }
    });//loadJSON
    console.log(scene.children); //-- SCREENSHOT OUTPUT --

}//init function

// HANDLER FUNCTION
function makeHandler(meshName, material,parentObj) { 
    return function(geometry) { 
    object3d = new THREE.Mesh( geometry, material); 
    object3d.name = meshName;
    parentObj.add(object3d);
    };
}

Thank you for your assistance.

Answer №1

When using loader.load(), it's important to remember that it is an asynchronous function call, which is why a callback function is necessary.

In the specific scenario you mentioned, calling console.log( scene.children ) occurs before the models finish loading and are included in the scene.

This code snippet pertains to three.js version r.77

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with PHP Storm and Vue component not working? Or encountering issues with unresolved function or method in your

I have been struggling with this issue for days and cannot seem to find a solution online. Being new to Vue.js, I am currently working on a TDD Laravel project: My goal is to add the standard Vue example-component to my app.blade.php as follows: app.bla ...

Utilizing modular structure for efficient jQuery AJAX request

Seeking guidance on optimizing multiple GET ajax calls, where each function contains repeated $.ajax({}) code lines. Is it feasible to create a single $.ajax({}) function and utilize it in all functions instead of duplicating the code? Perhaps something ...

Is the length of a complex match in Angular JS ng-if and ng-repeat greater than a specified value?

In my code, there is an ng-repeat that generates a table based on one loop, and within each row, another cell is populated based on a different loop: <tbody> <tr ng-repeat="r in roles | limitTo: 30"> <td>{{r.name}}</td> ...

What could be causing Highchart to return the error message "Typeerror: undefined variable byte"?

My current project involves creating a graph using highchart and updating values every second. I am also working on displaying data in Mb, Kb, and Gb format on the graph by developing a function to convert byte values. Here is a snippet of my code: // hi ...

"Here's a simple guide to generating a random number within a specified range

I have encountered a specific issue: Within an 8-column grid, I am attempting to randomly place an item with a random span width. While I have successfully managed to position the item and give it a random width, I am struggling with adjusting the width b ...

Retrieve the file from Amazon S3 and download it

I'm having an issue with downloading a file from a directory outside of the root. Whenever I try, it defaults to looking in the root directory. I need users to be able to download these files on my site. The file was initially uploaded to Amazon S3 a ...

The Art of Mass Updating Embedded Documents in MongoDB

Struggling with updating an embedded document in MongoDB using Bulk updates on version 3.0. Any assistance would be greatly appreciated. ...

Creating packaging for a node-webkit application

https://github.com/rogerwang/node-webkit/wiki/How-to-package-and-distribute-your-apps When I was packaging my node-webkit application for Windows using the instructions provided in the above link, I encountered a challenge. I could not figure out how to p ...

When utilizing jade.compile for compiling a jade template containing mixins or included partials

I'm currently exploring solutions for this challenge. The issue at hand involves compiling a jade template for an email campaign, but the file I am attempting to compile is a mixin that includes some partials. Here's an example: controllers/us ...

"Utilizing FileReader to seamlessly integrate data into FormData without the risk

Currently, I'm in the process of constructing an ajax file uploader. This is made possible thanks to the recently introduced FormData interface. Everything seems to work as expected when using the original file. However, I encounter issues when conver ...

When utilizing DomSanitizer, Angular2 suddenly ceases to function properly

I've been working with Angular 2 and TypeScript. Everything was going well until I encountered an issue with my pipe, which is causing the DomSanitizer to interfere with the (click) event functionality. Even though the (click) code appears intact in ...

Passing parent props to child components in Vue.js - A comprehensive guide!

Trying to understand the process of passing a prop from parent to child components. If I include the prop attribute with the #id within the child component tag, like Image cid="488484-49544894-584854", it functions correctly. However, I am interested in u ...

Dynamically loading an iFrame source and automatically populating input forms using Javascript explained

My current challenge involves retrieving URL parameters successfully and using them to decide which iframe src to populate. Additionally, I need to autofill the form created via the form src with other parameters. However, I am faced with two main issues. ...

The semantic-ui-react searchable dropdown feature may not show all of the options from the API right away

For some reason, when I attempt to call an API in order to populate the options for semantic UI, only a portion of the options are displayed initially. To view the full list, I have to first click outside the dropdown (blur it) and then click inside it aga ...

The auto complete feature seems to be malfunctioning as an error message is being displayed stating that iElement.autocomplete is not

I am currently attempting to create an auto complete feature for a search text box using Angularjs. However, I am encountering an error stating that iElement.autocomplete is not a function. Here is the code snippet: <body ng-controller='Friend ...

Scrolling infinitely in every direction - both upwards and downwards

I'm currently working on a page that I want to have an endless scrolling effect both up and down. Right now, I am using jQuery to move content from the top of the page to the bottom. This gives a smooth loop when scrolling down, but I also want it to ...

Load Facebook video onto webpage using ajax technology

I have a database where I store the IDs of videos from Facebook, YouTube, and Vimeo. When I load any video via Ajax, Vimeo and YouTube videos load perfectly. However, Facebook videos do not load properly. The code obtained via Ajax includes a script requir ...

A guide on adding a personal library to Ember using npm

Despite the abundance of blog posts discussing it, I am still facing challenges in utilizing a custom library as a dependency for my ember application through npm. I have developed a WebGL library and successfully imported it into my Ember app by installi ...

jQuery: use the "data-target" attribute to toggle the display of a single content area

My code generally works, but there is an issue. When clicking on "Link 1" followed by "Link 2", only the content for Link 2 should be visible. How can I achieve this with my code? $("li").click(function() { $($(this).data("target")).toggle(); // ...

Is there a way to maintain the original indexing of a list after users have selected a filtered item in React using Webpack?

After implementing a filter (filteredCat) for my catalogue, the visual display works as expected. One issue I am facing is that even though the items are filtered, the logged index does not correspond to the new filtered list but instead reflects the inde ...