Position of object

Hello
I asked this question previously on this forum:

I’m trying to find the position of objects inside my blender model. It’s a similar object and issue as the previous example, except with a glTF loader due to loading time. With this model, I can simply get the meshes position using mesh.position. However I have some objects that are groups of meshs like so:

However, now when I use the following code:

  mesh.geometry.computeBoundingBox();
  var center = new THREE.Vector3();
  mesh.geometry.boundingBox.getCenter(center);
  mesh.geometry.center();
  mesh.position.copy(center);

The position is set the centre. I can’t seem to find how I can return the position of the meshes inside the group… The information doesn’t appear to be in the geometry attribute or the position attibute. Any ideas on how I could return the position of the object?

Thanks in advance

The center of the bounding box of the geometry is in the object’s local coordinate system. You can convert it to a world position by using the object’s localToWorld method.

Object3D.position is a position in the parent object’s local coordinate system. You can convert it to a world position by using the parent object’s localToWorld method.

Make sure to apply localToWorld to a copy of the vector, because it outputs to the argument.

Hey, thanks.
Do you mean converting the mesh to a local to world? It doesn’t seem to have an affect. I have the following code:

  var light = this.getObject("l01"); // Retrieves Object3D in the scene
  light = light.children[0]; // To get the first mesh in the group

  light.geometry.computeBoundingBox(); 
  var center = new THREE.Vector3();
  light.geometry.boundingBox.getCenter(center);
  light.geometry.center();
  light.position.copy(center);
  var vec = new THREE.Vector3();

  light.localToWorld(vec);
        moonGlow.position.set(
    vec.x,
    vec.y,
    vec.z
  );

I have tried it without the bounding box code too.
Let me know,
Cheers

localToWorld converts the argument from local to world. In your code above, the argument is (0,0,0).

Try with:

var vec = new THREE.Vector3().copy(light.position);

Do I still compute the bounding box? And do I call localToWorld at the end?
I’m really struggling to work this out.

Sorry to have misled you.

var light = this.getObject("l01"); // Retrieves Object3D in the scene
light = light.children[0]; // To get the first mesh in the group
if ( ! light.geometry.boundingBox ) {
    light.geometry.computeBoundingBox();
}

//Assuming moonGlow is positioned globally, i.e. `scene.add( moonGlow )`:
light.localToWorld( light.geometry.boundingBox.getCenter( moonGlow.position ) );