Create a file that contains object *and* movement data that can be imported to Blender

I'm experimenting (outside of Blender) with some algorithms that place balls in a larger box, then incrementally move these balls to optimize positions until they reach an optimal state. Now, i would like to use Blender to visualize these algorithms, once to present the results to some audience, but also to find problems in the algorithms, as well as implementation bugs, as it's much easier to spot a wrong movement in a visualization than in a huge table of coordinates.

So what i would like to do is

1. After each step of my algorithm, create a file that can be imported into Blender and contains the X/Y/Z coordinates of each of those balls
2. Create a kind of motion description, like "File0 is the list of ball positions at 0 seconds, File1 is the list of positions after one second; i want a movie that moves each ball from its 1st position to the 2nd one"

I know how to solve part 1), as writing STL files is not very difficult, and Blender has an import feature. I will need to create a mesh of triangles around each center point, but i'm not afraid of that either.

The part where i don't have a clue how to do it is 2). Ideally, i'd like to create a file that i can import that contains all coordinate sets at all frame positions, but if that's not possible, i could import each STL file at its designated frame manually. However, i still have no clue how to tell blender "file 2 contains the same objects as file 1, just at a different point of time", and i don't want to "connect" every single vertext manually.

Is there a file format that can specify object positions as well as position changes over time, that's not too hard to create, that can be imported into Blender?

Instead of writing complete meshes you could use the first file to create the objects and set them to their initial position.

Ball.1=1,2,3
Ball.2=3,4,5
Ball.3=3,3,3.5
...


In Blender you would read this file line by line get the objects name and create a sphere by invoking:

bpy.ops.mesh.primitive_uv_sphere_add(size=1, view_align=False, enter_editmode=False, location=( X, Y, Z from file))


The following files could then simply have the names and the offset relative to the previous frame. The object are identified by their names:

Ball.1=.1,.1,.2
Ball.2=-.1,-.1,.3
Ball.3=.1,.2,.5
...


Which could be parsed with something like this (Note: the code wasn't tested it was just assembled from snippets I already created):

import bpy

for frame in range(0,10):

filename='balls' + str( frame )
for line in open(filename):
line=line.rstrip("\n")
obj,pos=obj_key.split("=")
x,y,z=pos.split(",")
bpy.context.scene.frame_set( int( frame )) # frame from file name or loop variable
bpy.context.scene.objects.active = bpy.data.objects[ obj ]
bpy.ops.transform.translate(value=( float( x ), float( y ), float( z )))


And add a keyframe for changed object.

bpy.ops.anim.keyframe_insert(type='BUILTIN_KSI_LocRot')


Related:

My first option would be to use python to implement your algorithm and run it directly in blender to position the balls directly.

Another option would be to read the first file to create and position the balls then keyframe the positions. Then read the next file using the data to move each corresponding ball to the new location and keyframe. Repeat. You would only need a text file with 3 values per line, these would be absolute coordinates in the first file but could be absolute or distances moved for following files.

Blender has an importer/exporter for MDD files (Lightwaves MotionDesigner format). It is only about 100 lines for the exporter so shouldn't be hard to follow. There is also a mesh cache modifier that can use the MDD file to alter a mesh object. While this format is designed for mesh deformation data per frame, you could define a mesh with a vertex at the location of each sphere, then use dupliverts to place a sphere at the location of each vertex in blender.