Skip to content

Instantly share code, notes, and snippets.

@manusolve
Created February 10, 2025 07:14
Show Gist options
  • Save manusolve/8a05c49d438905e4d3fe94bf4d6d2188 to your computer and use it in GitHub Desktop.
Save manusolve/8a05c49d438905e4d3fe94bf4d6d2188 to your computer and use it in GitHub Desktop.
extract stl from babylon.js -- one file per mesh
// paste this into the js console on firefox
// Step 1: Load the full Babylon.js (minified version) from a recent CDN
const script = document.createElement('script');
script.src = 'https://cdn.babylonjs.com/babylon.max.js'; // Use the latest minified version
script.onload = () => {
// Step 2: Load the serializers script for STL export
const serializerScript = document.createElement('script');
serializerScript.src = 'https://cdn.babylonjs.com/serializers/babylonjs.serializers.min.js'; // Load the serializers
serializerScript.onload = () => {
// Step 3: Use the existing scene object
if (typeof scene !== 'undefined') {
console.log("Scene found, exporting...");
// Check if STLExport is available
if (BABYLON.STLExport) {
// Iterate over each mesh in the scene
scene.meshes.forEach((mesh, index) => {
// Create STL data for the current mesh
const exportedData = BABYLON.STLExport.CreateSTL([mesh], true, `door_1_${index + 1}.stl`); // Pass the mesh as an array
/*
const blob = new Blob([exportedData], { type: 'application/sla' }); // Set the correct MIME type for STL
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = `exported_mesh_${index + 1}.stl`; // Set the desired file name
document.body.appendChild(link);
link.click(); // Trigger the download
document.body.removeChild(link); // Clean up
console.log(`Exported mesh ${index + 1} as STL.`);
*/
});
console.log("All meshes exported successfully.");
} else {
console.error("STLExport is not available in this version of Babylon.js.");
}
} else {
console.error("Scene is not defined in the current context.");
}
};
// Append the serializers script to the document to start loading
document.head.appendChild(serializerScript);
};
// Append the Babylon.js script to the document to start loading
document.head.appendChild(script);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment