Inverting time remapping from within time remapped comp script
So I'm working on a script which involves a feature where a layer that's created inside a time remapped comp is time remapped to cancel out it's parent remapping, thus outside of that time remapped comp it'll play at regular speed, even though it's inside a remapped comp, it's an interesting challenge that I've made progress on but was wondering if anyone could help me figure out how to make it better.
An interesting finding is that that the keyframe curve of the nested layer that cancels out the parent comps time remapping is actually the exact same curve shape, except just rotated 90 degrees then flipped horizontally as you can see here:
parent time remap:
nested time remap (cancels out parent):
(Notice how when the curve of the parent comp gets really fast in the middle of that s-curve, to compensate for that the nest curve must get really flat etc)
At the moment my script places a keyframe on every frame, but I was wondering if anyone can think of a way to use the same number of keyframes as the parent remapping, but some how rotate and reverse the influence and speed of those curves to create the correct curve, since generating keyframes of every frame brings AE to it's knees on really long comps, causes major loading and will sometimes crash AE before finishing, it would need to involve somehow converting the influence value of the keyframe to the speed value and then reversing the keyframes.
The formula I've come up with to invert remapping is actually fairly basic, first it must get the ratio of the time remap by dividing each keyframe value with that keyframe time. The time of the new generated keyframe is then set to the containing comps keyTime divided by the ratio, then its value is set to parent comps keyValue multiplied by the ratio.
I've included the code I'm currently using to generate the invert curve.
I'm also wondering if it would even be possible to rather create an expression on the nested comps time remap to cancel out it's parent using a similar formula along with valueAtTime etc. so it calculates continuously?
Any additional thoughts or ideas here would be much appreciated!