Is there a way to retrieve an object in Three.js without directly placing it in a scene? (FBXLoader)

When I invoke this JavaScript function, it successfully adds the object to the scene:

var loader = new THREE.FBXLoader();

function returnFBX(PATH, scene) {
    loader.load('obj/' + PATH + '.fbx', function (object) {
        scene.add(object);
    });
}

However, if I change:

scene.add(object);

to:

return object;

It appears to return undefined.

I attempted using Promises and the Loading Manager, but both only managed to add the object to the scene rather than returning it as expected.

The issue may stem from having two nested functions and the asynchronous nature of loading. Yet, I am unsure how to resolve this given that this is a common method for loading objects.

Answer №1

load operates asynchronously - you can utilize a callback function or employ await, like so:

const afterload = obj => { console.log(obj); }

function retrieveFBX(filePATH, scene) {
    loader.load('obj/' + filePATH + '.fbx', function (obj) {
        afterload(obj);
    });
}

alternatively

async function retrieveFBX(filePATH, scene) {
    return loader.load('obj/' + filePATH + '.fbx', function (object) {
        return object;
    });
}

let loadedObj = await retrieveFBX( ... )

Answer №2

This is the method I used by utilizing loadAsync()

async function LoadModel (...) {
  const response = await loader.loadAsync(...);
  return response;
}

const modelInstance = await LoadModel(...);

See how you can appropriately handle object loading time: here

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

SapUI5: Implementing a toggle functionality to display/hide one list item based on another list item's action

UI5 is a versatile framework with numerous possibilities, but sometimes I find myself struggling to implement ideas that would be easier in traditional HTML. Here's the scenario: I want to create a List with ListItems that display cities like Berlin, ...

A guide on tapping into the creation event of an express session

Is there a way to determine when express creates a new session? Specifically, I am utilizing a mongodb session store. I have been encountering an problem related to multiple sessions being generated and I would like to identify the root cause by monitorin ...

Steps for embedding a font in a .pptx file

While working on creating a .pptx file using ASPOSE.Slides, I encountered some issues with embedding fonts. As an alternative option, I am looking for suggestions on how to embed custom fonts in a .pptx file using Apache POI or other methods. If you have ...

Validate the array with AJAX and display an error message

Need assistance validating arrays using FormRequest validation. The error message for the 'name' field can be accessed as data.responseJSON.error.name[0] and displayed to the user. error: function(data, xhr, errmsg, err){ console.log(" ...

React: Updating a property in an array of objects causes properties to become undefined

My intention was simply to update a property within an object inside an array and then update the state of the array. However, I encountered an issue where all properties except the one that was updated became undefined. The code in question is as follows ...

What is the process for creating a local repository for Node.js npm?

When it comes to the building process in node js, there are a few goals that need to be called: Begin by calling npm install, which creates a folder called node_modules and places all dependencies from package.json into it. [for UI development] Execute a ...

What methods does Enzyme have for determining the visibility of components?

I am facing an issue with a Checkbox component in my project. I have implemented a simple functionality to hide the checkbox by setting its opacity : 0 based on certain conditions within the containing component (MyCheckbox) MyCheckBox.js import React fr ...

Discovering necessary information by iterating through JSON

Being new to vue js, my goal is to loop through the provided JSON data and check if the required data is present within the file. Here is a snippet of the JSON: [ { "id": "text-5", "widget": "hello", "params": { "0": "section-right", ...

Data not being saved when using the Post method in a Web API

I built a straightforward Web API application that allows users to GET or POST data. The data consists of a simple array of Strings like ["foo", "bar"]. However, when I try to send data using the POST method to the Web API and then make another call to ret ...

What is the process for dynamically populating a select dropdown based on the selection made in another select dropdown?

I need to dynamically populate the second select box based on the option selected in the first select box. Here's what I have tried so far, but it doesn't seem to be working as expected. HTML: <form id="step1"> <p> Creat ...

The CSS styling for a pie chart does not seem to be functioning properly when using jQuery's

https://i.stack.imgur.com/kEAKC.png https://i.stack.imgur.com/03tHg.png After examining the two images above, it appears that the CSS is not functioning properly when I try to append the same HTML code using JavaScript. Below is the snippet of my HTML co ...

The GeoChart zoomOut button is not visible at this time

I am currently exploring the geochart API, which is a relatively new one with limited information available. <!DOCTYPE html> <html> <head> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></scr ...

Unexpected panorama rendering using Three.js and canvas

I'm having some trouble with the canvas renderer in three.js. I tried using the panorama example with my own images, but the geometry isn't looking right. I've experimented with different parameters like texture canvas size and box size, but ...

Display a single specific outcome within a React component's list

import {useState, useEffect } from 'react' import axios from 'axios' const Singlecountry = ({searchedCountries, setWeather, weather}) => { const weatherName = searchedCountries[0].capital console.log(weather) useEffect(() =&g ...

What steps should I take to determine the value of a function when a specific x value is

I have been searching through various resources for assistance, yet I haven't come across a solution to what seems like a simple issue. I've created a function funB <- function(x) (0.8042851 + ((3.9417843-0.8042851)/(1+((x/0.4039609)^(- ...

Move the image inside the box without directly following the mouse cursor, but at a slightly faster pace

I am facing an issue with a Vue component that allows me to zoom in on an image and move it around the container. The problem is, when the image is zoomed in, it moves faster than the mouse due to using the scale transform. Additionally, I noticed that cl ...

Having issues with array.push functionality in JavaScript

let chatConversations = new Array(); jQuery('.CChatWindow').each(function(){ if (jQuery(this).is(":visible") && jQuery(this).attr("data-conversationid") != 0) { alert(jQuery(this).attr("data-conversationid")); // returns 1 and ...

Numerous web worker asynchronous requests are being made, but not all are coming back

Within my web worker script, I am utilizing the following code: self.addEventListener('message', function(e){ try { var xhr=new XMLHttpRequest() for(var i = 0; i < e.data.urls.length;i++){ xhr.open('GET&apos ...

The value of innerHTML is currently "undefined"

I am facing a new challenge while working with PHP. I need to edit the content of a div using the product ID fetched from the database. I am trying to accomplish this by iterating through two foreach loops to get the correct IDs separately. The goal is to ...

Accessing and displaying all states in $stateProvider using AngularJS and ui-router

Here's a simple question: how can I find all instances of $stateProvider.state in my app.js, which is my AngularJS config file? In the past with ngRoute, I could achieve this by using a similar approach in my .run() block: .run(function ($route) { ...