Many cups of COFFEE later:
I can't tell you how much pain I went through trying to write the COFFEE side of the script. It was a similar experience to when I started learning AE scripting and mainly for the same reason, a lack of available real world examples of COFFEE code. There is some stuff out there, but not much.
That's not the case with AE scripting anymore. Thanks to AEnhancers and a number of other sites, there's a lot of code out there, which is a great reference for usage examples when you hit something new. In C4D, it seems that you can keep your script as a regular COFFEE script (like mine, ending in .CSC) or turn it into a plugin (source = .COF, compiled = .COB). Unfortunately, most COFFEE scripters seem to favour the compiled .COB format, so there's no chance to study their code.
The COFFEE language:
Here are links to the COFFEE 9.5 SDK and the main forums I searched for answers:
Problem - Recording Position keyframes:
The first major problem I had was simply finding a way to set the Position keyframes. I couldn't find a single usage example of someone using COFFEE to set a sequence of Position keyframes and from what I read, it seems C4D 9.5 COFFEE has some surprising limitations in this area.
I eventually found I could cheat by using CallCommand() which behaves similarly to AE scripting's app.executeCommand() for accessing menu commands. So my workaround for setting keyframes using COFFEE is basically to get the script to press the "Next Frame" button, then change the X, Y and Z values before hitting the "Big Red Record" button. It's dumb, but it works.
Problem - Doing the same for rotation:
I thought that'd be simple, but once I got the script to hit the "Goto Start Frame" button and repeated the process for the Rotation values, the keyframes came in ok, but now the "Big Red Record" button was also resetting the Position keys I'd already set. I eventually fixed it by storing the Position values in an array and applying the Position and Rotation keyframes at the same time.
Problem - Rotation order:
It was finally time to investigate the rotation order issue. I remembered Stu Maschwitz had posted a solution to AE's "wrong" rotation order on creative-workflow-hacks.com for Dale Bradshaw's auto camera rig creation script:
It was my lucky day. Stu's AE camera rig uses the same rotation order as C4D!!!! So that's why, for the time being, this script requires you to use that camera rig. I'm fairly sure I know enough now to create an inverse rig in C4D instead, but once you've used the rig, you may find you prefer it. If you pan then tilt AE's normal camera, you'll find the view is no longer level with the horizon. That doesn't happen with the camera rig or in C4D.
So here I am, a week later, with what will hopefully prove to be a useful script. Depending on the demand, there are a lot of improvements that should be possible. Automatically setting the duration and frame rate in C4D, exporting other things like nulls, lights and layers (as reference planes). If someone can help me with the math, I can probably sort out zoom / field of view too.
One possible snag is I've heard the animation system has been overhauled in C4D R10. So it's quite possible this script won't work in it. There's no SDK available for COFFEE in R10 yet either so I have no idea what's changed.
Well that's it, if you've made it to the end, thanks for taking the time. And if you try the script, let me know how it works out for you.