2
$\begingroup$

Most advice on importing csv files seems to assume users are on Windows, not Mac and many hours of effort has not helped this noobie to figure out the Mac setup and paths to import csv files to Blender text window. So I'm thinking just create a big string as below outside Blender and paste into Blender Python text window and then program a way to convert this string to a list(lines of data) of lists(variables). Does this make sense? Simple enough but somewhat defeatist!

CSV FILE 5 LINES
number,y,x,z,m
1,0,0.44,8.19,11.21
2,-11.68,8.13,8.31,7.22
3,-1.54,1.42,5.93,2.16
4,-7.1,3.13,6.25,1.42
5,5.17,3.13,8,1.39

(REAL DAT WILL BE 1000+ LINES)

MANUALLY CONVERT TO A LONG STRING
quakeTxt="1,0,0.44,8.19,11.212,-11.68,8.13,8.31,7.223,-1.54,1.42,5.93,2.164,-7.1,3.13,6.25,1.425,5.17,3.13,8,1.39"

PASTE INTO BLENDER

Because the script below doesn't work

import os
import bpy

blendPath = /Users/dickmeehan/Desktop
fileName = "xyzevents.csv" # name of the csv which is on Desktop

txt = open(blendPath + fileName, "r") # open the file to read
quakeTxt = txt.read() # read the entire file into a string
txt.close() # close the file
print(quakeTxt)
$\endgroup$
2
  • 2
    $\begingroup$ why should using a mac make the paths any harder? python has a os.path module that makes the differences in OSs much less of a problem. $\endgroup$
    – David
    Mar 10 '18 at 15:19
  • $\begingroup$ I'm not in the best position to answer (because I don't have access to a mac) However your blendPath will not work - at all. Strings need to be quoted. try filePath = r"/Users/dickmeehan/Desktop/xyzevents.csv" $\endgroup$
    – David
    Mar 10 '18 at 15:43
1
$\begingroup$

Use the os.path module

I'm not on a mac. The code you posted wont work on any os, unless you have a "/Users/dickmeehan/Desktopxyzevents.csv" csv file in your user folder.

import os

dirpath = "/Users/dickmeehan/Desktop"
filename = "xyzevents.csv"

filepath = os.path.join(dirpath, filename)
txt = open(filepath, "r")
quaketxt = txt.read()

txt.close()
print(quaketxt)

Note: If the blend file is saved then the folder it is in is

blenfolder = os.path.dirname(bpy.data.filepath)

Use the filebrowser

To avoid having to hardcode in paths altogether, open Text Editor > Templates > Operator File Import and edit any occurences of ".txt" to ".csv". (approx line 28)

# ImportHelper mixin class uses this
filename_ext = ".csv"

filter_glob = StringProperty(
        default="*.csv",
        options={'HIDDEN'},
        maxlen=255,  # Max internal buffer length, longer would be clamped.
        )

run the script, select the file... and bingo prints the selected csv file to system console. Edit the read_some_data(..) method to suit.

Use the Text Editor

open the csv file in a text block (or paste into new and rename)

for line in bpy.data.texts["xyzevents.csv"].lines:
    print(line.body)

Lastly

There is no need to convert to some unwieldy string.

csvtext = '''
number,y,x,z,m
1,0,0.44,8.19,11.21
2,-11.68,8.13,8.31,7.22
3,-1.54,1.42,5.93,2.16
4,-7.1,3.13,6.25,1.42
5,5.17,3.13,8,1.39
'''

print(csvtext)
$\endgroup$
3
  • $\begingroup$ The first os.path module worked great for me. It also helped to learn that at if you want to see output, least on the Mac, Blender should be opened from the terminal window by commanding /Applications/Blender/blender.app/Contents/MacOS/blender $\endgroup$ Mar 23 '18 at 5:20
  • $\begingroup$ Congratulations on finding how to start blender from console on Mac. If you want to markdown your code, select text hit the {} button or press ctrl-K $\endgroup$
    – batFINGER
    Mar 24 '18 at 11:53
  • $\begingroup$ Thanks much you've been very helpful consistently. I've been pulling in data from another place but your last idea allows me to include the data within the blender file which works for my purposes. But my data file is quite large, 1000 lines, not 5 as you have shown, and visually dominates the python file. Is there some way it can be included and referenced in the blender file but not shown in the python script? $\endgroup$ Mar 30 '18 at 16:42

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.