In as much as you can get all the texts in a blend file with [text for text in bpy.data.texts]
and the filepath is text.filepath
. If a file is pasted into the text editor then it will have filepath = ""
. I recommend using the text editor current text as your csv data. A simple panel is added to the text editor properties, which ensures that the current csv file is
current_csv_file = context.edit_text
Firstly, a simple operator that prints out the csv data, split by "," line for line.
import bpy
def main(context):
text = context.edit_text
for line in text.lines:
split = line.body.split(",")
print(split)
class SimpleOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "text.import_csv"
bl_label = "Import CSV"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
main(context)
return {'FINISHED'}
def register():
bpy.utils.register_class(SimpleOperator)
def unregister():
bpy.utils.unregister_class(SimpleOperator)
if __name__ == "__main__":
register()
Can make a panel in the text editor UI like so

import bpy
class TextPTImportCSV(bpy.types.Panel):
"""Creates a Panel in the scene context of the properties editor"""
bl_label = "Import CSV"
bl_idname = "TEXT_PT_import_csv"
bl_space_type = 'TEXT_EDITOR'
bl_region_type = 'UI'
@classmethod
def poll(cls, context):
text = context.edit_text
return text.name[-4:].lower() == ".csv"
def draw(self, context):
layout = self.layout
layout.label(context.edit_text.name)
layout.operator("text.import_csv")
def register():
bpy.utils.register_class(TextPTImportCSV)
def unregister():
bpy.utils.unregister_class(TextPTImportCSV)
if __name__ == "__main__":
register()
which running on the simple test file prints
['1', ' 1', ' 1', ' 1', ' 1', ' 1']
['2', ' 2', ' 2', ' 2', ' 2', ' 2']
['3', ' 3', ' 3', ' 3', ' 3', ' 3']
to the console.
EDIT: as a side note, split
above will be a list of strings, and will need to be converted if they are other. Here is a basic way to make a key for your csv file. Consider a file that has 4 columns, the first is an integer for frame, the next three a location. Quite likely there is a way to use the text.lines
as input to csv.reader
(simple workaround would be to save to tmp file)
# key of csv file, [property name, column(s), type]
key = [["frame", 0, int], # frame in col 1
["location", [1, 2, 3], float] # loc x, y, z in cols 1, 2, 3.
]
'''
data in csv like
1, 1.0, 1.0, 1.0
'''
split = ['1', ' 1.0', ' 1.0', '1.0']
for prop, cols, prop_type in key:
if isinstance(cols, list):
val = [prop_type(split[col]) for col in cols]
elif isinstance(cols, int):
val = prop_type(split[cols])
print(prop, " = ", val)