Your post does not relate to the Editor itself, it looks like a scripting question so it’s best asked on the Scripting forum. I’ll move your post there.
Note that there’s also a dedicated Animation forum.
Also, please edit your post to use code-tags as nobody is likely to wade through a wall of plain text.