2
$\begingroup$

I've been trying animate a large amount of objects based on location and frame data from an excel sheet. So far I've read through a dozen different threads both here and other forums, but I'm just not able to understand how to do it. My only previous coding experience is only a small amount of html/css, so having a slightly hard time wrapping my head around this.

enter image description here

Object_id,X-Loc,Y-Loc,Z-Loc,Frame
Sphere_01,0,0,0,1
Sphere_01,6,0,0,25
Sphere_01,6,-6,0,40
,,,,
Cube_01,3,3,0,1
Cube_01,3,3,10,25
Cube_01,3,3,0,40
Cube_01,10,3,0,60

This is what I use to test on. The goal is to have the script select each object, set its location at a specified frame and keyframe it. So that I can then manually tweak the transition between them if needed.

So far this is the code I have so far, partially copied from here. And i somewhat understand what it does, I think, it at least moves the sphere in the scene.

import bpy
import csv

csvfile = open('FilePath/Sphere_Anim.csv')
readcsv = csv.reader(csvfile, delimiter=',')

for i, row in enumerate (readcsv):
    if i==0: continue
    obname = row[0]
    loc = [float(x) for x in row[1:4]]
    frame = row[4]

    bpy.context.object.location = loc

Would greatly appreciate it if someone could point me in the right direction.

$\endgroup$
0
$\begingroup$

Data to blender edits.

  • Get the scene object ob by name. Skip if not present in scene.
  • Add location keyframes at each frame using

    ob.keyframe_insert("location", frame)
    

    Read the docs:

Edited script.

import bpy
import csv

scene = bpy.context.scene    

csvfile = open('FilePath/Sphere_Anim.csv')
readcsv = csv.reader(csvfile, delimiter=',')

for i, row in enumerate(readcsv):
    if i == 0: 
        continue
    obname = row[0]
    # get the object
    ob = scene.objects.get(obname)
    if not ob:
        continue
    loc = [float(x) for x in row[1:4]]
    # set location
    ob.location = loc # local
    #ob.matrix_world.translation = loc # global
    frame = int(row[4])
    # insert all location keyframes  
    ob.keyframe_insert("location", frame=frame)

Related.

Fast keyframe insertion

| improve this answer | |
$\endgroup$
  • $\begingroup$ Oh, wow, this seems to work perfectly :) Thanks for the links too, seems I've got a lot to read up on:) $\endgroup$ – GalacticSalmon Mar 10 '19 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.