It took me forever to get this right so I thought I'd post my solution. Most of the examples I found were for previous versions of Blender (the API changed a lot in Blender 2.5). The sample scripts I ...