# Create reusable action/animation?

I created a tank turret and I was wondering if there was a way to create script or a "property" for firing the turret instead of animating it firing each time manually (telling it to reload shells (visible to viewer), create smoke, create shell, add physics to said shell, add recoil that is based on turret angle, etc.)?

If so, are there any examples that I could watch/read to understand it a bit better?

• I have edited your question to try and make it more clear. If I you disagree with my edits, please feel free to roll them back :) – gandalf3 Feb 28 '14 at 0:05
• A few options comes to mind: NLA Editor, Rigging, Drivers, and finally Python script. Are you using or familiar with these features? Detailed answer coming soon. – Mike Pan Feb 28 '14 at 1:06
• No, I only know really the basics of each one of those. – Starius Mar 10 '14 at 1:42

You can actually quite a bit with just a few drivers.

Result

I'm not going to explain the entire process, but I'll cover a lot of the more interesting points.

If you haven't already, learn how drivers work (here's a tutorial I made). They're an extremely powerful way to control different object attributes.

For the barrel recoil, I created a driver that makes the barrel follows the position of an empty (the one moving back and forth). The position of each section of the barrel is just controlled by a custom curve (offset for the back section so it follows slower). This is the curve of one section:

There are three bullets at the beginning of the animation hidden in the back of the barrel. I couldn't figure out how to dynamically add them while the animation was playing. There might be a hacky way to do it with a particle system, but I decided that hiding them was the simplest solution.

The first thing to note about the bullets is that they're dropping as they follow the empty. All I did was create a driver that calculates the trajectory of the projectile using the Y position of the empty for the current distance. Because it was long (and hard to type into the expr bar), I just put it in a Python script:

Then all you have to do in the expr box is put: calc_path(x, ang, height, velocity).

Finally, the bullets are set to be animated rigid bodies. That way they can interact with the other rigid bodies in the scene. When I wanted them to simulate and bounce around on the ground, I simply animated the animated attribute in the rigid body settings:

The result isn't completely elegant, but it certainly does cut down on the amount of animation you have to do.

If you're interested in dissecting the .blend yourself, you can download it.