Jump to content

Reading .babylon file to find all "connected" image files


joshcamas
 Share

Recommended Posts

Ello people of the world!

 

On my editor I'm working on a mesh database system thingy. So how it works is you drag your babylon files into a folder, and then the editor will automatically categorize everything into separate folders.

 

Problem is, after grabbing the babylon file, how would I read it and know what image files go with what babylon file, and so on??

 

Thanks!

Link to comment
Share on other sites

OK, so it turns out it wasn't that hard at all!!!
 
Here's the code: (Just thrown together)
 

function scanBabylonImages(dir) {    var attachedImages = [];    $.ajax({        url: dir,        async: true,        success: function(data) {            onComplete(data)        },        error: function(data,error) {            console.error(error);        }    });    function onComplete(data) {        //Go through the babylon file's materials. Find any texture images        var data = JSON.parse(data)        data.materials.forEach(function(mat,m) {            var toScan = ["diffuseTexture","ambientTexture","opacityTexture","reflectionTexture","emissiveTexture","specularTexture","bumpTexture"]            toScan.forEach(function(texture,t) {                if(mat[texture] != null) {                    attachedImages.push(mat[texture].name)                }            });        })        //Clean any duplicates        finalAttachedImages = [];        for(var i = 0, n = attachedImages.length; i < n; i++) {            for(var x = 0, y = finalAttachedImages.length; x < y; x++) {                if(finalAttachedImages[x]==attachedImages[i]) {                    continue;                }            }            finalAttachedImages[finalAttachedImages.length] = attachedImages[i];        }        console.log(finalAttachedImages);    }}

 

Basically it first loads the file using jquery, then it finds any texture files, and then gets rid of any duplicates. Then it logs what it finds.

If anyone wants to use it, feel free to do whatever. :) :)

Link to comment
Share on other sites

When reading a file babylon he sufit to go see the given material:

 

mesh.material.diffuseTexture.url

or

mesh.material.subMaterials[1].diffuseTexture.url

 

this gives you the path of the image and the image that the model use.

Link to comment
Share on other sites

Ahhhh ok. Is this how that would be done?

 

function scanBabylonImages(dir) {	var attachedImages = [];	$.ajax({		url: dir,		async: true,		success: function(data) {			onComplete(data)		},		error: function(data,error) {			console.error(error);		}	});	function onComplete(data) {		//Go through the babylon file's materials. Find any texture images		var data = JSON.parse(data)		data.materials.forEach(function(mat,m) {			var toScan = ["diffuseTexture","ambientTexture","opacityTexture","reflectionTexture","emissiveTexture","specularTexture","bumpTexture"]			toScan.forEach(function(texture,t) {				if(mat[texture] != null) {					attachedImages.push(mat[texture].name)				}			});						if(mat.subMaterials != null && mat.subMaterials.length != 0) {				mat.subMaterials.forEach(function(texture,t) {					if(mat.subMaterials[texture] != null) {						attachedImages.push(mat[texture].name)					}				});			}		})		//Clean any duplicates		finalAttachedImages = [];		for(var i = 0, n = attachedImages.length; i < n; i++) {			for(var x = 0, y = finalAttachedImages.length; x < y; x++) {				if(finalAttachedImages[x]==attachedImages[i]) {					continue;				}			}			finalAttachedImages[finalAttachedImages.length] = attachedImages[i];		}		console.log(finalAttachedImages)	}}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...