-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot.py
349 lines (290 loc) · 13.1 KB
/
plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import bpy
# TODO: check if new data same as old,
# if so, don't go through update process
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), delimiter=entry_delimiter) # read csv string as csv file
print("READER: {}".format(reader))
if(has_headers):
headers = next(reader)
print("headers: {}".format(headers))
else:
headers = None
string_list = list(reader)
print("string_list: {}".format(string_list))
pos_list = [i for i in sanitizeData(string_list)]
print("pos_list: {}".format(pos_list))
return pos_list, headers
def sanitizeData(data):
for i in data:
try:
x = list(map(float, i))
yield x
except:
pass
# findRoot function courtesy of MMDTools addon
def findRoot(obj):
if obj:
if obj.plotrock_type == 'ROOT':
return obj
return findRoot(obj.parent)
return None
class NewPlot:
""""
bl_idname = "plotrock.plot"
bl_label = "Create Simple Plot" # Display name in the interface.
bl_options = {'REGISTER', 'UNDO'} # Enable undo for the operator.
"""
obj = None
crv = None
spline = None
has_header = False
delimiter = None
def execute(self, **args): # execute() is called when running the operator.
#self.report({"ERROR"}, "error mes")
#self.report({"INFO"}, "info mes")
print("plotting")
self.csv_textdata = args.get("csv_textdata")
self.filepath= args.get("filepath")
self.has_headers = args.get("has_headers")
#self.delimiter = args.get("delimiter")
self.pos_list, self.headers = convertData(self.csv_textdata, self.csv_textdata['delimiter'], self.has_headers)
if self.pos_list is None:
return {'FINISHED'}
if self.obj is None:
print("no class obj")
self.create_obj()
self.create_curve(self.pos_list)
self.center_curve(self.obj)
if(self.has_headers):
print("x-axis: {}, y-axis: {}".format(self.headers[0], self.headers[1]))
self.create_axis_text()
else:
print("class obj found: {}".format(self.obj))
self.update_curve()
return {'FINISHED'}
def center_curve(self, curv_obj):
driver_x = curv_obj.driver_add("location", 0)
driver_x.driver.expression = "-offset if use_min_x else 0"
var = driver_x.driver.variables.new()
var.name = "offset"
var.targets[0].id = self.root
var.targets[0].data_path = "plotrock_settings.min_x"
var2 = driver_x.driver.variables.new()
var2.name = "use_min_x"
var2.targets[0].id = self.root
var2.targets[0].data_path = "plotrock_settings.use_min_x"
driver_y = curv_obj.driver_add("location", 1)
driver_y.driver.expression = "-offset if use_min_y else 0"
var = driver_y.driver.variables.new()
var.name = "offset"
var.targets[0].id = self.root
var.targets[0].data_path = "plotrock_settings.min_y"
var2 = driver_y.driver.variables.new()
var2.name = "use_min_y"
var2.targets[0].id = self.root
var2.targets[0].data_path = "plotrock_settings.use_min_y"
def create_axis_text(self):
xaxis_crv = bpy.data.curves.new(type="FONT",name="xAxisCrv")
xaxis_crv.align_x = "CENTER"
xaxis_crv.align_y = "CENTER"
xaxis_obj = bpy.data.objects.new("xAxisObj", xaxis_crv)
xaxis_obj.data.body = self.headers[0]
xaxis_obj.parent = self.root
xaxis_obj.location[0] = self.root.plotrock_settings.max_x/2
xaxis_obj.location[1] = -1
xaxis_obj.lock_location[2] = True
bpy.data.scenes[0].collection.objects.link(xaxis_obj)
self.root.plotrock_settings.x_axis_label = xaxis_obj
yaxis_crv = bpy.data.curves.new(type="FONT",name="yAxisCrv")
yaxis_crv.align_x = "CENTER"
yaxis_crv.align_y = "CENTER"
yaxis_obj = bpy.data.objects.new("yAxisObj", yaxis_crv)
yaxis_obj.data.body = self.headers[1]
yaxis_obj.parent = self.root
yaxis_obj.location[0] = -2
yaxis_obj.location[1] = self.root.plotrock_settings.max_y/2
yaxis_obj.lock_location[2] = True
bpy.data.scenes[0].collection.objects.link(yaxis_obj)
self.root.plotrock_settings.y_axis_label = yaxis_obj
def create_curve(self, coords_list):
print("create curve")
spline = self.spline
spline.points.add(len(coords_list) -1 )
for i, val in enumerate(coords_list):
spline.points[i].co = (val + [0.0] + [1.0]) # where val is [x, y], followed by + [z] + [w]
# Size of Empty same for all axis => set as max of x and y value
min_x = min(coords_list)[0]
min_y = min(coords_list, key=lambda x: x[1])[1]
max_x = max(coords_list)[0] # Max of nested list checks first val
max_y = max(coords_list, key=lambda x: x[1])[1] # Funct to check max by second val, and return that val
self.root.empty_display_size = max(max_x, max_y) # compares the 2 maxes
self.root.plotrock_settings.min_x = min_x
self.root.plotrock_settings.min_y = min_y
self.root.plotrock_settings.max_x = max_x
self.root.plotrock_settings.max_y = max_y
self.crv.plotrock_csv = self.csv_textdata
def create_obj(self):
print("create new obj")
self.root = bpy.data.objects.new("rockplot_root", None)
self.root.empty_display_type = "ARROWS"
self.root.plotrock_type = "ROOT"
crv = bpy.data.curves.new('crv', 'CURVE')
crv.dimensions = '2D'
crv.extrude = 0.5
crv.bevel_depth = 0.05
crv.plotrock_type="plot"
spline = crv.splines.new(type='POLY')
spline.use_smooth = False
self.crv = crv
self.spline = spline
self.obj = bpy.data.objects.new('plot', crv)
self.obj.location[2] = 0.5
self.obj.parent = self.root
bpy.data.scenes[0].collection.objects.link(self.obj)
bpy.data.scenes[0].collection.objects.link(self.root)
self.grid = self.create_grid()
self.grid.parent = self.root
bpy.data.scenes[0].collection.objects.link(self.grid)
def create_grid(self):
gridGeo = bpy.data.node_groups.new("gridNodeTree", "GeometryNodeTree")
gridMesh = bpy.data.meshes.new("gridMesh")
gridObj = bpy.data.objects.new("gridObj", gridMesh )
geoModifier = gridObj.modifiers.new("gridGeoNodes", "NODES")
geoModifier.node_group = gridGeo
wireModifier = gridObj.modifiers.new("gridWire", "WIREFRAME")
wireModifier.thickness = 0.125
nodes = gridGeo.nodes
gridGeo.inputs.new("NodeSocketGeometry", "Geometry")
gridGeo.outputs.new("NodeSocketGeometry", "Geometry")
input_node = nodes.new("NodeGroupInput")
input_node.location.x = -700 - input_node.width
input_node.location.y = -100
output_node = nodes.new("NodeGroupOutput")
output_node.is_active_output = True
output_node.location.x = 200
group_input_size= gridGeo.inputs.new("NodeSocketVector", "Size")
group_input_size.default_value=[10,10,0]
# TODO: allow both driver and user modifiable
group_input_size.description = "Custom Grid Size. TEMPORARILY DISABLED"
node = nodes.new("NodeReroute")
node.name = "SIZE_SPLITTER"
node.location.x = -600
node=nodes.new("FunctionNodeInputVector")
node.name = "DRIVER_SIZE"
node.location.x = -700 - node.width
node.location.y = 100
fcurve = node.driver_add("vector")
var = fcurve[0].driver.variables.new()
fcurve[0].driver.expression = "var"
var.targets[0].id = self.root
var.targets[0].data_path = "plotrock_settings.max_x"
var = fcurve[1].driver.variables.new()
fcurve[1].driver.expression = "var"
var.targets[0].id = self.root
var.targets[0].data_path = "plotrock_settings.max_y"
node = nodes.new("GeometryNodeMeshGrid")
node.location.x = -100 - node.width
node.location.y = 200
node.name = "MESH_GRID"
node = nodes.new("GeometryNodeTransform")
node.name = "XLATE_XFORM"
node = nodes.new("ShaderNodeVectorMath")
node.name = "DIV_BY_TWO"
node.location.x = -50 - node.width
node.location.y = -100
node.operation = "DIVIDE"
node.inputs[1].default_value=[2,2,1]
node = nodes.new("ShaderNodeVectorMath")
node.name = "ADD_GRID_LINE"
node.location.x = -450 - node.width
node.location.y = 200
node.operation = "ADD"
node.inputs[1].default_value=[1,1,0]
node = nodes.new("ShaderNodeSeparateXYZ")
node.name = "SepXYZ_size"
node.location.x = -275 - node.width
node.location.y = 250
node = nodes.new("ShaderNodeSeparateXYZ")
node.name = "SepXYZ_verts"
node.location.x = -275 - node.width
node.location.y = 100
# Choose between user-editable modifier input, and automatic driver
#size_input = input_node.outputs[1]
size_input = nodes["DRIVER_SIZE"].outputs[0]
gridGeo.links.new(nodes["SIZE_SPLITTER"].inputs[0], size_input)
gridGeo.links.new(output_node.inputs[0], nodes["XLATE_XFORM"].outputs[0])
gridGeo.links.new(nodes["XLATE_XFORM"].inputs["Geometry"], nodes["MESH_GRID"].outputs[0])
gridGeo.links.new(nodes["XLATE_XFORM"].inputs[1], nodes["DIV_BY_TWO"].outputs[0])
gridGeo.links.new(nodes["SepXYZ_size"].inputs[0], nodes["SIZE_SPLITTER"].outputs[0])
gridGeo.links.new(nodes["DIV_BY_TWO"].inputs[0], nodes["SIZE_SPLITTER"].outputs[0])
gridGeo.links.new(nodes["ADD_GRID_LINE"].inputs[0], nodes["SIZE_SPLITTER"].outputs[0])
gridGeo.links.new(nodes["MESH_GRID"].inputs[0], nodes["SepXYZ_size"].outputs[0])
gridGeo.links.new(nodes["MESH_GRID"].inputs[1], nodes["SepXYZ_size"].outputs[1])
gridGeo.links.new(nodes["MESH_GRID"].inputs[2], nodes["SepXYZ_verts"].outputs[0])
gridGeo.links.new(nodes["MESH_GRID"].inputs[3], nodes["SepXYZ_verts"].outputs[1])
gridGeo.links.new(nodes["SepXYZ_verts"].inputs[0], nodes["ADD_GRID_LINE"].outputs[0])
return gridObj
class UpdatePlot(bpy.types.Operator):
bl_idname = "plotrock.update_plot"
bl_label = "Update Plot" # Display name in the interface.
bl_options = {'REGISTER', 'UNDO'} # Enable undo for the operator.
@classmethod
def poll(cls, context):
obj = context.active_object
# Need to check not None otherwise selecting nothing will result in True
return obj is not None and obj.type == "CURVE" and obj.data.plotrock_type == "plot"
def update_curve(self):
print("updating curve")
# num point length is diff than current spline
if len(self.spline.points) != len(self.pos_list):
print("point length diff")
self.crv.splines.remove(self.spline)
new_spline = self.crv.splines.new(type='POLY')
new_spline.points.add(len(self.pos_list) -1 )
self.spline = new_spline
else:
print("point length same")
spline = self.spline
for i, val in enumerate(self.pos_list):
print("updating point {}".format(i))
spline.points[i].co= (val + [0.0] + [1.0])
def update_axis(self):
coords_list = self.pos_list
# Size of Empty same for all axis => set as max of x and y value
min_x = min(coords_list)[0] # Max of nested list checks first val
min_y = min(coords_list, key=lambda x: x[1])[1] # Funct to check max by second val, and return that val
max_x = max(coords_list)[0] # Max of nested list checks first val
max_y = max(coords_list, key=lambda x: x[1])[1] # Funct to check max by second val, and return that val
self.root.empty_display_size = max(max_x, max_y) # compares the 2 maxes
self.root.plotrock_settings.min_x = min_x
self.root.plotrock_settings.min_y = min_y
self.root.plotrock_settings.max_x = max_x
self.root.plotrock_settings.max_y = max_y
def execute(self, context):
print("updating")
self.obj = context.active_object
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.root = findRoot(self.obj)
self.pos_list, self.headers = convertData(self.csv_textdata, self.delimiter, self.has_headers)
if self.pos_list is not None:
self.update_curve()
self.update_axis()
return {"FINISHED"}
def register():
#bpy.utils.register_class(Plot)
bpy.utils.register_class(UpdatePlot)
print("Hello World")
def unregister():
#bpy.utils.unregister_class(Plot)
bpy.utils.unregister_class(UpdatePlot)
print("Goodbye World")
if __name__ == "__main__":
register()