I am very new to blender and I am trying to move a cube in an environment. I have a csv file with a list of coordinates designated by columns, each column is the X, Y, or Z coordinates with n rows. From this list, I am trying to read the file in blender and move the cube to the coordinates in each row. Any python coding help or tips would be fantastic.
Assuming you need only the first 3 columns of each row and you need to iterate over, one way of doing could be this. you need to have your object selected
""" Example csv: 1.1, 1.3, 1.1 2.2, 2,1, 2.3 """ import bpy import csv csv_path = "path_to_your_csv" with open(csv_path) as file: csv = csv.reader(file, delimiter=",") # iterate over each rows assuming you need only # the the first 3 columns (if there are more) for row in csv: # row is a list with strings elements so must be converted in float x, y, z, *_ = [float(value) for value in row] bpy.ops.transform.translate(value=(x, y, z))
Need frame for an animation
If you wish to move the object to the coordinate of each row, use the coordinate to set its location.
Since it appears there are multiple coordinates for same object suggest making this an animation, hence some kind of frame needs to be used. In test script below the frame of first row is set to 1, with 10 added for each subsequent row. (ie keyframes at 1, 11, 21, 31, ...
Another method would be to add a frame column to the csv.
Similarly if you wish to displace the object by the vector of each row would add
ob.location += [float(v) for v in row[:3]]
for each displacement in csv.
If keyframing is not used, setting location will end in object being at last coordinate of csv, adding coordinate will result in object being displaced by the sum of the vectors in csv, after running script. (Row Summing When Moving an Object)
Test script, edit the path, select the object(s) you wish to animate with csv and run script.
""" Example csv: 1.1, 1.3, 1.1 2.2, 2,1, 2.3 """ import bpy import csv f = start_frame = 1 frames_per_row = 10 csv_path = "path_to_your_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