How can I identify and access a specific object loaded from a .obj file in Three JS?

There is a function in my code that loads objects from .obj files in a specific way:

function loadObject(obj, mtl, clr, opc, px, py, pz, rx, ry, rz, cs, rs, name) {
    switch(mtl) {
        case 'transparent':
            var material = new THREE.MeshLambertMaterial({
                color: clr,
                transparent: true,
                opacity: opc,
            });
            break;
        case 'web':
            var material = createElementMaterial('img/web.png');
            break;
        case 'basic':
            var material = new THREE.MeshBasicMaterial({ color: clr });
            break;
        default:
            var material = new THREE.MeshLambertMaterial({color: clr, transparent: true, opacity: opc});
            break;
    }
    var objLoader = new THREE.OBJLoader();
    objLoader.load(obj, function(mesh) {
        globalobject = mesh;
        globalobject.name = name;
        mesh.traverse(function(node) {
            if(node instanceof THREE.Mesh) {
                node.castShadow = cs;
                node.receiveShadow = rs;
                node.material = material;
                node.position.x = px;
                node.position.y = py;
                node.position.z = pz;
                node.rotation.x = rx;
                node.rotation.y = ry;
                node.rotation.z = rz;           }
        });
        scene.add(mesh);
    });
}

Now, I want to assign a unique name to each loaded object and be able to control its properties like opacity using getObjectByName(). When I load an object and try to access it by name, the console returns undefined. However, when I directly query the console, I can print the object successfully. Therefore, I am looking for a solution to individually access each object loaded from .obj files.

Answer №1

const element = document.getElementById( "elementId" );

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

Deactivate the Windows button and prevent the use of Alt + E to type in

I am looking to develop a web application called "QUIZ". In this application, I want users to be able to navigate using only the mouse as I have disabled all keys except for the windows, alt, 'num lock', and caps button. I tried to disable the wi ...

find total sum of elements outside ng-repeat loop

I am currently working on a calculation for the total outside of an ng-repeat, which is influenced by ng-change within the ng-repeat. Here is a preview of my table layout: <table> <tr> <td>Total - the total is {{tot}}</td ...

Setting up multiple RabbitMQ or other backend servers within Node configurations

As someone working in DevOps, I have encountered a situation where our developers are claiming that the Node.js software they wrote can only point to a single backend server due to Node.js limitations. This assertion seems unbelievable to me. How is it eve ...

What is the best way to display the identical HTML page for both online and offline applications?

I am facing a challenge with my application, as I need to ensure that it can also be accessed offline. While I can easily create dynamic content on the client side using Javascript and JQuery instead of relying on server-side processing, I am struggling wi ...

What is the best way to store user input in local storage using Vue.js?

Following the official Vue.js site, I have been trying to implement Framework 7. However, when using an input field, [object InputEvent] is displayed both when typing text and attempting to save it. Is there a way to store a name in local storage and have ...

Tips for managing unexpected TCP disconnects?

Using Node.js, I set up both a TCP server and an HTTP server. The TCP server was intended to connect with hardware devices via TCP connection. I have 100 TCP clients that are maintaining their connections with the server. Normally, when a TCP client disc ...

Is there a method to determine if localForage/indexedDb is currently handling data operations?

Currently, I am working on a webapp that utilizes async localForage (specifically angular localForage). My goal is to alert users if they attempt to close the browser window while there are ongoing localForage operations. I have observed that some browser ...

Creating dynamic TextBox fields in JSP based on the selected option from a dropdown menu fetched from the database

Hello, I need some assistance in creating dependent Textboxes based on Dropdown values fetched from a database using Jsp. The code provided below is working fine for one Textbox, but I am unsure how to extend it for multiple dependent Textboxes. Any help w ...

Does the 'a' HTML tag secretly trigger window.location.assign?

Being relatively new to the online world, I have a simple question in mind. Imagine that I have a very basic a tag in my HTML: <a href="www.google.com"> Click me </a> What actually happens when I click on this link? Does the browser simply ...

What strategies would you use to put in place conditional imports in a way that is reminiscent of ReactNative

Is there a way to implement conditional imports in other projects similar to how React Native imports Android or iOS specific classes using *.android.js and *.ios.js? If I wanted to have different classes for development and production purposes, could I u ...

Avoid displaying incorrect UI states while data is being loaded

Within my Vue application version 2.6.X, I utilize single-file components similar to the following structure: <template> <div> <div v-if="users.length"> <!-- users list rendering here --> </div> & ...

Generating a .html file through the use of a Chrome extension

I am currently working on my first Chrome extension. At the moment, my code is capable of extracting elements from a webpage and generating HTML markup, which is then stored in a JavaScript string. Within my extension, there is a button that... $(".colum ...

Is the Browser not Opening when Running npm start with webpack Due to a Peer Dependency Issue with [email protected] and webpack@^4.0.0?

I just started learning JavaScript and decided to explore the webpack-starter project on GitHub (https://github.com/wbkd/webpack-starter). Following the instructions, I cloned the repository and ran npm install in the project folder. Upon encountering a lo ...

When submitting the form, set the value to "null" if the input field is left empty

I am new to the form validation process and need help returning a value of null if the input value is an empty string upon form submission. In the example below, there are three options for verifying a user's identity. Depending on the selected radio ...

Capturing input and select values dynamically in JavaScript without explicitly targeting them

I'm currently working on a project that involves looping through a set of list items containing input and select boxes. My goal is to extract the values from these elements, perform a calculation based on those values, and store the results in an arra ...

Challenges when Accessing JSON Documents

Currently, I'm working on a webpage project where I needed to access a JSON file in my JavaScript code. Here's the script I wrote for that purpose: function xhrSuccess () { this.callback.apply(this, this.arguments); } functi ...

Having trouble getting autocomplete to work with JQuery UI?

Currently facing issues in implementing the Amazon and Wikipedia Autocomplete API. It seems that a different autocomplete service needs to be used based on the search parameter. Unfortunately, neither of the services work when adding "?search=5" for Wikipe ...

Having trouble retrieving JSON data from an external source

I'm currently facing a challenge where I am unable to fetch JSON data from a webpage despite multiple attempts. I keep encountering the following errors: Uncaught SyntaxError: Unexpected token : or XMLHttpRequest cannot load URL. No 'Access-Co ...

How to load a HTML page dynamically with pure vanilla JavaScript utilizing AJAX

On a main page, there are various links that, when clicked, are supposed to load the contents of each link inside a specific div tag on the main page. For example: MainPage.htm <html> ... <body> ... <div id="MainContent"> ...

Capacitor's push notification feature and websocket support for enhancing chat app functionality

I'm struggling to integrate push notifications with WebSockets effectively. My goal is to display incoming messages as push notifications, similar to how WhatsApp handles chat messages. For detailed guidance on implementing push notifications, check ...