Retrieve Object3D in three.js based on a custom property value

In my coding project, I have been creating instances of LineSegments in the following manner:

const geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));

const edges = new THREE.EdgesGeometry(geometry);
const line = new THREE.LineSegments(edges2, new THREE.LineBasicMaterial({ color: 0xffffff }));
scene.add(line);

After that, I went ahead and added some custom properties to the lines like this:

line.prop1 = "hello";
line.prop2 = "world";

Now I'm wondering how I can retrieve all objects with a specific property value, for example, finding all objects where prop1 is set to "hello"?

Answer №1

Unfortunately, there is no built-in method available for this specific scenario. The Object3D.getObjectByProperty() function is limited to objects with a unique property value. To solve this issue, you can use the following custom function:

function findObjectsWithProperty(object, property, value, result = []) {

    // Check if the current object has the specified property and value
    if (object[property] === value) {
        result.push(object);
    }

    // Check children recursively
    for (let i = 0, l = object.children.length; i < l; i++) {
        const child = object.children[i];
        findObjectsWithProperty(child, property, value, result);
    }

    return result;
}

//

const objectsMatchingProperty = findObjectsWithProperty(scene, 'property1', 'test');

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

Transitioning from the older version of meteor 1.8.3 to the latest version 2.6

I'm working with an older project that is currently using version 1.8.3 of a software that supports MongoDB 5 in Meteor 2.6. I have reviewed the changelog and migration guide and now I have a question: Should I upgrade directly from 1.8.3 to 2.6, or i ...

Arrange divs on the screen in a specific formation

I am exploring ways to position specific divs in the middle of the screen to create a pearl necklace-like shape. The desired shape is as follows: 0 0 0 0 0 0 0 0 0 0 My aim is to achieve this using jQuery on a mobile ...

When the caret triangle is upside down, it indicates that a drop-down menu is available even when the

I am facing an issue with a dropdown list where the triangle indicator is incorrectly displayed: https://i.stack.imgur.com/L4NBW.png Both images show that the arrows are in reverse direction, and I am struggling to identify the cause of this problem. He ...

What could be causing the malfunction of useEffect() in my script?

const [isOpen, setIsOpen] = useState(false); useEffect(() => { if (!token) { return <Navigate to="/auth/login"/> } getMe(token) }, [token, getMe]) return ( <RootStyle> <DashboardNavbar onOpenSi ...

Executing a personalized function - settings.func does not exist as a valid function

I have developed a custom jQuery plugin where I intend to invoke a specific function like this... (function($) { $.fn.customPlugin= function(options) { var settings = { func: null }; if (options) { $. ...

My data is not appearing with ng-repeat or ng-bind

One issue I am encountering is that my ng-repeat / ng-bind is not displaying the data within $scope.articles, even though I am getting the expected data in the console. To help identify the problem more easily, I have created a code snippet below: var A ...

Data can be retrieved in a React/Next.js application when a button is clicked, even if the button is located in a separate

Whenever the button is clicked, my function fetches weather data for the current location. I am trying to figure out how to transfer this data from the Location component to the pages/index.tsx. This is where another component will display the data. This ...

Experiencing a 400 error while transitioning from ajax to $http?

I am facing an issue while trying to make a GET request using $http instead of ajax. The call functions perfectly with ajax, but when attempting the same with $http, I encounter a 400 (Bad Request) error. I have referenced the Angular documentation and bel ...

What is the best way to dynamically insert an object into a field name in react-final-form?

When using the react-final-form component, you can expect the following result: <Field name="answers[0].name" component="input" type="radio" value="0" /> { answers: [ { name: 'value' } ] ...

Choosing the perfect item with the help of a material's GridList

I've developed a react-mobx application using Material-UI, and the code structure is similar to this: render() { // defining some constants return ( <div> <img src={selectedPhoto} alt={'image title'} /> < ...

Loading Embedded Content with ReactJS

Currently, I am developing a one-page website with ReactJS. Each section of the site is created as individual React components, which are displayed conditionally based on the user's tab selection in the navigation bar. As part of my design, I have in ...

Stop materializecss dropdown from closing when clicking within it

As I work on my current project, I am utilizing Materialize.css which includes a dropdown with various input forms inside. The dropdown can be closed by: clicking outside of .dropdown-content clicking inside of .dropdown-content clicking on .dropdown-bu ...

Exporting Blender's Particle System Hair to Three.JS

Having spent numerous months delving into the world of three.js while creating a model in Blender 2.7, I have been unsuccessful in finding a way to export the blender particle system hair. Does anyone have any suggestions on how to accomplish this task? ...

Prevent the use of harmful language by implementing jQuery or JavaScript

Is there a way for me to filter certain words (such as "sex") using regex, even when people use variations like "b a d", "b.a.d", or "b/a/d"? How can I prevent these kinds of words from getting through? I'm trying to filter more than just one word - ...

We apologize, but the module you are looking for cannot be found: Unable to locate 'fs'

Trying to create a new MDX blog website using Next.js 13 with the latest app router, but encountering an error message saying "Module not found: Can't resolve 'fs'". It was working fine in Next.js 12 and pages directory, but not in the lates ...

Refreshing a jsp page without the need to reload the content

On my jsp page, I am displaying the contents of a constantly changing table. This means that users have to refresh the page every time they want to see updated information. Is there a way for me to update the content dynamically without requiring users t ...

Prevent draggable functionality of jQuery UI on elements with a specific class

I have created a dynamic cart feature where users can drag and drop items into the cart. However, once an item is placed in the cart, it should no longer be draggable (though still visible but faded). I attempted to achieve this by using the following code ...

Can someone explain the meaning of the paragraph in the "Index Routes" section of the React-Router documentation?

Lesson 8 of the React-Router tutorial delves into the concept of "Index Routes", outlining the importance of structuring routes in a specific way. Here are some key points from their discussion: The tutorial suggests that while setting up the initial rout ...

AngularJS Resource GET Request Unsuccessful

Is there a way to verify if a resource failed to be fetched in AngularJS? For instance: //this is valid syntax $scope.word = Word.get({ id : $routeParams.id },function() { //this is valid, but won't be triggered if the HTTP response is 404 or an ...

Images are failing to load in Ionic 3

Currently working on developing an Ionic application and troubleshooting the use of the camera native plugin. The script functions flawlessly in a fresh project, but encounters issues within the current project environment. Initially suspected a problem w ...