Exporting Scene as STL mutates the scene?

hey there, i am trying to export a babylonjs scene as stl / gltf. i basically works by:

STLExport.CreateSTL(scene.meshes as Mesh[], true, fileName, true, true, true)
But: there are two Problems:

  1. it seems to mutate the original scene! → at least after using the function, meshes get copied all over the place.
  2. it seems to not export the meshes with its global positions/rotations → some of the exported meshes are rotated, some are at an other position.

whats a quick way around this? i just want to save the whole scene as i see it in the viewport as stl / obj / gltf…

Cheers and thank you so much!

PS.: is this the Problem? scene.meshes as Mesh[],? scene.meshes is a AbstractMesh :confused:

Adding @bghgary

bake the transforms into each mesh before exporting the whole thing.

STL do not have transformation matrices and are just vertex arrays that have the positions.

I wrote the exporter a long while ago and forget if I had it take the transformations into account or not.

but quick and dirty would be

meshes.forEach((m)=>{
m.bakeCurrentTransformIntoVertices()
})

then do your stl export

2 Likes

Hi,

i am doing it like this now:

        const exportMeshes = scene.meshes
          .map(mesh => {
            if (mesh.id !== 'filterbla' && mesh.id !== 'filterbla' && mesh.id !== 'filterbla') {
              return
            }
            return new Mesh('exportSTLMesh', scene, null, mesh as Mesh, true)
               .flipFaces()
               .bakeCurrentTransformIntoVertices()
          })
          .filter(x => x) as Mesh[]

        STLExport.CreateSTL(exportMeshes, true, fileName, true, true, true)
        exportMeshes.map(mesh => mesh?.dispose())

But it still mutates the original mesh (the Meshes in the viewport get changed (flipFaces) + when i open the STL (in rhino for example) it is flipped.
Why?

Cheers and thanks!

EDIT: another try - with the same result → scene.meshes get mutated. But why, even with cloning the whole thing!!!

        const rootMesh = new Mesh('rootMesh')
        const scenarioMesh = scene.meshes
          .filter(mesh => mesh.id == 'bla')
          .map(mesh => {
            const clonedMesh = new Mesh('clonedMesh', scene, rootMesh, mesh as Mesh)
            return clonedMesh
          })

        scenarioMesh.map(bla => bla.flipFaces())

        STLExport.CreateSTL([...scenarioMesh], true, fileName)

I don’t know Rhino but it sounds like it might have a different handiness. Can you place them all into a parent transform and set its z scaling to -1?

1 Like