Posts Tagged ‘blender’

Wednesday, January 27th, 2010

Tim Knip, a papervision core developer, has brought a pipeline improvement for users of Blender to import blender files directly into papervision and as3. This allows you to get at the blender objects, or blender DNA as it is called, that construct the 3d scene within Blender.

Unity3D has a great workflow that includes this where you can update your .blend file and then it updates in the Unity IDE, this work by Tim creates a similar workflow for Flash (recompile would be needed to show if embedded).

Typically exporters are made from the 3d IDE SDKs such as Blender using Python to export to COLLADA or other formats.  But here Tim is parsing the source file directly.  This also opens up the possibility to make other exporters from more simplified Flash AS3 code rather than learning a new IDE SDK just for an exporter.

I am not sure how much people want to embed .blend files with their applications as there is more information in the .blend file for the Blender app and it will add to the download.  But what this might do it inspire others to create simplified exporters from Tim’s work for Blender to COLLADA, 3ds and more that work well with papervision and flash 3d engines, directly in Flash.  So instead of learning each IDE to build an exporter that is the same, this solution could act as a proxy or middle man to simplify exporter creation, pretty much any Flash coder that understands 3d could build one from .blend files at a minimum.  If it was made as a higher level abstraction so the 3d software source could be swapped out it may open up simplified exporter tools a bit.  Since it is really just reading the binary data in the file, in theory other formats could do the same (3dsmax, Maya, Milkshape, etc).

There is a whole host of opportunities with this new tool! It is definitely nice to have this as I use Blender for Flash 3D and Unity 3D most often.  It will be interesting to see how this evolves.

Note from Tim on the tool:

I created a library to read Blender files (.blend) directly. So no
more headaches with broken exporters!

Grab the code here:
http://github.com/timknip/asblender/tree/papervision3d

Here’s a first example:
https://dl.dropbox.com/u/438592/blender/PapervisionTest.swf

And its code:
http://github.com/timknip/asblender/blob/papervision3d/src/PapervisionTest.as

ASBlender is simply a library which reads *everything* in a .blend file. So in theory you could grab materials, animations, armatures, the works… But its up to *you* to grab the relevant bits, since *all* the data is accessible.

Of course: this means you need to study the .blend format, see
http://wiki.github.com/timknip/asblender/ for more information.

Sample Code Snippet posted by Tim:

[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);
    }
  }
}
Sunday, June 14th, 2009

Rozengain or Dennis Ippel of AKQA updated probably one of the tools I use the most for flash 3d and that is the blender to as3 exporter.  This simplifies loading in the meshes you have and lessens the bulk of the COLLADA format.  COLLADA is great but flash is still client side and fairly memory intensive for 3d so loading in models directly to as3 is nice if flash is your presentation tool.

AS3 Blender exporter has been updated to allow multiple object export. Also in April is was updated to export quads and modifiers.

One concern you might have is statically binding the code within a main swf fileon compile and resulting file size compared to loading in the DAE dynamically.  But you can just load these in as you would external DAE COLLADA files as compiled swfs and since it is just code it is very compact.   This adds some duplication of code (such as tweening libraries or the 3d engine source as needed) but allows a more horizontal loading or lazy loading of meshes when needed.

This is just another option to get 3D models into the flash 3d engine of your choice in addition to COLLADA, some MD2 support and limited ASE support.

This is an awesome project that keeps getting better, thanks Rozengain.

Friday, September 26th, 2008

If you are exporting from blender to actionscript directly so you can get your models into flash as script you can use the Blender to .as3 exporter which so kindly supports all flash 3d engines currently Away3D, Papervision3D and Sandy3D.  Dennis Ippel made the Blender exporter a while back but the update supports papervision 2.0.

The benefits of COLLADA are nice but there are so many differences that you can run into trouble.  With the exporter it is a direct faces and vector export without all the bloat of DAE/COLLADA xml.  This works if you are only developing for flash and dont’ need to use the models in other platforms/systems/engines that aren’t in flash.