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')
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')
|