No Flip Knee finding offset via Script

Hi Guys,

I wrote a script for a IK/FK leg switch with a no flip knee setup.
The way I`m doing the no flip knee is like in the Art of Rigging. After creating the ikhandles I zero out all pole vectors except the polevectorX and put a value of 0.01. This causes the knee to flip sideways but I know i have to put a value of 90 into the twist attribute to get back to the original position, because I created the joints in a linear way. But if the joint are not linear the twist value changes. I know if you duplicate the joints and use that as a reference you can find the right twist value but how would I go about it in a script ? I tried some things but failed with all of them.

Also if there is a better way of doing a no flip knee let me know.

Cheers
Marcell

you could use a locator positioned at the hip aiming at the noflip ankle with the noflip knee as an upvector
parent a new locator in that one, do the same but then for the original leg chain. then use the rotation value you find in the new locator for the twist
i think that should do the trick.