Exporting Models From Blender to Flash Sandy 3D, Experiments in 3D

Last week, reader rafbaf asked how to use Blender models in Sandy 3D. There are a few options, for instance Sandy 3D can import the Collada format that Blender exports. But there is also an easier way. A Python script for Blender allows you to export models straight to ActionScript code. However, as you will see in this post, it’s not as straightforward as it seems.

downloadExample FlashDevelop project.

There’s a reason this post is titled “experiments”. It is actually an unfinished work, but I wanted to give you a look at what I’ve already been able to create. This is how far I got for now:

  1. First, I got an A-Wing from the blendermodels archive. Before I got the first render result shown in the picture above, I had to fix the normals of most of the meshes in there (in Blender: go into edit mode, select vertices and ctrl-n)
  2. Next, get the export script from Dennis’s page and install it. Take care to get the correct directory, it’s not that easy to find. If you have one of the latest versions of Blender, you should look in your user folder. For Windows that’s “C:Documents and SettingsApplication DataBlender FoundationBlender.blenderscripts”.
  3. If you haven’t already done so, you also need to have Python installed. Python is necessary to execute the script.
  4. Once you got this out of the way, you will need to restart Blender. Check the file > export menu and verify that the ActionScript option is available. If it’s not, you probably placed the AS3Export.py file in the wrong directory.
  5. Select the object you want to export and click the export to ActionScript menu item. It’s probably a good idea to enter a package name, as this will keep your code cleaner.

Now you should have one or more .as files: one for every mesh and texture in your model. This model doesn’t come with textures, so there are none.

Next we are going to use those files in a FlashDevelop project. The first step is to create a new project in FlashDevelop and put the Sandy 3D library inside the src folder. Like described in previous blog posts.

Now inside the src directory, you should create a directory with the same name as the package you specified in step 5 above. If you forgot the package name or are not sure, you can open one of the files that was created and check the first line.

That’s pretty much it: add the meshes as Shape3D objects to your scene, just like you would add a cube or other shape:

scene.root.addChild( new Mesh() );

(you might want to add an appearance too)

You’ll notice I’m skipping a few steps here, but they are all very basic. If you download the example project and read through my previous blog posts, everything should be clear (feel free to comment if it isn’t).

You’ll notice a few issues/problems (correct me if I’m wrong):

  • The 3D object I choose, consists of several meshes. Each with their own texture (material in Sandy3D terminology). The export plugin doesn’t really help you on this point. You need to create every mesh and add them individually to the scene.
  • There seems to be an issue with the normal calculations. I did correct them in Blender as you can see in the small rendering at the top of my post, but still there seem to be missing triangles in the Sandy 3D version.
  • It looks like there is an issue with a bounding box or face culling. I still need to investigate this thoroughly.

To be continued…

downloadExample FlashDevelop project.