1
$\begingroup$

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')
     lines = file.readlines()
     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

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

1 Answer 1

1
$\begingroup$

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')
     lines = file.readlines()
     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.)

#Read in all data files.
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

#add mesh object:
bpy.ops.mesh.primitive_circle_add(vertices=n)
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)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .