diff --git a/README.md b/README.md index 8ab994d..5db3da9 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ ## Limitations * Only handles files formatted as CSV. -* CSVs must actually use commas. Other deliminators not yet supported. +* ~~CSVs must actually use commas. Other deliminators not yet supported.~~ * Files must have the extension `.csv` or `.txt`. -* Headers not yet supported. +* ~~Headers not yet supported.~~ * Currently only creates line graphs. diff --git a/operator_file_import.py b/operator_file_import.py index 333cc39..d11b39b 100644 --- a/operator_file_import.py +++ b/operator_file_import.py @@ -50,19 +50,20 @@ class ImportSomeData(Operator, ImportHelper): default=True, ) - deliminator: EnumProperty( - name="Deliminator", - description="Choose deliminator used in file (typically a comma)", + delimiter: EnumProperty( + name="Delimiter", + description="Choose delimiter used in file (typically a comma)", items=( - ("comma", ", Comma", "Items seperated by comma"), - ("semi", "; Semicolon", "Items seperated by semicolon"), - ("NONE", "None", "None"), + (",", ", Comma", "Items seperated by comma"), + (";", "; Semicolon", "Items seperated by semicolon"), ), - default="comma", + default=",", ) def execute(self, context): self.csv_textdata = bpy.data.texts.load(self.filepath, internal=True) + self.csv_textdata["has_headers"] = self.has_headers + self.csv_textdata["delimiter"] = self.delimiter new_plot = plot.NewPlot new_plot().execute( @@ -70,7 +71,7 @@ def execute(self, context): csv_textdata = self.csv_textdata, filepath = self.filepath, has_headers = self.has_headers, - deliminator = self.deliminator + #delimiter = self.delimiter ) #return read_some_data(context, self.filepath, self.use_setting) return {"FINISHED"} diff --git a/plot.py b/plot.py index 701f6d4..9e9c577 100644 --- a/plot.py +++ b/plot.py @@ -3,15 +3,20 @@ # TODO: check if new data same as old, # if so, don't go through update process -def convertData(csv_textdata): +def convertData(csv_textdata, entry_delimiter=",", has_headers=True): from io import StringIO import csv print("converting data") raw_data = csv_textdata.as_string() - reader = csv.reader(StringIO(raw_data)) # read csv string as csv file + reader = csv.reader(StringIO(raw_data), delimiter=entry_delimiter) # read csv string as csv file + if(has_headers): + headers = next(reader) + print("headers: {}".format(headers)) + else: + headers = None string_list = list(reader) pos_list = [list(map(float, x)) for x in string_list] # convert list of strings to list of floats - return pos_list + return pos_list, headers class NewPlot: @@ -25,7 +30,7 @@ class NewPlot: crv = None spline = None has_header = False - deliminator = None + delimiter = None def execute(self, **args): # execute() is called when running the operator. #self.report({"ERROR"}, "error mes") @@ -34,11 +39,16 @@ def execute(self, **args): # execute() is called when running the operato self.csv_textdata = args.get("csv_textdata") self.filepath= args.get("filepath") self.has_headers = args.get("has_headers") - self.deliminator = args.get("deliminator") - self.pos_list = convertData(self.csv_textdata) + #self.delimiter = args.get("delimiter") + self.pos_list, self.headers = convertData(self.csv_textdata, self.csv_textdata['delimiter'], self.has_headers) + + if self.obj is None: print("no class obj") self.create_obj() + if(self.has_headers): + print("x-axis: {}, y-axis: {}".format(self.headers[0], self.headers[1])) + self.create_axis_text() self.create_curve(self.pos_list) else: print("class obj found: {}".format(self.obj)) @@ -46,6 +56,23 @@ def execute(self, **args): # execute() is called when running the operato return {'FINISHED'} + def create_axis_text(self): + xaxis_crv = bpy.data.curves.new(type="FONT",name="xAxisCrv") + xaxis_crv.offset_x = 2 + xaxis_crv.offset_y = -1 + xaxis_obj = bpy.data.objects.new("xAxisObj", xaxis_crv) + xaxis_obj.data.body = self.headers[0] + xaxis_obj.parent = self.root + bpy.data.scenes[0].collection.objects.link(xaxis_obj) + + yaxis_crv = bpy.data.curves.new(type="FONT",name="yAxisCrv") + yaxis_crv.offset_x = -2 + yaxis_crv.offset_y = 2 + yaxis_obj = bpy.data.objects.new("yAxisObj", yaxis_crv) + yaxis_obj.data.body = self.headers[1] + yaxis_obj.parent = self.root + bpy.data.scenes[0].collection.objects.link(yaxis_obj) + def create_curve(self, coords_list): print("create curve") spline = self.spline @@ -56,6 +83,9 @@ def create_curve(self, coords_list): def create_obj(self): print("create new obj") + + self.root = bpy.data.objects.new("rockplot_root", None) + self.root.empty_display_type = "ARROWS" crv = bpy.data.curves.new('crv', 'CURVE') crv.dimensions = '2D' crv.plotrock_type="plot" @@ -63,7 +93,9 @@ def create_obj(self): self.crv = crv self.spline = spline self.obj = bpy.data.objects.new('object_name', crv) + self.obj.parent = self.root bpy.data.scenes[0].collection.objects.link(self.obj) + bpy.data.scenes[0].collection.objects.link(self.root) class UpdatePlot(bpy.types.Operator): bl_idname = "plotrock.update_plot" @@ -98,8 +130,10 @@ def execute(self, context): self.crv = self.obj.data self.spline = self.crv.splines[0] self.csv_textdata = self.crv.plotrock_csv + self.delimiter = self.csv_textdata['delimiter'] + self.has_headers = self.csv_textdata['has_headers'] - self.pos_list = convertData(self.csv_textdata) + self.pos_list, self.headers = convertData(self.csv_textdata, self.delimiter, self.has_headers) self.update_curve() return {"FINISHED"}