# Animating vertices from coordinates in “frame” files

I have coordinates of vertices in datafiles ("frames") with filenames 000.txt, 001.txt, 002.txt, …. The files have rows like this (vertex: ID, X, Y, Z):

15   2.525695e+00 -4.162952e+00 -2.020646e+00


I am not sure how to modify the answers here (which generate the coordinates within Python) for the case of reading in coordinates from "frame" files.

Here is how I import all the "frames"/files into one giant array data:

import glob

def fileNameToData(filepath):
file = open(filepath,'r')
data=[]
for i in lines:
data.append(list(map(float,i.split())))
return data

files = glob.glob('*.txt')
files.sort()
data=[]
for f in files:
for i in fileNameToData(f):
data.append(i)


How could my data be used to animate the vertices?

cf. ch. 7 and 8 of 3D Scientific Visualization with Blender

• Are you asking how to open and read the file, or how to use the data that has been read, or both? – cmomoney Jan 10 '17 at 4:07
• @cmomoney I only need to know "how to use the data that has been read". thanks – Geremia Jan 10 '17 at 17:05
• @cmomoney I've clarified my question. Hopefully that helps. thanks – Geremia Jan 10 '17 at 17:11

Here is what I did (based upon the answer here):

import bpy #Blender Python
import bmesh #Blender mesh creation pkg
import glob

#function def:
def fileNameToData(filepath):
#import first data file:
file = open(filepath,'r')
data=[]
for i in lines:
data.append(list(map(float,i.split()[1:4])))
return data

#number of objects / vertices
n = len(fileNameToData("000.txt")) # (000.txt is the first frame.)

files = glob.glob('*.txt')
files.sort()
data = [] #data is of the format ((frame 1 verts: (2,3,3), (3,6,2), …), (frame 2 verts: (0,5,2), (3,3,5), …), …)
for f in files:
data.append(fileNameToData(f))

n_frames = len(files)
bpy.context.scene.frame_end = n_frames

obj = bpy.context.active_object

for i_frame in range(n_frames):
block = obj.shape_key_add(name=str(i_frame), from_mix=False)  # returns a key_blocks member
block.value = 1.0
block.mute = True
vertex_idx = 0
for (vert, co) in zip(block.data, data[i_frame]):
vert.co = co
block.mute = True
block.keyframe_insert(data_path='mute', frame=0, index=-1)
block.mute = False
block.keyframe_insert(data_path='mute', frame=i_frame + 1, index=-1)
block.mute = True
block.keyframe_insert(data_path='mute', frame=i_frame + 2, index=-1)