0
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ there are only 3 columns (x, y, z) and you need to iterate over each row? $\endgroup$ – Virgil Sisoe Feb 11 at 20:27
1
$\begingroup$

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))
$\endgroup$
  • $\begingroup$ Thank you for the help Virgil! I tried your code and I am still having problems with reading the csv file. I need to find a way to change the values from strings to floats so they can be used in blender. $\endgroup$ – Jot1925 Feb 12 at 14:27
  • $\begingroup$ if you use my code then this line does just that. x, y, z, *_ = [float(value) for value in row]. if you are instead using part of this code into your, you could show it so I can help you better $\endgroup$ – Virgil Sisoe Feb 12 at 16:48
  • $\begingroup$ Upvote for pinching your code for alternative answer. Rather than using the operator for each row, could sum them and run the operator once with result. See blender.stackexchange.com/questions/7358/… $\endgroup$ – batFINGER Feb 23 at 3:12
0
$\begingroup$

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
$\endgroup$

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.