Skip to content

Commit

Permalink
Merge pull request #2 from theTaikun/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
theTaikun authored Jul 31, 2021
2 parents 431afef + b8ef0ab commit ee6ed1c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
17 changes: 9 additions & 8 deletions operator_file_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,28 @@ 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(
context = 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"}
Expand Down
48 changes: 41 additions & 7 deletions plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
Expand All @@ -34,18 +39,40 @@ 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))
self.update_curve()

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
Expand All @@ -56,14 +83,19 @@ 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"
spline = crv.splines.new(type='POLY')
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"
Expand Down Expand Up @@ -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"}
Expand Down

0 comments on commit ee6ed1c

Please sign in to comment.