– getObject by any custom property present in userData of Object
I want to get an Object by its custom property set un userData field … can i use Object.getObjectByProperty method for that ? if Yes then how ?
Example lets say my 3d Object has custom property called type under userData field … and using scene.getObjectByProperty method i want to get my Object …how can i do this ?
No, this method does not support this kind of deep selection. The following should work:
THREE.Object3D.prototype.getObjectByUserDataProperty = function ( name, value ) {
if ( this.userData[ name ] === value ) return this;
for ( var i = 0, l = this.children.length; i < l; i ++ ) {
var child = this.children[ i ];
var object = child.getObjectByUserDataProperty( name, value );
if ( object !== undefined ) {
return object;
}
}
return undefined;
}
1 Like
@Mugen87 I really like the approach! I do use the following too when we want a more functional approach and an array which sometimes is the case:
const getMeshByUserDataValue = (scene, name, value) => {
const meshes = [];
scene.traverse((node) => {
if (node.userData[name] === value) {
meshes.push(node);
}
});
return meshes;
};
1 Like