forked from geforcefan/BlenderNoLimitsCSVImporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_import_nolimits_csv.py
290 lines (218 loc) · 8.53 KB
/
io_import_nolimits_csv.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
# <pep8 compliant>
from contextvars import Token
TOOL_NAME = "NoLimits 2 Professional Track Data (.csv)"
bl_info = {
"name": "NoLimits 2 Professional Track Data (.csv)",
"author": "Daniel Hilpert",
"version": (3, 0, 1),
"blender": (2, 80, 0),
"location": "File > Import > NoLimits 2 Professional Track Data (.csv)",
"description": "Generates a curve object from NoLimits 2 Roller Coaster "
"Simulation Professional CSV data",
"wiki_url": "https://github.com/bestdani/BlenderNoLimitsCSVImporter",
"category": "Import-Export"
}
import csv
import pathlib
import bpy
import mathutils
from bpy.props import StringProperty, IntProperty, BoolProperty
from bpy.types import Operator
from bpy_extras.io_utils import ImportHelper, ExportHelper
TO_BLENDER_COORDINATES = mathutils.Matrix(
((1.0, 0.0, 0.0),
(0.0, 0.0, -1.0),
(0.0, 1.0, 0.0))
)
def get_vertices_from_csv(file_path):
vertices = []
with open(file_path, 'r') as csv_file:
treader = csv.reader(csv_file, delimiter='\t', quotechar='|')
for row in treader:
try:
vertices.append(
{
'pos': mathutils.Vector(
(float(row[1]), float(row[2]), float(row[3]))
),
'front': mathutils.Vector(
(float(row[4]), float(row[5]), float(row[6]))
),
'left': mathutils.Vector(
(float(row[7]), float(row[8]), float(row[9]))
),
'up': mathutils.Vector(
(float(row[10]), float(row[11]), float(row[12]))
)
}
)
except ValueError:
continue
return vertices
def apply_vertex_positions(spline, vertices):
for i, vertex in enumerate(vertices):
new_point = spline.points[i]
new_point.co = (TO_BLENDER_COORDINATES @ vertex['pos']).to_4d()
def create_tmp_reader(context, target_object):
reader = bpy.data.objects.new('tmp_curve_reader', None)
reader.empty_display_type = 'ARROWS'
context.layer_collection.collection.objects.link(reader)
constraint = reader.constraints.new('FOLLOW_PATH')
constraint.target = target_object
constraint.use_curve_follow = True
constraint.use_fixed_location = True
return constraint, reader
def apply_tilt_values(context, vertices, target_object, spline_points):
point_count = len(vertices)
largest_index = point_count - 1
constraint, reader = create_tmp_reader(context, target_object)
for i in range(point_count):
constraint.offset_factor = i / largest_index
dg = bpy.context.evaluated_depsgraph_get()
bpy.context.scene.frame_current = 1
eval_reader = reader.evaluated_get(dg)
matrix = eval_reader.matrix_world.copy()
evaluated_up = matrix.col[2].xyz
expected_up = TO_BLENDER_COORDINATES @ vertices[i]['up']
expected_forward = TO_BLENDER_COORDINATES @ vertices[i]['front']
calculated_forward = evaluated_up.cross(expected_up)
forward_direction = calculated_forward @ expected_forward
tilt_angle = expected_up.angle(evaluated_up)
if forward_direction < 0:
tilt_angle *= -1
spline_points[i].tilt = tilt_angle
bpy.data.objects.remove(reader, do_unlink=True)
def create_empties(context, name: str, vertices, parent_object):
collection = bpy.data.collections.new(name)
context.scene.collection.children.link(collection)
for vertex in vertices:
obj = bpy.data.objects.new(name, None)
obj.empty_display_type = 'ARROWS'
matrix_nl2 = mathutils.Matrix().to_3x3()
matrix_nl2.col[0] = vertex['left']
matrix_nl2.col[1] = vertex['up']
matrix_nl2.col[2] = vertex['front']
matrix_blender = (TO_BLENDER_COORDINATES @ matrix_nl2).to_4x4()
matrix_blender.col[3] = (
TO_BLENDER_COORDINATES @ vertex['pos']).to_4d()
obj.matrix_world = matrix_blender
collection.objects.link(obj)
def add_curve_from_csv(context, file_path: str, import_raw_points: bool):
file_path = pathlib.Path(file_path)
name = file_path.stem
vertices = get_vertices_from_csv(file_path)
curve_data = bpy.data.curves.new(name, 'CURVE')
curve_data.twist_mode = 'MINIMUM'
curve_data.dimensions = '3D'
spline = curve_data.splines.new('POLY')
spline.resolution_u = 1
spline.tilt_interpolation = 'LINEAR'
spline.points.add(len(vertices) - 1)
apply_vertex_positions(spline, vertices)
curve_object = bpy.data.objects.new(name + " Object", curve_data)
curve_object.location = (0, 0, 0)
if import_raw_points:
create_empties(context, name + " Raw", vertices, curve_object)
context.scene.collection.objects.link(curve_object)
apply_tilt_values(context, vertices, curve_object, spline.points)
return {'FINISHED'}
def sample_curve_as_csv(context, file_path, point_count):
file_path = pathlib.Path(file_path)
curve = context.active_object
if not curve or curve.type != 'CURVE':
return {'CANCELLED'}
point_count = len(curve.data.splines[0].points) if point_count == 0 \
else point_count
largest_index = point_count - 1
constraint, reader = create_tmp_reader(context, curve)
matrices = []
for i in range(point_count):
offset = i / largest_index
constraint.offset_factor = offset
dg = bpy.context.evaluated_depsgraph_get()
bpy.context.scene.frame_current = 1
eval_reader = reader.evaluated_get(dg)
matrices.append(eval_reader.matrix_world.copy())
bpy.data.objects.remove(reader, do_unlink=True)
csv_header = '"No."\t"PosX"\t"PosY"\t"PosZ"\t' \
'"FrontX"\t"FrontY"\t"FrontZ"\t' \
'"LeftX"\t"LeftY"\t"LeftZ"\t' \
'"UpX"\t"UpY"\t"UpZ"'
csv_rows = [csv_header]
for i, m in enumerate(matrices):
pos = m.col[3]
up = m.col[2]
csv_rows.append(
f'{i + 1}\t{pos.x}\t{pos.z}\t{-pos.y}'
f'\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t'
f'{up.x}\t{up.z}\t{-up.y}'
)
csv_content = '\n'.join(csv_rows)
with open(file_path.with_suffix('.csv'), 'w') as f:
f.write(csv_content)
return {'FINISHED'}
class ImportNl2Csv(Operator, ImportHelper):
"""Imports coaster track splines as a curve"""
bl_idname = "import_nl.csv_data"
bl_label = TOOL_NAME
filename_ext = ".csv"
filter_glob: StringProperty(
default="*.csv",
options={'HIDDEN'},
maxlen=255,
)
import_raw_points: BoolProperty(
default=False,
name="Import Raw Points (slow!)",
description="Imports the raw points as empties. Attention, this can take several minutes for many vertices!",
)
def execute(self, context):
return add_curve_from_csv(
context, self.filepath, self.import_raw_points
)
class ExportNl2Csv(Operator, ExportHelper):
"""Exports the active curve as a NoLimits 2 Professional compatible CSV
file"""
bl_idname = "export_nl.csv_data"
bl_label = TOOL_NAME
filename_ext = ".csv"
point_count: IntProperty(
name="Point Count",
default=0,
min=0,
)
filter_glob: StringProperty(
default="*.csv",
options={'HIDDEN'},
maxlen=255,
)
def execute(self, context):
result = sample_curve_as_csv(context, self.filepath, self.point_count)
if result != {'FINISHED'}:
self.report(
{'ERROR_INVALID_CONTEXT'}, 'No valid curve object selected'
)
return result
def menu_func_import(self, context):
self.layout.operator(
ImportNl2Csv.bl_idname,
text=ImportNl2Csv.bl_label
)
def menu_func_export(self, context):
self.layout.operator(
ExportNl2Csv.bl_idname,
text=ExportNl2Csv.bl_label
)
def register():
bpy.utils.register_class(ImportNl2Csv)
bpy.types.TOPBAR_MT_file_import.append(menu_func_import)
bpy.utils.register_class(ExportNl2Csv)
bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
def unregister():
bpy.utils.unregister_class(ImportNl2Csv)
bpy.types.TOPBAR_MT_file_import.remove(menu_func_import)
bpy.utils.unregister_class(ExportNl2Csv)
bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)
if __name__ == "__main__":
register()
bpy.ops.import_nl.csv_data('INVOKE_DEFAULT')