Getting vertex_positions and faces from Threejs BufferGeometry to use with Pyvista ( Python )

Hello, I Started recently playing around with Threejs and Pyvista.

Is there a way to get the vertex_positions and the faces so I can use them for mesh creation on Pyvista.

In my case, there is a selection of an area ( building ) that we’ll need to save, create the 3D visualisation and voxelize it ! but we want to do this on the back side ( using Python )

For the moment, I’ve been doing this to get the coordinates :

  • Getting the BufferGeometry from the selected box/area ;
    image

  • What I’m doing now to export the needed data so I can use it later :

 // Get the vertex positions from the geometry
    const positions = bufferBoxGeometry.attributes.position.array;

    // Get the vertex normals from the geometry (if available)
    const normals = bufferBoxGeometry.attributes.normal
      ? bufferBoxGeometry.attributes.normal.array
      : null;

    // Get the UV coordinates from the geometry (if available)
    const uvs = bufferBoxGeometry.attributes.uv
      ? bufferBoxGeometry.attributes.uv.array
      : null;

    // Get the triangle indices from the geometry
    const indices = bufferBoxGeometry.index
      ? bufferBoxGeometry.index.array
      : null;

    // Convert the vertex positions to a nested array of [x, y, z] coordinates
    const vertices = [];
    for (var i = 0; i < positions.length; i += 3) {
      vertices.push([positions[i], positions[i + 1], positions[i + 2]]);
    }

    // Convert the vertex normals to a nested array of [x, y, z] coordinates (if available)
    const normalsArray = [];
    if (normals) {
      for (var i = 0; i < normals.length; i += 3) {
        normalsArray.push([normals[i], normals[i + 1], normals[i + 2]]);
      }
    }

    // Convert the UV coordinates to a nested array of [u, v] coordinates (if available)
    const uvsArray = [];
    if (uvs) {
      for (var i = 0; i < uvs.length; i += 2) {
        uvsArray.push([uvs[i], uvs[i + 1]]);
      }
    }

    // Convert the triangle indices to a nested array of triangle indices (if available)
    const triangles = [];
    if (indices) {
      for (var i = 0; i < indices.length; i += 3) {
        triangles.push([indices[i], indices[i + 1], indices[i + 2]]);
      }
    }

    // Create an object that contains all the exported data
    const exportData = {
      vertices: vertices,
      normals: normalsArray,
      uvs: uvsArray,
      triangles: triangles,
    };

    const jsonStr = JSON.stringify(exportData);

Is there a CORRECT way or best way to do so, because for the moment I’m having issues getting this data and manipulate it using Pyvista in Python.

Thanks you in advance.