From 7fc4b79ab48b7658f846648fc2a30b1dcd9df9fa Mon Sep 17 00:00:00 2001 From: theTaikun <43371347+theTaikun@users.noreply.github.com> Date: Sat, 31 Jul 2021 10:44:23 -0400 Subject: [PATCH 1/2] Addd axis labels and ability to choose delimiter axis labels based on header of csv file if csv has header, or semicolon deliminator, must be specified on import --- README.md | 4 ++-- operator_file_import.py | 17 +++++++++-------- plot.py | 41 ++++++++++++++++++++++++++++++++++------- 3 files changed, 45 insertions(+), 17 deletions(-) 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..71500be 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,21 @@ 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] + 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] + bpy.data.scenes[0].collection.objects.link(yaxis_obj) + def create_curve(self, coords_list): print("create curve") spline = self.spline @@ -98,8 +123,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"} From b8ef0ab3f3de4fe975309bb45f7885d8a9e25347 Mon Sep 17 00:00:00 2001 From: theTaikun <43371347+theTaikun@users.noreply.github.com> Date: Sat, 31 Jul 2021 11:19:18 -0400 Subject: [PATCH 2/2] Add Empty as the parent of all plot elements Can be used to move/rotate entire plot --- plot.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/plot.py b/plot.py index 71500be..9e9c577 100644 --- a/plot.py +++ b/plot.py @@ -62,6 +62,7 @@ def create_axis_text(self): 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") @@ -69,6 +70,7 @@ def create_axis_text(self): 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): @@ -81,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" @@ -88,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"