iPLEOMAX

[Blender 2.7+ Addon] anim_keyframe_insert_blank.py

May 20th, 2014
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.97 KB | None | 0 0
  1. # Blank Keyframe Insertion Addon for Blender 2.70+
  2. # Copyright (C) 2014  Morshidul Chowdhury
  3.  
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8.  
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13.  
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.  
  17. bl_info = {
  18.     "name": "Insert Blank Keyframes",
  19.     "author": "Morshidul Chowdhury (iPLEOMAX)",
  20.     "version": (1, 0),
  21.     "blender": (2, 7, 0),
  22.     "location": "Graph Editor > Blank Keyframes",
  23.     "description": "Insert blank keyframes starting from current frame",
  24.     "warning": "",
  25.     "category": "Animation"
  26. }
  27.  
  28. #Addon created as a response to this thread:
  29. #http://blenderartists.org/forum/showthread.php?337331-Add-on-script-for-frame-insertion&p=2650614
  30.  
  31. import bpy
  32. from mathutils import Vector
  33. from bpy.types import Header
  34. from bpy.props import IntProperty
  35.  
  36. bpy.types.Scene.blank_keyframes = IntProperty(name = "Blank Keyframes", default = 1, min = 1, max = 1000)
  37.  
  38. class InsertBlankKeyframe(bpy.types.Operator):
  39.     bl_idname = "anim.keyframe_insert_blank"
  40.     bl_label = "Insert"
  41.     bl_description = "Insert blank keyframes at current frame"
  42.  
  43.     def execute(self, context):
  44.    
  45.         object = context.active_object
  46.         if not object: return {'CANCELLED'}
  47.        
  48.         scene = context.scene
  49.         if not scene: return {'CANCELLED'}
  50.    
  51.         bpy.ops.graph.keyframe_insert(type='ALL')
  52.        
  53.         frame = bpy.context.scene.frame_current
  54.         blanks = context.scene.blank_keyframes
  55.        
  56.         values = {}
  57.        
  58.         for fc in object.animation_data.action.fcurves:
  59.             for kp in fc.keyframe_points:
  60.                 if kp.co[0] >= frame:
  61.                     if kp.co[0] == frame:
  62.                         values[fc.data_path + str(fc.array_index)] = kp.co[1]
  63.                     kp.select_control_point = True
  64.                     kp.select_left_handle = True
  65.                     kp.select_right_handle = True
  66.                 else:
  67.                     kp.select_control_point = False
  68.                     kp.select_left_handle = False
  69.                     kp.select_right_handle = False
  70.  
  71.         bpy.ops.transform.translate(value=(blanks, 0, 0))
  72.        
  73.         bpy.context.scene.frame_current = frame
  74.         bpy.ops.graph.keyframe_insert(type='ALL')
  75.  
  76.         for fc in object.animation_data.action.fcurves:
  77.             index = fc.data_path + str(fc.array_index)
  78.             if index in values:
  79.                 for kp in fc.keyframe_points:
  80.                     if kp.co[0] == frame:
  81.                         kp.handle_left_type = 'FREE'
  82.                         kp.handle_right_type = 'FREE'
  83.                         #kp.handle_right[1] = kp.co[1]
  84.                         kp.co[1] = values[index]
  85.                         kp.handle_right[1] = kp.co[1]
  86.                     if kp.co[0] == frame + blanks:
  87.                         kp.handle_left_type = 'FREE'
  88.                         kp.handle_right_type = 'FREE'
  89.                         kp.handle_left[1] = kp.co[1]
  90.        
  91.         bpy.ops.transform.translate()
  92.         return {'FINISHED'}
  93.  
  94. class GRAPH_HT_header_custom(Header):
  95.     bl_space_type = 'GRAPH_EDITOR'
  96.  
  97.     def draw(self, context):
  98.         layout = self.layout
  99.         row = layout.row(align=True)
  100.         row.prop(context.scene, 'blank_keyframes')
  101.         row.operator(InsertBlankKeyframe.bl_idname)
  102.        
  103. def register():
  104.     bpy.utils.register_module(__name__)
  105.  
  106. def unregister():
  107.     bpy.utils.unregister_module(__name__)
  108.  
  109. if __name__ == "__main__":
  110.     register()
Advertisement
Add Comment
Please, Sign In to add comment