0
$\begingroup$

Please help, I have tried a Python forum and although I have had many suggestions none work.

I need to import data from a csv file.

Here is my code.

import bpy

ob1 = bpy.data.objects["Base"]
ob2 = bpy.data.objects["Middle Arm"]
ob3 = bpy.data.objects["Top Arm"]

ob1Strt = 0
ob1Dist = 1.5
ob1FrmEnd = ob1Dist*33

print (ob1FrmEnd)

import csv

line = 1 column = 2

with open("Program.txt", "r") as file:
    mycsv = csv.reader(file)
    mycsv = list(mycsv)
    text = mycsv[line][column]
    print(text)
    rotate=int(text)

I get the error message;

49.5 Traceback (most recent call last): File "C:\Users\Owner\Desktop\Blender\Probot 2\ProbotPositions.blend\Text", line 21, in FileNotFoundError: [Errno 2] No such file or directory: 'Program.txt' Error: Python script fail, look in the console for now...

$\endgroup$
  • $\begingroup$ Did you name your csv file "Program.txt" ?? If so, you may need to specify the directory it's in, because your script might be reading from the dir the script is in, or the blender base dir, which probably isn't the same dir your csv file is in. $\endgroup$ – Doyousketch2 Jan 30 '18 at 23:29
  • $\begingroup$ How Do I specify the directory the CSV file is in. I have tried many suggestions and none work. $\endgroup$ – Sean Feb 2 '18 at 21:20
1
$\begingroup$

Hard to tell from your question if that's one script or 2.

Might just be because you aren't used to entering code on StackExchange.
Preface code with four spaces, a tab, or surround with backticks.
Seems strange at first, but that's how they do it.

Assuming line = 1 and column = 2 are on separate lines,
as Python would error there otherwise.

Good practice to put all your import lines up top,
so if this is one script, I'd bring import csv up.

Bad practice to use the word file, as that happens to be a Python keyword.
Might not be important for the purposes of this script, but I called it openfile instead.

You can specify an absolute location of the filename in one line if you prefer,
then you don't have to bother with the .join(). It's up to you.
Might be easier if you reuse the script for other files.

Edit: Assuming limb only rotates along X-axis.
bpy.data.objects[limb].rotation_euler.x
if Y-axis rotates, you'd use .y instead.

import bpy
import csv
import os

ob1 = bpy.data.objects["Base"]
ob2 = bpy.data.objects["Middle Arm"]
ob3 = bpy.data.objects["Top Arm"]

ob1Strt = 0
ob1Dist = 1.5
ob1FrmEnd = ob1Dist*33

print(ob1FrmEnd)

directory = "C:\Users\(User_Name)\Documents"
filename = "Program.txt"

joined = os.path.join(directory, filename)

with open(joined, "r") as openfile:
    mycsv = csv.reader(openfile)
    for row in mycsv:
      limb, direction = row
      print(limb, direction)
      bpy.data.objects[limb].rotation_euler.x = int(direction)
$\endgroup$
  • $\begingroup$ import csv import os ob1 = bpy.data.objects["Base"] ob2 = bpy.data.objects["Middle Arm"] ob3 = bpy.data.objects["Top Arm"] ob1Strt = 0 ob1Dist = 1.5 ob1FrmEnd = ob1Dist*33 print(ob1FrmEnd) line = 1 column = 2 directory = "C:\Users\Owner\Desktop\Blender\Probot 2" filename = "Program.txt" joined = os.path.join(directory, filename) with open(joined, "r") as openfile: mycsv = csv.reader(openfile) mycsv = list(mycsv) text = mycsv[line][column] print(text) rotate = int(text) $\endgroup$ – Sean Jan 31 '18 at 6:47
  • $\begingroup$ I haven't seen the csv file you're using, so it's really hard to attempt writing code for it. This will only print out line 1 and column 2. Is that what you are going for? I'd imagine from your answer that it still didn't do exactly as intended... What exactly do you need? For it to iterate through each row and column? $\endgroup$ – Doyousketch2 Jan 31 '18 at 11:13
  • $\begingroup$ I have a model robot arm, I want to control its movements using data from a csv file. the print commands are only for testing my code. the csv file is simply contains the name of the limb then the distance moved separated by a comma. $\endgroup$ – Sean Jan 31 '18 at 23:27
  • $\begingroup$ i need the code to read from the file $\endgroup$ – Sean Jan 31 '18 at 23:30
0
$\begingroup$

I finally got a solution that works, thanks to everyone who answered.

import bpy

from pathlib import Path
p = Path('C:/')/'Users'/'Owner'/'Desktop'/'Blender'/'Probot 2/Program.txt'
print(str(p))

import csv
line = 1
column = 2

speed=50

with p.open() as file:
    mycsv = csv.reader(file)
    mycsv = list(mycsv)
    text = mycsv[line][column]
    print(text)
    distance0=int(text)*0.0175
    rotate0=int(distance0)*speed 
$\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.