Jump to content

Custom 3d File Types


Pryme8
 Share

Recommended Posts

SO, I ran into the problem where I was needing just the positions of an null object and its name, none of its material data none of its vertices nothing just positions, to serve as a template file.

Initially I was just going to grab this information from a the obj file when I realized it does not store global location matrices... so the natural thought then was to use an fbx when I realized that has way way more information then I need and is not directly parse-able without some prior handling, and if Im gonna parse it why not make it into a format that I want.  Hence the creation of a bt file (block template in my project I made it for or a Babylon Template for general purposes).

With this new file I am able to iterate through it after grabbing it and turning it into a JSON object to create Instances or clones of prefabs stored in a container on the scene.
The structure of my 3d file was a null object with the name Placeholder(nameOFpreset).# and the position and location of the null to where I want the preset (I initially positioned them with the presets mesh in the null orientated to the null and then once everything was in place deleted all the children of the placeholders).  Once it exported from c4d to an fbx as a text file it changes the names to MODEL::Placeholder(name)__#.  So following that naming convention and a little nifty regex I was able to accommodate my means, and get it fully functioning.  I figured someone might find this useful in one of their projects.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FBX to BT</title>

</head>
<body>
<input id='input' type="file" accept="fbx" />
<a id="downloadAnchorElem" style="display:none"></a>
<script>

document.getElementById('input').addEventListener('change', function(e){
	 var reader = new FileReader();
  	reader.onload = function(progressEvent){
   	 // Entire file
    	var response = this.result;
		console.log(response);
		var parsed = [];
		var objectReg = /(?:(?:Model:.*"Model::)(.*)(?:(?=",))(?:.*(?:\n|\r)){4})(?:.*P:\s"Lcl(.*)(?:\n|\r))(?:.*P:\s"Lcl(.*)(?:\n|\r))?(?:.*P:\s"Lcl(.*)(?:\n|\r))?/ig;
		var placeholders;
		
		while ((placeholders =  objectReg.exec(response)) !== null) {
  			
			var position = [0,0,0];
			var rotation = [0,0,0];
			var scale = [1,1,1];
			//console.log(placeholders);
			if(placeholders[2]){
				placeholders[2] = placeholders[2].split(',');
				position[0] = parseFloat(placeholders[2][4]);
				position[1] = parseFloat(placeholders[2][5]);
				position[2] = parseFloat(placeholders[2][6]);
				//console.log("position:");
				//console.log(position);
			
			}
			if(placeholders[3]){
				placeholders[3] = placeholders[3].split(',');
				rotation[0] = parseFloat(placeholders[3][4])/180;
				rotation[1] = parseFloat(placeholders[3][5])/180;
				rotation[2] = parseFloat(placeholders[3][6])/180;
				//console.log("rotation:");
				//console.log(rotation);
			}
			if(placeholders[4]){
				placeholders[4] = placeholders[4].split(',');
				scale[0] = parseFloat(placeholders[4][4]);
				scale[1] = parseFloat(placeholders[4][5]);
				scale[2] = parseFloat(placeholders[4][6]);
			//	console.log("scale:");
			//	console.log(scale);
			}
			if(rotation[0]==-0){
				rotation[0] = 0;	
			}
			if(rotation[1]==-0){
				rotation[1] = 0;	
			}
			if(rotation[2]==-0){
				rotation[2] = 0;	
			}
			
			
			console.log((placeholders[1].replace(/Placeholder_/g,'')).replace(/(__\d*)|(_(\n|\r))/g,''));
			
			parsed.push({
				name:(placeholders[1].replace(/Placeholder_/g,'')).replace(/(__\d*)|(_(\n|\r))/g,''),
				position : position,
				rotation : rotation,
				scale : scale,
				});
				
				
			
			}
			
		var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(parsed));
		var dlAnchorElem = document.getElementById('downloadAnchorElem');
		
		dlAnchorElem.setAttribute("href",     dataStr     );
		dlAnchorElem.setAttribute("download", (e.target.files[0].name).replace('.fbx', '')+".bt");
		dlAnchorElem.click();
				
  	};

  reader.readAsText(e.target.files[0]);
	
}, false);

</script>
</body>
</html>

Much love from the Monkey King ^_^...

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...