Paste Code
Paste Blends
Paste Images
import bpy


def create_cursor(context, filepath):

if context.object and context.object.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

#for ob in bpy.data.objects:
# ob.select = False
bpy.ops.object.select_all(action='DESELECT')

# Add empty object
ob = bpy.data.objects.new("Cursor", None)
context.scene.objects.link(ob)
#ob.location.y = context.scene.cursor_location.y
context.scene.objects.active = ob
ob.select = True

f = open(filepath, 'r')
#data = f.read()
data = f.readlines()
f.close()

#data = data.replace(" ", "").strip(",;")

#x, y = [pairs.split(",") for pairs in data.split(";")]

#pairs = data.split(";")

ob.animation_data_create()
ob.animation_data.action = bpy.data.actions.new(name="MyCursorAction")
fcu_x = ob.animation_data.action.fcurves.new(data_path="location", index=0)
fcu_x.keyframe_points.add(len(data))
fcu_z = ob.animation_data.action.fcurves.new(data_path="location", index=2)
fcu_z.keyframe_points.add(len(data))

counter = 0
for frame, pair in enumerate(data, context.scene.frame_current):

# Cast coords from string to int
x, z = [int(pair) for pair in pair.split(",")]

# Keyframe xz locations to be viewed from front view
#ob.location.x = pair[0]
#ob.keyframe_insert("location", index=0, frame=frame)
#ob.location.z = pair[1]
#ob.keyframe_insert("location", index=2, frame=frame)

fcu_x.keyframe_points[counter].co = frame, x / 100
fcu_z.keyframe_points[counter].co = frame, z / 100
fcu_x.keyframe_points[counter].interpolation = \
fcu_z.keyframe_points[counter].interpolation = 'CONSTANT'
counter += 1

context.scene.update()
if context.scene.frame_end < frame:
context.scene.frame_end = frame
context.scene.frame_set(context.scene.frame_current)

return {'FINISHED'}


# ImportHelper is a helper class, defines filename and
# invoke() function which calls the file selector.
from bpy_extras.io_utils import ImportHelper
from bpy.props import StringProperty
from bpy.types import Operator


class ImportCursor(Operator, ImportHelper):
'''Import X and Z values in the format: x1,y1;x2,y2; etc.'''
bl_idname = "import_anim.cursor"
bl_label = "Import Cursor animation"

# ImportHelper mixin class uses this
filename_ext = ".txt"

filter_glob = StringProperty(
default="*.txt",
options={'HIDDEN'},
)

def execute(self, context):
return create_cursor(context, self.filepath)


# Only needed if you want to add into a dynamic menu
def menu_func_import(self, context):
self.layout.operator(ImportCursor.bl_idname)


def register():
bpy.utils.register_class(ImportCursor)
bpy.types.INFO_MT_file_import.append(menu_func_import)


def unregister():
bpy.utils.unregister_class(ImportCursor)
bpy.types.INFO_MT_file_import.remove(menu_func_import)


if __name__ == "__main__":
register()

# test call
bpy.ops.import_anim.cursor('INVOKE_DEFAULT')
  1. import bpy
  2.  
  3.  
  4. def create_cursor(context, filepath):
  5.    
  6.     if context.object and context.object.mode != 'OBJECT':
  7.         bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
  8.        
  9.     #for ob in bpy.data.objects:
  10.     #    ob.select = False
  11.     bpy.ops.object.select_all(action='DESELECT')
  12.        
  13.     # Add empty object
  14.     ob = bpy.data.objects.new("Cursor", None)
  15.     context.scene.objects.link(ob)
  16.     #ob.location.y = context.scene.cursor_location.y
  17.     context.scene.objects.active = ob
  18.     ob.select = True
  19.    
  20.     f = open(filepath, 'r')
  21.     #data = f.read()
  22.     data = f.readlines()
  23.     f.close()
  24.  
  25.     #data = data.replace(" ", "").strip(",;")
  26.    
  27.     #x, y = [pairs.split(",") for pairs in data.split(";")]
  28.    
  29.     #pairs = data.split(";")
  30.    
  31.     ob.animation_data_create()
  32.     ob.animation_data.action = bpy.data.actions.new(name="MyCursorAction")
  33.     fcu_x = ob.animation_data.action.fcurves.new(data_path="location", index=0)
  34.     fcu_x.keyframe_points.add(len(data))
  35.     fcu_z = ob.animation_data.action.fcurves.new(data_path="location", index=2)
  36.     fcu_z.keyframe_points.add(len(data))
  37.    
  38.     counter = 0
  39.     for frame, pair in enumerate(data, context.scene.frame_current):
  40.        
  41.         # Cast coords from string to int
  42.         x, z = [int(pair) for pair in pair.split(",")]
  43.        
  44.         # Keyframe xz locations to be viewed from front view
  45.         #ob.location.x = pair[0]
  46.         #ob.keyframe_insert("location", index=0, frame=frame)
  47.         #ob.location.z = pair[1]
  48.         #ob.keyframe_insert("location", index=2, frame=frame)
  49.        
  50.         fcu_x.keyframe_points[counter].co = frame, x / 100
  51.         fcu_z.keyframe_points[counter].co = frame, z / 100
  52.         fcu_x.keyframe_points[counter].interpolation = \
  53.         fcu_z.keyframe_points[counter].interpolation = 'CONSTANT'
  54.         counter += 1
  55.        
  56.     context.scene.update()
  57.     if context.scene.frame_end < frame:
  58.         context.scene.frame_end = frame
  59.     context.scene.frame_set(context.scene.frame_current)
  60.        
  61.     return {'FINISHED'}
  62.  
  63.  
  64. # ImportHelper is a helper class, defines filename and
  65. # invoke() function which calls the file selector.
  66. from bpy_extras.io_utils import ImportHelper
  67. from bpy.props import StringProperty
  68. from bpy.types import Operator
  69.  
  70.  
  71. class ImportCursor(Operator, ImportHelper):
  72.     '''Import X and Z values in the format: x1,y1;x2,y2; etc.'''
  73.     bl_idname = "import_anim.cursor"
  74.     bl_label = "Import Cursor animation"
  75.  
  76.     # ImportHelper mixin class uses this
  77.     filename_ext = ".txt"
  78.  
  79.     filter_glob = StringProperty(
  80.             default="*.txt",
  81.             options={'HIDDEN'},
  82.             )
  83.  
  84.     def execute(self, context):
  85.         return create_cursor(context, self.filepath)
  86.  
  87.  
  88. # Only needed if you want to add into a dynamic menu
  89. def menu_func_import(self, context):
  90.     self.layout.operator(ImportCursor.bl_idname)
  91.  
  92.  
  93. def register():
  94.     bpy.utils.register_class(ImportCursor)
  95.     bpy.types.INFO_MT_file_import.append(menu_func_import)
  96.  
  97.  
  98. def unregister():
  99.     bpy.utils.unregister_class(ImportCursor)
  100.     bpy.types.INFO_MT_file_import.remove(menu_func_import)
  101.  
  102.  
  103. if __name__ == "__main__":
  104.     register()
  105.  
  106.     # test call
  107.     bpy.ops.import_anim.cursor('INVOKE_DEFAULT')
  108.  
go to heaven