To export a specified object to a string, you can use the STLExporter.parse()
method. However, if you want to save this string as a file, additional steps are needed. One simple approach is to utilize FileSaver.js
for saving the string to a file.
Before proceeding, make sure to download and include FileSaver.js
in your code. You can find the download link here.
After using STLExporter to export the scene, convert the resulting string into a Blob and then save it as a file with 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 as file.stl
If you're unfamiliar with FileSaver.js, you can try the following alternative 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
// This code snippet helps 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();