# Is it possible to export a simulation (animation) from Blender to Unity?

I created an explosion animation within Blender, something as shown in this video. To achieve this I used a particle-system, together with the "Explode" modifier.

This kind of animation (simulation) doesn't get exported to other file-formats though and I'd like to export the animation as FBX, so that I can use it in Unity. So I basically need some way to "bake" the animation to keyframes. I wonder if there's a way to achieve this?

After some testing I found out, that the only way the simulation gets exported is by writing a sequence of obj files. So an answer on how to integrate/animate such a sequence of obj files in Unity would also be welcome.

Side note: I also tried to get an answer to this over at the blender community.

• As a 3D asset or a set of textures? (I assume the first). Also any reason you decided against a particle system? – Jonathan Dickinson Feb 14 '12 at 16:58
• @JonathanDickinson It's a 3D asset. Blender uses a particle-system internally to specify the physics for the Explode modifier. What this does is that it fractures and moves the geometry of a mesh, according to the particles. The particles are just there to model the movement of the mesh parts... And now I simply want to render this movement into separate keyframes (which will probably result in unique mesh geometry on every frame). – bummzack Feb 14 '12 at 18:54
• Sorry, I wish I knew. It's still doable with a particle engine that can handle 3D artefacts (but you would need to break up the model yourself and give everything thickness/depth - which isn't too hard). I can think of a fast way to do this without needing to export it - if you would consider that an answer? – Jonathan Dickinson Feb 14 '12 at 19:04

After countless tests (and some coding) I was able to find two valid approaches to the problem. Both aren't optimal solutions, but apparently there is none (exporting a simulation from Blender to Unity simply doesn't work).

### Approach 1: Export as obj sequence and swap meshes in Unity

Pretty straight-forward. Exporting a simulation using the obj exporter generates a separate file for every frame of the animation (if the "Animation" option has been toggled in the export options). Drag all these files into your Unity project (eg. into Assets/Models/MyAnimation). Then write a simple script to swap the mesh of the GameObject at runtime.

Pros:

• Animation is the same as in Blender

Cons:

• I haven't tested this with hi-poly meshes or multiple concurrently playing animations, but most likely it's going to have an impact on performance.
• It's really cumbersome to drag each mesh file separately onto the script component. Not really feasible for an animation with lots of frames. See image below (each Element in the Meshes array is a file with the mesh for that frame):

### Approach 2: Create the explosion procedurally within Unity

This approach is a mixture of the approach proposed by Jonathan Dickinson and the way the explode-modifier works in Blender. In Blender, the explode-modifier uses a particle-system to determine the movement of shards/shrapnels caused by the explosion.

I wrote a script that splits the mesh into triangles and uses a ParticleSystem to animate the triangles.

Pros:

• Works with any mesh.
• Behavior/animation can easily be tweaked by modifying the particle-system. For best results, the simulation has to be in world-space and the particle-renderer should be turned off (see screenshot below).

Cons:

• Explodes into plain old triangles, which doesn't look very natural.
• With hi-poly meshes it hits the CPU hard when the mesh is being split into triangles. This could be optimized though.

### Comparison

Here's a video that shows both types of animation in effect. The used scripts (both written in C#) can be found here:

### Update: MetaMorph

Thanks to the Blender Community (especially the users chipmasque and enzyme69) I was pointed towards a third possible solution, which is MetaMorph. It's a Unity script and a Blender export addon which allows you to export shape-keys from Blender and play them in Unity. While there seems to be some issues with getting a shape-key animation from an explosion, it should work fine for other types of simulations and other shape-key driven animations (eg. facial animations/morphs etc.).

• I actually kind of like the triangles one. It almost looks like the object disintegrated into a bunch of tiny pieces. – ashes999 Feb 19 '12 at 16:29
• i have a similar problem. What i need to do is import an obj sequence and make it work. So, relly helpfull for me is approch 1 but, because i am really new at this, can you please answer 2 questions? ! How do i fill up the list titled Meshes with my obj files, and 2 how do we activate the script so it starts to animate? Is it a component attached to an obj?Thanks in advance. – CannonBall Mar 14 '13 at 21:05
• @user27423 Drag your base-object into the Unity scene. Add the script as a component. Set the "Size" of Meshes to the number of meshes you use. Drag the different animation-states (eg. your meshes) to the corresponding slot of the script. Call Play on the script to make it animate. – bummzack Mar 15 '13 at 7:47
• @bummzack I am unable to get your mesh animation script to work. I added the first OBJ asset to the scene, and then added your script. I added some extra meshes (should element 0 be the initial mesh, or the 2nd?) but it simply wouldn't play (with clicking the mouse, as in your script). What does "Frame Duration" do exactly/what should it be set as? Could you provide a sample project/asset to show how you set it up? – Mr Pablo Nov 25 '14 at 10:28
• @MrPablo The animation "frames" should be all the mesh states.. so yes, element 0 would be the initial mesh. Frame Duration is the duration of one frame in seconds. So for an animation with 12 frames per second it's 0.0833 (1.0 / 12) – bummzack Nov 25 '14 at 12:10

I'm going to go ahead and answer this with the procedural approach. I'll assume you have a workable knowledge of linear algebra. I am going to hash over this quite quickly so feel free to ask me to expand on any point of the answer.

• Storing each model in memory (and disk) using a vertex buffer (or whatever your graphical platform uses).
• Having to render this buffer each frame; this includes swapping buffers in and out of GPU memory.

This isn't too different from this:

• Having to determine the 'broken' parts of the mesh (this can be pre-calculated during your build).
• Having to fill a buffer each frame (set up with discard-on-write).
• Having to render this single buffer each frame.

I would strongly recommend at least experimenting with a procedural approach to this.

Essentially what you need to do is find the normal of each triangle face, and choose a extrusion distance (just a constant that you can tweak, most likely). Following that create a new set of vertices for each triangle offset by Extrusion * -Normal (they can likely inherit the UV coord of their original vertex). Finally wire them up in the index buffer - which you can keep as static data (so long as you retain the order of the triangles). This can be done when you load meshes that can explode; or as part of your build phase. Remember that shared vertices (arising due to index buffers or such) need to be duplicated. I would essentially store the data in something like the following structure:

ExtrudedTriangle
{
Vector3 A, B, C, D, E, F
Vector3 TA, TB, TC // Texture coords, D, E, F are same as A, B, C
Vector3 Translation
Quaternion Rotation
Single Scale // Possibly
}


Now when you need to fill the buffer all you need to do is go over each triangle (in order) and append it to your vertex buffer. You don't need to worry about the indicies as those are constant. This is very similar to how sprite particle system works (except the indicies are not constant).

You can also skip filling the vertex buffer each frame by additionally passing in the offsetToCenterOfExtrudedTriangle, originalCenterOfExtrudedTriangle and extrudedTriangleNormal into the vertex buffer. You can then have your vertex shader animate each individual vertex.

• This is a nice approach, but sadly a bit too much work to to for my requirements. I have the animation in blender. Together with forces, elements that are bouncing off the ground etc. I'm just looking for an easy way to import this into the game-engine as an animated mesh. Doing it procedurally would certainly be nice, but is a bit overkill for my requirements. – bummzack Feb 14 '12 at 20:15
• No worries. I think it's a good answer nevertheless, it should definitely remain here. – bummzack Feb 14 '12 at 21:57