Align Rotation Axis in Maya

[x]

Making a rig for my portfolio, I stumbled upon an annoyingly repetitive task:

Very often you need handles for a certain node (usually joints), in order to control it.
Optimally the handle uses the same rotation pivot as the target joint - meaning every rotation value on the handle results in the exact same rotation on the joint.
So the local rotation axes and rotation order need to be the same.
This can be done by simple rotating the handle - but the rotation values should be 0 (in rest position) in order to make animation easy and avoid gimbal lock.
(No, you can't just freeze the rotation - that would reset the local rotation axes)

The trick is to put the handle in a group, align the pivots with the target and then rotate the group so that the rotation axes are aligned.
The handle in it's group is then perfectly in sync with the target joint:
every X, Y or Z rotation means the exact X, Y, Z rotation to the target. All thats left to do is to place the constraint(s).

Since it's very repetitive to do so for every single joint, it's easier done with a script:

// Get current selection (Select first the target, then the handle)
string $select[] = `ls -sl`;
// Group the handle
string $group = `group -n ($select[1]+"_group") $select[1]`;
// Set the group pivot to object pivot
float $piv[] = `xform -q -ws -rp $select[1]`;
xform -ws -rp $piv[0] $piv[1] $piv[2] $group;

// Move the group to the target node/joint
float $pos[] = `xform -q -ws -rp $select[0]`;
move -rpr -a $pos[0] $pos[1] $pos[2] $group;

// Freeze transformations
makeIdentity -a true $group;
// Set rotation order
int $ro = `getAttr ($select[0]+".rotateOrder")`;
setAttr ($group+".rotateOrder") $ro;
setAttr ($select[1]+".rotateOrder") $ro;

// Align group local rotation axis with the target joint/node
float $rot[] = `xform -q -ws -ro $select[0]`;
xform -ws -ro $rot[0] $rot[1] $rot[2] $group;

A further improvement would be to transfer the rotation limits as well,
so you can't rotate your handle in ways the joint cant go...