0
$\begingroup$

I have used the script to move an object using CSV. Can it be modified to include rotation as well? Can this be done using one imported file, or is it best with two imported files. One for Location the other for Rotation.

The object needs to move and rotate using all csv coordinates.

import bpy
import csv

f = start_frame = 1
frames_per_row = 10    
csv_path = "C:/Book3.csv"

ob = bpy.context.object

with open(csv_path) as file:
    csv = csv.reader(file, delimiter=",")

    for row in csv:
        ob.location = [float(v) for v in row[:3]]
        ob.keyframe_insert("location", frame=f)
        f += frames_per_row
$\endgroup$

1 Answer 1

3
$\begingroup$

This is a somewhat trivial task given the code is basically already there

Data contained in the same csv with columns like:

loc_x,loc_y,loc_z,rot_x,rot_y,rot_z

import bpy
import csv


f = start_frame = 1
frames_per_row = 10    
csv_path = "C:/Book3.csv"

ob = bpy.context.object

with open(csv_path) as file:
    csv = csv.reader(file, delimiter=",")

    for row in csv:
        ob.location = [float(v) for v in row[:3]]
        ob.rotation_euler = [float(v) for v in row[3:6]]
        ob.keyframe_insert("location", frame=f)
        ob.keyframe_insert("rotation_euler", frame=f)
        f += frames_per_row

If two files need to be used you can either zip them beforehand or keep track using the keyframe. It is also possible to use quaternion rotation.

$\endgroup$
2
  • $\begingroup$ The script I used in my post works perfectly with either the location or the rotation. Once I use both together (as in the answer) they do not seem to work. All my locations are in columns 1 to 3 and my radians are in 4 to 6. $\endgroup$ Commented Dec 5, 2020 at 21:57
  • $\begingroup$ I think this one is the culprit as it works with a 3 column CSV file but not a 6 column CSV "file ob.location = [float(v) for v in row[:3]]" From what I see the columns can be set, so [3:6] means that it starts reading at column 3 and stops at 5. $\endgroup$ Commented Dec 5, 2020 at 22:27

You must log in to answer this question.

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