C4D Python Party: Rigging Splines

Point auto-rig Update: Now with more control over your spline tangents! You can animate uneven tangents and corners more easily with the latest version. I've also made a tutorial about the latest rig.
Update: The first version of the script would flip your spline tangents, I've put an updated version here that fixes that.

Do you like animating splines? I love animating splines! They're great in lofts, extrudes, sweeps, lathes, spline wraps and all sorts of other places.

Cinema 4D lets you animate splines in a bunch of ways, including by directly moving their points around with PLAs - but these are a mixed blessing. They don't let you control their curves, and if you want to keyframe one point, every point gets keyframed.

This script, 'Point autorig', will create nulls for you that are linked to your spline's points. Animate the nulls' positions to move the spline, and for Bezier splines you can scale and rotate the nulls to change the spline's tangents.

To install the script:
1 Download it here (it's a freebie)
2 Unzip it into your scripts folder (eg "C:\Program Files\MAXON\CINEMA 4D R13\library\scripts\" on Windows, or "/Applications/MAXON/CINEMA 4D R13/library/scripts/" on OSX)

To use the script:
1 Select the spline you want to rig.
2 Run the script (click 'Script' > 'User Scripts' > 'point_autorig').
3 You'll end up with a bunch of new nulls under your spline, animate them!

This script will also work on FFDs and polygon objects (although you can quickly get a huge amount of nulls that way which are unwieldy to animate with, so I would only recommend it for objects with a small number of points).

I think it's quite exciting to be able to use scripts to automatically rig stuff for you. Joe showed me a rig for animating splines using the IK-Spline tag, which inspired me to see if Python can do that set up automatically. This script sets up the joints and tags necessary to animate your splines with IK-Splines. While I was messing about with that script I realised that since I was using Python anyway I didn't need the IK-Spline as the go-between for my nulls and my spline anymore (but maybe someone who wants to actually use the IK-Spline for joint rigging can modify it).

While writing the old script I also learnt about the 'CallButton' command, which appears on line 79:
79    c4d.CallButton(iktag, c4d.ID_CA_IKSPLINE_HANDLE_ADD)
I'm using that to fake a button click on the Spline-IK tag (passed as the first variable, I've called it 'iktag') - the second variable is the ID of the button you want to click.

Because you can't drag buttons onto your console it's a bit tougher to find what their ID values are. I ended up searching within Cinema 4D's resource files, which are full of files that describe all the GUI elements that go with each tool, object, tag etc - you can open these up in any text editor and read through them. The one that belongs to the Spline-IK tag is called 'tcaikspline.res', and on line 99 I found what I needed:
The second word is the ID we need to access that button in scripts, and you could use the 'CallButton' command on the 'Create' or 'Remove' buttons in the Spline-IK tag using their own ID values from the next two lines in that resource file. Or you could use Python to press any other button.

'CallButton' did give me an error when trying to use it from a python tag, so it seems it will only work inside 'User Scripts...' (and perhaps Python plugins, although I haven't tested that). It also messes about with your Undoing a bit - you can see this if you run the older script and try undoing it, you have to undo once for each point instead of once for the entire script. It seems C4D automatically adds an undo for every CallButton command (which is fair enough in case of horrible accidents), so even if you're adding your own undo events in your script extra undos are added for the button presses.

Thanks to Joe for sharing his IK-Spline knowledge, and also to Chiny who showed me the great trick of using Python scripts to set the code for a Python tag with multi-line strings.

That's all for now, see you next time!
Posted March 21, 2013