Hello, Everyone. Hope you are doing well.
I have created some cubes and added info in their user data.
And then I would like to get mesh with this info.
I think I could get using this but it didn’t work.
let mesh = scene.getMeshesByUserdata();
Is there any solution for this?
Thanks
There is no such a method like you have used “getMeshesByUserdata”
Still you can find mesh using userdata.
You have to write that function using loop.
May be, you can use traverse function
anidivr
February 3, 2023, 12:21pm
3
When adding userData to a mesh, add it to an array while adding to the scene.
let mesh=[];
OBJLoader.load("models/common.obj",function(object){
while(object.children.length){
var name=object.children[0].name;
mesh[name]=object.children[0];
mesh[name].matrixAutoUpdate=false;
mesh[name].updateMatrixWorld=function(){};
scene.add(mesh[name]);
}
mesh["home"].geometry.computeBoundingBox();
mesh["mountain"].visible=false;
});
Fennec
February 3, 2023, 1:56pm
5
This is a generic solution you can tweak it to fit your exact use case.
const meshes = getMeshesByUserdata(scene, prop, value)
function getMeshesByUserdata(root, prop, value) {
const result = [];
root.traverse(object => {
if(object.userData
&& object.userData.hasOwnProperty(prop)
&& object.userData[prop] === value)
result.push(object);
});
return result;
}
1 Like
I’d just use the plain old-school vanilla scene.children.
filter
(...)
.