Guest User

Untitled

a guest
Dec 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. import bpy
  2. from pdb import set_trace
  3.  
  4. # quick handy little lambdas
  5. first = lambda l : l[0]
  6. last = lambda l : l[-1]
  7.  
  8. def chunks(lst, n):
  9. for i in range(0, len(lst), n):
  10. yield lst[i:i+n]
  11.  
  12. def swap_ys(p1, p2):
  13. p1.co.y, p2.co.y = p2.co.y, p1.co.y
  14.  
  15. ob = bpy.context.object
  16. fcurves = ob.animation_data.action.fcurves
  17. fcrv = last([c for c in fcurves if c.select])
  18. kps = fcrv.keyframe_points
  19. kps = [kp for kp in kps if kp.select_control_point]
  20. kps_pairs = list(chunks(kps, 2))
  21.  
  22.  
  23. kps_pairs = filter(lambda l: len(l)>1, kps_pairs)
  24.  
  25. def swap_pair(pair):
  26.  
  27. def get_handle_diff(pt):
  28. left_diff = pt.handle_left.y - pt.co.y
  29. right_diff = pt.handle_right.y - pt.co.y
  30. return left_diff, right_diff
  31.  
  32. # get the handle relative diff in advance before swapping
  33. left_diff_0, right_diff_0 = get_handle_diff(pair[0])
  34. left_diff_1, right_diff_1 = get_handle_diff(pair[1])
  35.  
  36. # do the coordinate swap
  37. y1, y2 = pair[0].co.y, pair[1].co.y
  38. pair[0].co.y, pair[1].co.y = y2, y1
  39.  
  40. # update the handles
  41. pair[0].handle_left.y = pair[0].co.y + left_diff_0
  42. pair[0].handle_right.y = pair[0].co.y + right_diff_0
  43.  
  44.  
  45. pair[1].handle_left.y = pair[1].co.y + left_diff_1
  46. pair[1].handle_right.y = pair[1].co.y + right_diff_1
  47.  
  48.  
  49.  
  50. for pair in kps_pairs:
  51. swap_pair(pair)
Add Comment
Please, Sign In to add comment