Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. import bpy
  2. from math import *
  3.  
  4. def set_kp(kp, co, easing):
  5. kp.co = co
  6. kp.handle_left = co
  7. kp.handle_right = co
  8. kp.easing = easing
  9. kp.interpolation = 'SINE'
  10.  
  11.  
  12. def wipe_fcurves(action):
  13. while len(action.fcurves) > 0:
  14. action.fcurves.remove(action.fcurves[-1])
  15.  
  16.  
  17. def keyframe_action1(action):
  18. wipe_fcurves(action)
  19.  
  20. fc = action.fcurves.new("location", 2)
  21. fc.keyframe_points.add(5)
  22. set_kp(fc.keyframe_points[0], [0,0], 'EASE_OUT')
  23. set_kp(fc.keyframe_points[1], [20,1], 'EASE_IN')
  24. set_kp(fc.keyframe_points[2], [40,0], 'EASE_OUT')
  25. set_kp(fc.keyframe_points[3], [60,-1], 'EASE_IN')
  26. set_kp(fc.keyframe_points[4], [80,0], 'EASE_OUT')
  27.  
  28. def keyframe_action2(action, scale=1):
  29.  
  30. wipe_fcurves(action)
  31. timescale = 400
  32. fc = action.fcurves.new("location", 2)
  33. fc.keyframe_points.add(5)
  34. set_kp(fc.keyframe_points[0], [0,0], 'EASE_IN')
  35. set_kp(fc.keyframe_points[1], [1*timescale,1*scale], 'EASE_OUT')
  36. set_kp(fc.keyframe_points[2], [2*timescale,2*scale], 'EASE_IN')
  37. set_kp(fc.keyframe_points[3], [3*timescale,1*scale], 'EASE_OUT')
  38. set_kp(fc.keyframe_points[4], [4*timescale,0], 'EASE_IN')
  39.  
  40. def action_for(u,v):
  41.  
  42. name = "high frequency sine"
  43. action = bpy.data.actions.get(name)
  44. if action is None:
  45. action = bpy.data.actions.new(name)
  46. keyframe_action1(action)
  47.  
  48. return action
  49.  
  50. def action2a():
  51. name = "low frequency cosine up"
  52. action = bpy.data.actions.get(name)
  53. if action is None:
  54. action = bpy.data.actions.new(name)
  55. keyframe_action2(action, 2)
  56. return action
  57.  
  58. def action2b():
  59. name = "low frequency cosine down"
  60. action = bpy.data.actions.get(name)
  61. if action is None:
  62. action = bpy.data.actions.new(name)
  63. keyframe_action2(action, -2)
  64. return action
  65.  
  66.  
  67. def rig_track_one_strip(ad, track_no, strip_start_frame, action, source_start, source_end, repeat_count, strip_scale=1,
  68. track_name="high frequency", blend_type='REPLACE'):
  69. if track_no < len(ad.nla_tracks):
  70. tr1 = ad.nla_tracks[track_no]
  71. else:
  72. tr1 = ad.nla_tracks.new()
  73. tr1.name = track_name
  74. if len(tr1.strips) < 1:
  75. strip1 = tr1.strips.new("bacon", strip_start_frame, action)
  76. else:
  77. strip1 = tr1.strips[0]
  78. strip1.action = action
  79. strip1.frame_start = strip_start_frame
  80. strip1.action_frame_start = source_start
  81. strip1.action_frame_end = source_end
  82. strip1.repeat = repeat_count
  83. strip1.scale = strip_scale
  84. strip1.blend_type = blend_type
  85.  
  86. return (tr1, strip1)
  87.  
  88. def rig_nlas(obj, u,v):
  89. theta = u/3+v/10 # + frame*pi/40
  90.  
  91. if obj.animation_data is None:
  92. obj.animation_data_create()
  93.  
  94. ad = obj.animation_data
  95.  
  96. if 0 == u % 2:
  97. repeat = 40
  98. scale = 1
  99. else:
  100. repeat = 38
  101. scale = 40 / 38
  102. rig_track_one_strip(ad, 0, -theta / (pi / 40), action_for(u, v), 0,80, repeat, scale)
  103.  
  104. if 0==u%2:
  105. rig_track_one_strip(ad, 1, 1, action2a(), 0, 1600, 2, 1, "low frequency", 'ADD')
  106. else:
  107. rig_track_one_strip(ad, 1, 1, action2b(), 0, 1600, 2, 1, "low frequency", 'ADD')
  108.  
  109.  
  110. def fab_object_for(scn, u,v):
  111. name = "ball2 at %d,%d" % (u, v)
  112. obj = bpy.data.objects.get(name)
  113. if obj is None:
  114. obj = bpy.data.objects.new(name, bpy.data.meshes.get("Icosphere"))
  115. try:
  116. scn.objects.link(obj)
  117. except:
  118. pass
  119. obj.location = (u,v,0)
  120. rig_nlas(obj, u, v)
  121.  
  122.  
  123. scn = bpy.context.scene
  124.  
  125. for v in range(20):
  126. for u in range(20):
  127. fab_object_for(scn ,u,v)
  128.  
  129. keyframe_action1(bpy.data.actions['high frequency sine'])
  130. keyframe_action2(bpy.data.actions['low frequency cosine down'], -2)
  131. keyframe_action2(bpy.data.actions['low frequency cosine up'], 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement