-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_as_array.py
More file actions
340 lines (292 loc) · 10.9 KB
/
Copy pathexport_as_array.py
File metadata and controls
340 lines (292 loc) · 10.9 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
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
# Copyright (C) 2025 Timotej Milcak
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
bl_info = {
"name": "Export as array",
"description": "Exports selected object as an array",
"author": "Timotej Milcak",
"blender": (2, 80, 0),
"location": "Object > Export as array",
"warning": "WIP",
"wiki_url": "",
"tracker_url": "",
"support": "COMMUNITY",
"category": "Export"
}
import bpy
import bmesh
from bpy.props import *
class Profile():
def __init__(self,
name,
vert_suffix, index_suffix,
array_begin, array_delim, array_end,
object_begin, object_delim, object_end):
self.name = name
self.vert_suffix = vert_suffix
self.index_suffix = index_suffix
self.array_begin = array_begin
self.array_delim = array_delim
self.array_end = array_end
self.object_begin = object_begin
self.object_delim = object_delim
self.object_end = object_end
profiles = {
"profile_empty": Profile("Empty", "", "", "", "", "", "", "", ""),
"profile_c": Profile("C", "f", "", "{", ",", "};", "{", ",", "}"),
"profile_py": Profile("Python", "", "", "[", ",", "]", "[", ",", "]")
}
def generate_profile_items(self, context):
result = []
for key, value in profiles.items():
result.append((key, value.name, ""))
return result
def get_selected_mesh(self, context):
selected_objects = bpy.context.selected_objects
selected_objects_count = len(selected_objects)
if selected_objects_count == 1:
active_object = selected_objects[0]
if active_object is not None and active_object.type == "MESH":
return active_object.to_mesh()
else:
self.report({"ERROR"}, "Invalid object selected!")
return None
else:
self.report({"ERROR"}, "Select precisely one object!")
return None
last_profile = "profile_empty"
def change_profile(self, context):
global last_profile
changed_to = self.profiles_enum
if last_profile == changed_to:
return
storage = bpy.context.scene.export_as_array_properties
profiles[last_profile].vert_suffix = storage.vert_suffix
profiles[last_profile].index_suffix = storage.index_suffix
profiles[last_profile].array_begin = storage.array_begin
profiles[last_profile].array_delim = storage.array_delim
profiles[last_profile].array_end = storage.array_end
profiles[last_profile].object_begin = storage.object_begin
profiles[last_profile].object_delim = storage.object_delim
profiles[last_profile].object_end = storage.object_end
last_profile = changed_to
storage.vert_suffix = profiles[changed_to].vert_suffix
storage.index_suffix = profiles[changed_to].index_suffix
storage.array_begin = profiles[changed_to].array_begin
storage.array_delim = profiles[changed_to].array_delim
storage.array_end = profiles[changed_to].array_end
storage.object_begin = profiles[changed_to].object_begin
storage.object_delim = profiles[changed_to].object_delim
storage.object_end = profiles[changed_to].object_end
class ExportAsArrayProperties(bpy.types.PropertyGroup):
vert_suffix: StringProperty()
index_suffix: StringProperty()
array_begin: StringProperty()
array_delim: StringProperty()
array_end: StringProperty()
object_begin: StringProperty()
object_delim: StringProperty()
object_end: StringProperty()
profiles_enum: EnumProperty(
name="Profile",
update=change_profile,
items=generate_profile_items,
)
export_mode_enum: EnumProperty(
name="Export mode",
items=[
("export_mode_verts_plus_faces", "Vertices + faces", ""),
("export_mode_verts_only", "Vertices only", ""),
("export_mode_faces_only", "Faces only", ""),
("export_mode_verts_per_face", "Vertices (per face)", ""),
]
)
def format_vertices(vertices):
storage = bpy.context.scene.export_as_array_properties
vert_suffix = storage.vert_suffix
object_begin = storage.object_begin
object_delim = storage.object_delim
object_end = storage.object_end
array_begin = storage.array_begin
array_delim = storage.array_delim
array_end = storage.array_end
vertex_lines = [
(
f"\t{object_begin} {v.co.x}{vert_suffix}{object_delim} "
f"{v.co.y}{vert_suffix}{object_delim} "
f"{v.co.z}{vert_suffix} "
f"{object_end}"
)
for v in vertices
]
return (
f"verts = {array_begin}\n" +
f"{array_delim}\n".join(vertex_lines) +
f"\n{array_end}"
)
def format_faces(faces):
storage = bpy.context.scene.export_as_array_properties
index_suffix = storage.index_suffix
object_begin = storage.object_begin
object_delim = storage.object_delim
object_end = storage.object_end
array_begin = storage.array_begin
array_delim = storage.array_delim
array_end = storage.array_end
face_lines = [
(
f"\t{object_begin} " +
f"{object_delim} ".join([f"{v.index}{index_suffix}" for v in f.verts]) +
f" {object_end}"
)
for f in faces
]
return (
f"faces = {array_begin}\n" +
f"{array_delim}\n".join(face_lines) +
f"\n{array_end}"
)
def format_vertices_per_face(faces):
storage = bpy.context.scene.export_as_array_properties
vert_suffix = storage.vert_suffix
object_begin = storage.object_begin
object_delim = storage.object_delim
object_end = storage.object_end
array_begin = storage.array_begin
array_delim = storage.array_delim
array_end = storage.array_end
face_lines = [
(
f"\t{object_begin} {v.co.x}{vert_suffix}{object_delim} "
f"{v.co.y}{vert_suffix}{object_delim} "
f"{v.co.z}{vert_suffix} "
f"{object_end}"
)
for f in faces
for v in f.verts
]
return (
f"verts_per_face = {array_begin}\n" +
f"{array_delim}\n".join(face_lines) +
f"\n{array_end}"
)
def get_array_string_for(mesh):
storage = bpy.context.scene.export_as_array_properties
export_mode = storage.export_mode_enum
export_vertices = False
export_faces = False
export_vertices_per_face = False
if export_mode == "export_mode_verts_plus_faces":
export_vertices = True
export_faces = True
elif export_mode == "export_mode_verts_only":
export_vertices = True
elif export_mode == "export_mode_faces_only":
export_faces = True
elif export_mode == "export_mode_verts_per_face":
export_vertices_per_face = True
bm = bmesh.new()
bm.from_mesh(mesh)
sep = "\n\n"
result = (
f"{format_vertices(bm.verts) if export_vertices else ''}"
f"{sep if export_vertices else ''}{format_faces(bm.faces) if export_faces else ''}"
f"{format_vertices_per_face(bm.faces) if export_vertices_per_face else ''}"
)
bm.free()
return result
class OBJECT_OT_export_as_array_disk(bpy.types.Operator):
bl_label = "Export to disk"
bl_idname = "object.export_as_array_disk"
filepath: StringProperty(subtype="FILE_PATH", options={"HIDDEN"})
check_existing: BoolProperty(default=True, options={"HIDDEN"})
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {"RUNNING_MODAL"};
def execute(self, context):
selected_mesh = get_selected_mesh(self, context)
if selected_mesh != None:
string = get_array_string_for(selected_mesh)
try:
with open(self.filepath, "w") as f:
f.write(string)
self.report({"INFO"}, "Successfuly saved!")
except Exception as e:
self.report({"ERROR"}, str(e))
return {"FINISHED"}
class OBJECT_OT_export_as_array_clipboard(bpy.types.Operator):
bl_label = "Copy to clipboard"
bl_idname = "object.export_as_array_clipboard"
def execute(self, context):
selected_mesh = get_selected_mesh(self, context)
if selected_mesh != None:
string = get_array_string_for(selected_mesh)
context.window_manager.clipboard = string
self.report({"INFO"}, "Successfuly copied")
return {"FINISHED"}
class OBJECT_OT_export_as_array(bpy.types.Operator):
bl_label = "Export as array"
bl_idname = "object.export_as_array"
def invoke(self, context, event):
return context.window_manager.invoke_popup(self, width=250)
def draw(self, context):
layout = self.layout
storage = bpy.context.scene.export_as_array_properties
row = layout.row()
row.label(text="Export settings")
row.prop(storage, "profiles_enum")
layout.separator(type="LINE")
split = layout.split(factor=0.5)
left_col = split.column()
right_col = split.column()
props = [
("Vertex suffix", "vert_suffix"),
("Index suffix", "index_suffix"),
("Array begin", "array_begin"),
("Array delimiter", "array_delim"),
("Array end", "array_end"),
("Object begin", "object_begin"),
("Object delimiter", "object_delim"),
("Object end", "object_end")
]
for prop in props:
left_col.label(text=prop[0])
right_col.prop(storage, prop[1], text="")
layout.prop(storage, "export_mode_enum", text="")
layout.operator(OBJECT_OT_export_as_array_clipboard.bl_idname)
layout.operator(OBJECT_OT_export_as_array_disk.bl_idname)
def execute(self, context):
return {"FINISHED"}
def menu_func(self, context):
layout = self.layout
layout.separator(type="LINE")
layout.operator(OBJECT_OT_export_as_array.bl_idname)
classes = [
ExportAsArrayProperties,
OBJECT_OT_export_as_array,
OBJECT_OT_export_as_array_clipboard,
OBJECT_OT_export_as_array_disk
]
def register():
for c in classes:
bpy.utils.register_class(c)
bpy.types.Scene.export_as_array_properties = PointerProperty(type=ExportAsArrayProperties)
bpy.types.VIEW3D_MT_object.append(menu_func)
def unregister():
del bpy.types.Scene.export_as_array_properties
for c in classes:
bpy.utils.unregister_class(c)
bpy.types.VIEW3D_MT_object.remove(menu_func)
if __name__ == "__main__":
register()