Hardest part is formatting the data (IMHO). I'd probably read the values and create a dictionary of location lists per object, which allows to structure and access the values in a reasonable way:
dict = {
object-1 : [
[row1-X, row1-Y, row1-Z], # Frame 1
[row2-X, row2-Y, row2-Z], # Frame 2
[row3-X, row3-Y, row3-Z], # Frame 3
...
],
object-2 : [
[row1-X, row1-Y, row1-Z], # Frame 1
[row2-X, row2-Y, row2-Z], # Frame 2
[row3-X, row3-Y, row3-Z], # Frame 3
...
],
...
}
Reads the data and creates the dictionary
import bpy
file_data = [] # create an empty list
animation_data = {} # create an empty dictionary
# read the file
with open(<PATH-TO-FILE>) as f:
content = f.readlines()
for line in content:
# store the line as list in file_data
file_data.append(line.split())
# create location keyframe dictonary
for line in file_data:
try: # try converting strings to floats
floats = [float(i) for i in line]
# create composite list of this line
composite = [floats[x:x+3] for x in range(0, len(floats),3)]
# store it in the dict to organize the values
for c, item in enumerate(composite):
#key = "object" + str(c)
animation_data.setdefault(c, []).append(item)
except ValueError:
print ("Incorrect data")
Adds one cube per dict item to the scene as well as the keys, starting at frame 1
if animation_data:
for i in animation_data:
bpy.ops.mesh.primitive_cube_add()
# get the newly created cube
obj = bpy.context.object
# get keyframe location values
obj_keyframes = animation_data.get(i)
# start animation at frame 1
for c, loc in enumerate(obj_keyframes, 1):
bpy.context.scene.frame_set(c) # set frame
# move the cube and insert a keyframe
obj.location = (loc[0], loc[1], loc[2])
obj.keyframe_insert(data_path="location", index=-1)
It's basically the same for loose bones of an armature
if animation_data:
# get the selected object
selected_obj = bpy.context.object
if selected_obj.type == "ARMATURE":
# continue if number of bones match the data
if len(selected_obj.data.bones) == len(animation_data):
for i in animation_data:
# get the values from the dict
obj_keyframes = animation_data.get(i)
for c, loc in enumerate(obj_keyframes, 1):
bpy.context.scene.frame_set(c) # set the frame
# move the bone and insert a keyframe
selected_obj.pose.bones[i].location = (loc[0], loc[1], loc[2])
selected_obj.pose.bones[i].keyframe_insert(data_path="location", index=-1)
else:
print ("Data does not match number of bones")
file.read
andfile.readlines
for this case? If you need help scripting, post a MCVE. $\endgroup$1000 == 1.0e+003
Also suggest posting your script or a part thereof to determine a) the cause of your hassles, b) whether it is just a python issue and c) what you have tried so far. $\endgroup$