ASBlender is an Actionscript 3 library to read Blender .blend files.
See http://www.blender.org/development/architecture/blender-file-format/ for an explanation of the .blend format.
The complete .blend is read into simple AS3 Object's which map to the structures defined in the so-called DNA.
Disadvantage of this approach is that you have to "know" how the structures look like. => check "printDNA" in USAGE below
[Embed (source="/assets/crystal_cube.blend", mimeType="application/octet-stream")] public var BlenderData:Class;
var blend:BlendFile = new BlendFile();
blend.read(new BlenderData());
if (blend.scenes.length) { // Blender can have multiple scenes, don't know yet how to grab the "active" scene. buildScene(blend.scenes[0]); }
/**
Prints out the DNA as contained in the .blend */ private function printDNA(blend:BlendFile):void { var struct:DNAStruct; var field:DNAField;
for each (struct in blend.dna.structs) { var type:String = blend.dna.types[ struct.type ];
trace(type);
for each (field in struct.fields) {
trace(field.type + " " + field.name);
}
} }
private function buildScene(scene:Object):void {
var obj:Object = scene.base.first;
while (obj) {
// grab the Blender Object.
// The Blender Object defines rotation, scale, translation etc.
var object:Object = obj.object;
trace("Object name: " + object.id.name + " type: " + object.type + " matrix: " + object.obmat);
//for (var key:String in object) {
// trace(key);
//}
if (object.data) {
switch (object.type) {
case 1: // Mesh
trace (" -> Mesh: " + object.data.id.name);
buildMesh(object.data);
break;
case 10: // Lamp
trace (" -> Lamp: " + object.data.id.name);
break;
case 11: // Camera
trace (" -> Camera: " + object.data.id.name);
break;
default:
break;
}
}
obj = obj.next;
}
}
private function buildMesh(mesh:Object):void { var numVertices:int = mesh.totvert; var numFaces:int = mesh.totface; var i:int;
trace(" -> #verts : " + numVertices);
for (i = 0; i < numVertices; i++) {
var v:Object = mesh.mvert[i];
var x:Number = v.co[0];
var y:Number = v.co[1];
var z:Number = v.co[2];
trace(" -> -> vertex: " + x + " " + y + " " + z);
}
trace(" -> #faces : " + numFaces);
for (i = 0; i < numFaces; i++) {
var f:Object = mesh.mface[i];
var v1:int = f.v1;
var v2:int = f.v2;
var v3:int = f.v3;
var v4:int = f.v4;
trace(" -> -> indices: " + v1 + " " + v2 + " " + v3 + " " + v4);
if (mesh.mtface) {
// UV coords are defined
var tf:Object = mesh.mtface[i];
trace(" -> -> -> uv: " + tf.uv);
}
}
}