-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_sailorpoints.py
More file actions
254 lines (191 loc) · 7.16 KB
/
export_sailorpoints.py
File metadata and controls
254 lines (191 loc) · 7.16 KB
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
from math import sqrt, ceil
import struct
import time
import re
import sys
import cProfile
import os
import bmesh
import bpy
from mathutils import Vector, Matrix
from collections import defaultdict
from bpy.props import BoolProperty, EnumProperty, StringProperty
from bpy.types import Operator
from bpy_extras.io_utils import ExportHelper, axis_conversion
sys.setrecursionlimit(10000)
bl_info = {
"name": "SeaDogs SailorPoints export",
"description": "Export SailorPoints files",
"author": "Wazar",
"version": (2, 2, 0),
"blender": (3, 6, 0),
"location": "File > Export",
"warning": "",
"support": "COMMUNITY",
"category": "Export",
}
correction_export_matrix = axis_conversion(
from_forward='Y', from_up='Z', to_forward='X', to_up='Y')
point_types = {
'normal': 0,
'cannonl': 1,
'cannonr': 2,
'cannonf': 3,
'cannonb': 4,
'mast1': 5,
'mast2': 6,
'mast3': 7,
'mast4': 8,
'mast5': 9,
'nottarget': 10
}
point_types_r = {
0: 'normal',
1: 'cannonl',
2: 'cannonr',
3: 'cannonf',
4: 'cannonb',
5: 'mast1',
6: 'mast2',
7: 'mast3',
8: 'mast4',
9: 'mast5' ,
10:'nottarget'
}
def remove_blender_name_postfix(name):
return re.sub(r'\.\d{3}', '', name)
def convert_coordinate_from_ini(coord):
return (coord[2], -coord[0], coord[1])
def convert_coordinate_to_ini(coord):
return (-coord[1], coord[2], coord[0])
class Link:
def __init__(self, idx, points):
self.points = points
self.idx = idx
class Point:
def __init__(self, idx, matrix, point_type):
self.matrix = matrix
self.point_type = point_type
self.idx = idx
class SailorPoints:
def __init__(self):
self.points = []
self.links = []
def generate(self, file_path='', report_func=None):
for i in range(len(self.points)):
if self.points[i] is None:
report_func({'ERROR'}, 'point with number "{}" not found'.format(i))
return {'CANCELLED'}
for i in range(len(self.links)):
if self.links[i] is None:
report_func({'ERROR'}, 'link with number "{}" not found'.format(i))
return {'CANCELLED'}
with open(file_path, 'w') as file:
file.write('[SIZE]\n')
file.write('points = {}\n'.format(len(self.points)))
file.write('links = {}\n'.format(len(self.links)))
file.write('\n')
file.write('[POINT_DATA]\n')
for i in range(len(self.points)):
matrix = correction_export_matrix.to_4x4() @ self.points[i].matrix
matrix.translation *= Vector([-1, 1, 1])
vec = matrix.translation
file.write('point {} = {:.6f},{:.6f},{:.6f},{}\n'.format(i, vec[0], vec[1], vec[2], point_types[self.points[i].point_type]))
file.write('\n')
file.write('[LINK_DATA]\n')
for i in range(len(self.links)):
file.write('link {} = {},{}\n'.format(i, self.links[i].points[0], self.links[i].points[1]))
return None
def export_sailorpoints(context, file_path="", report_func=None):
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
root = bpy.context.view_layer.objects.active
root_children = root.children
collection = root.users_collection[0]
points = []
# TODO get list of childrens children
for child in root_children:
if child.type == 'EMPTY' and child.name == 'points':
locator = child
for child in locator.children:
if child.type == 'EMPTY':
points.append(child)
break
bpy.context.scene.frame_set(0)
bpy.context.scene.cursor.location = root.location
bpy.context.view_layer.objects.active = root
sp = SailorPoints()
sp.points = [None] * len(points)
loc_to_point = {}
num = 0
for point in points:
label_name = remove_blender_name_postfix(point.name)
label_m = Matrix(point.matrix_world)
parts = label_name.split('_')
if parts[0] not in point_types:
report_func({'ERROR'}, 'unknown point type "{}"'.format(parts[0]))
return {'CANCELLED'}
sp.points[num] = Point(num, label_m, parts[0])
loc_to_point[point.name] = sp.points[num]
num += 1
links = []
num = 0
for point in points:
for con in point.constraints[:]:
if con.type != "TRACK_TO":
continue
link_name = remove_blender_name_postfix(con.name)
target = con.target
if target is None:
report_func({'ERROR'}, 'wrong link "{}"'.format(con.name))
return {'CANCELLED'}
if point.name not in loc_to_point:
report_func({'ERROR'}, 'wrong point "{}"'.format(point.name))
return {'CANCELLED'}
if target.name not in loc_to_point:
report_func({'ERROR'}, 'wrong point "{}"'.format(target.name))
return {'CANCELLED'}
links.append(Link(num, [loc_to_point[point.name].idx, loc_to_point[target.name].idx]))
num += 1
links.sort(key=lambda l: l.idx)
for i in range(len(links)):
if i != links[i].idx:
report_func({'ERROR'}, 'link with number "{}" not found'.format(i))
return {'CANCELLED'}
sp.links = links
ret = sp.generate(file_path, report_func)
if ret is not None:
return {'CANCELLED'}
print('\nSailorPoints Export finished successfully!')
return {'FINISHED'}
class ExportSailorPoints(Operator, ExportHelper):
"""This appears in the tooltip of the operator and in the generated docs"""
bl_idname = "export.sailorpoints"
bl_label = "Export sailorpoints"
# ExportHelper mixin class uses this
filename_ext = ".ini"
filter_glob: StringProperty(
default="*.ini",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
def invoke(self, context, event):
selected_object = context.view_layer.objects.active
if selected_object:
collection = selected_object.users_collection[0]
directory = os.path.dirname(self.filepath)
new_filename = remove_blender_name_postfix(collection.name) + self.filename_ext
self.filepath = os.path.join(directory, new_filename)
return super().invoke(context, event)
def execute(self, context):
return export_sailorpoints(context, self.filepath, report_func=self.report)
def menu_func_export(self, context):
self.layout.operator(ExportSailorPoints.bl_idname,
text="SailorPoints Export(.ini)")
def register():
bpy.utils.register_class(ExportSailorPoints)
bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
def unregister():
bpy.utils.unregister_class(ExportSailorPoints)
bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)
if __name__ == "__main__":
register()