Search code examples
javascriptthree.js3d

How to convert Three.js to .stl files for 3D printing?


I found one page link: Convert Three.js to .stl for 3D printing?

var exporter = new THREE.STLExporter();
var str = exporter.parse(scene);
console.log(str);

But when I'm using them, not export a stl file.

How can I do then?


Solution

  • The STLExporter.parse() will export the specified object to a string. If you wish to save the string as a file you have to perform some more operations. As a simple approach you can use FileSaver.js for saving the string to a file.

    First of all you have to download and include FileSaver.js in your code.

    Download link:

    After exporting the scene using STLExporter, convert the resulting string to a Blob and then save the Blob as a file using FileSaver.js,

    var exporter = new THREE.STLExporter();
    var str = exporter.parse( scene ); // Export the scene
    var blob = new Blob( [str], { type : 'text/plain' } ); // Generate Blob from the string
    saveAs( blob, 'file.stl' ); //Save the Blob to file.stl
    

    If you are not familiar with FileSaver.js you can try the following method,

    var exporter = new THREE.STLExporter();
    var str = exporter.parse( scene ); // Export the scene
    var blob = new Blob( [str], { type : 'text/plain' } ); // Generate Blob from the string
    //saveAs( blob, 'file.stl' ); //Save the Blob to file.stl
    
    //Following code will help you to save the file without FileSaver.js
    var link = document.createElement('a');
    link.style.display = 'none';
    document.body.appendChild(link);
    link.href = URL.createObjectURL(blob);
    link.download = 'Scene.stl';
    link.click();