-
Notifications
You must be signed in to change notification settings - Fork 3
/
exporter_ops.py
364 lines (290 loc) · 11.4 KB
/
exporter_ops.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# Copyright (C) 2019 h0bB1T
#
# 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, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import bpy, os
from bpy.types import Operator
from bpy.props import StringProperty, BoolProperty
from . utils import textures_of_objects, blender_2_8x, export_file, ASSET_TYPE_OBJECT
from . common_utils import calc_bounding_box
from . properties import Properties
from . preferences import PreferencesPanel
from . execute_blender import run_blend_fix
class UseObjectNameOperator(Operator):
bl_idname = "asset_wizard.use_object_name_op"
bl_description = "Use name from active object."
bl_label = "Use object name"
bl_options = {'REGISTER', 'INTERNAL'}
def execute(self, context):
if context.active_object:
Properties.get()["eobj_asset_name"] = context.active_object.name
return {'FINISHED'}
class ExportObjectBase:
category: StringProperty()
asset_name: StringProperty()
pack_textures: BoolProperty()
location: StringProperty()
rotation: BoolProperty()
rename: StringProperty()
rename_material: StringProperty()
export_type: StringProperty()
class OverwriteObjectExporterOperator(Operator, ExportObjectBase):
bl_idname = "asset_wizard.overwrite_object_exporter_op"
bl_label = "Overwrite existing file?"
bl_description = "Export selected objects (file already exists)."
bl_options = {'REGISTER', 'INTERNAL'}
def execute(self, context):
bpy.ops.asset_wizard.object_exporter_op(
category = self.category,
asset_name = self.asset_name,
pack_textures = self.pack_textures,
location = self.location,
rotation = self.rotation,
rename = self.rename,
rename_material = self.rename_material,
export_type = self.export_type
)
return {'FINISHED'}
def draw(self, context):
properties = Properties.get()
layout = self.layout
rows = min(20, len(properties.eobj_pack_textures_list))
layout.row().template_list(
"UI_UL_TexturePackList",
"PackList",
properties,
"eobj_pack_textures_list",
properties,
"eobj_pack_textures_index",
rows=rows,
maxrows=rows
)
def invoke(self, context, event):
if self.export_type == '0' and self.pack_textures and len(textures_of_objects(context.selected_objects)) > 0:
properties = Properties.get()
properties.eobj_pack_textures_list.clear()
for m in textures_of_objects(context.selected_objects):
properties.eobj_pack_textures_list.add().name = m
return context.window_manager.invoke_props_dialog(self, width=800)
else:
return context.window_manager.invoke_confirm(self, event)
class TexturePackSelectionOperator(Operator, ExportObjectBase):
bl_idname = "asset_wizard.texture_selection_op"
bl_label = "Select textures to pack"
bl_description = "Export selected objects"
bl_options = {'REGISTER', 'INTERNAL'}
def invoke(self, context, event):
properties = Properties.get()
properties.eobj_pack_textures_list.clear()
for m in textures_of_objects(context.selected_objects):
properties.eobj_pack_textures_list.add().name = m
return context.window_manager.invoke_props_dialog(self, width=800)
def draw(self, context):
properties = Properties.get()
layout = self.layout
rows = min(20, len(properties.eobj_pack_textures_list))
layout.row().template_list(
"UI_UL_TexturePackList",
"PackList",
properties,
"eobj_pack_textures_list",
properties,
"eobj_pack_textures_index",
rows=rows,
maxrows=rows
)
def execute(self, context):
bpy.ops.asset_wizard.object_exporter_op(
category = self.category,
asset_name = self.asset_name,
pack_textures = self.pack_textures,
location = self.location,
rotation = self.rotation,
rename = self.rename,
rename_material = self.rename_material,
export_type = self.export_type
)
return {'FINISHED'}
class ObjectExporterOperator(Operator, ExportObjectBase):
bl_idname = "asset_wizard.object_exporter_op"
bl_label = "Export"
bl_description = "Export selected objects"
bl_options = {'REGISTER', 'UNDO'}
def calc_offset(self, objects):
"""
Depending on export location, calculate and return the XYZ offset.
"""
if self.location == "0": # Original
return (0, 0, 0)
bmin, bmax = calc_bounding_box(objects)
center = (
(bmax[0]-bmin[0])/2 + bmin[0],
(bmax[1]-bmin[1])/2 + bmin[1],
(bmax[2]-bmin[2])/2 + bmin[2]
)
if self.location == "1": # Origin
return (-center[0], -center[1], -center[2])
if self.location == "2": # Origin, Z == 0
return (-center[0], -center[1], -bmin[2])
def material_list(self, objects):
"""
Create unique list of materials used by the objects.
"""
l = []
for o in objects:
[ l.append(m.material) for m in o.material_slots if m.material ]
return set(l)
def store_object_information(self, objects):
"""
Store (eventually) changed original values for later restoration.
Returns dict with object: (name, meshName, originalPos, originalRot).
"""
original = {}
for o in objects:
original[o] = (
o.name,
o.data.name if o.data else None,
(o.location.x, o.location.y, o.location.z),
(o.rotation_euler.x, o.rotation_euler.y, o.rotation_euler.z)
)
return original
def restore_object_information(self, original):
"""
Return objects to their original location.
"""
for o in original.keys():
name, mname, loc, rot = original[o]
o.name = name
if o.data:
o.data.name = mname
o.location = loc
o.rotation_euler.x, o.rotation_euler.y, o.rotation_euler.z = rot
def store_material_information(self, objects):
"""
Store original material names.
Returns dict with material: name.
"""
original = {}
for m in self.material_list(objects):
original[m] = m.name
return original
def restore_material_information(self, original):
"""
Restores the original names of the material.
"""
for m in original.keys():
m.name = original[m]
def clear_rotation(self, objects):
"""
If single object is selected, temporary clear it's rotation.
"""
if self.rotation and len(objects) == 1:
bpy.ops.object.rotation_clear(False)
def translate_objects(self, objects, offset):
"""
Move all objects by the offset, clears rotation, returns dictionary
"""
for o in objects:
o.location.x += offset[0]
o.location.y += offset[1]
o.location.z += offset[2]
def rename_objects(self, objects):
"""
Fix names according to setting.
"""
if self.rename == "1": # Prefix
for o in objects:
o.name = "%s_%s" % (self.asset_name, o.name)
if o.data:
o.data.name = o.name
if self.rename == "2": # Full
for i, o in enumerate(objects):
o.name = "%s_%03i" % (self.asset_name, i)
if o.data:
o.data.name = o.name
def rename_materials(self, objects):
"""
Fix names according to setting.
"""
if self.rename_material == "1": # Prefix
for m in self.material_list(objects):
m.name = "%s_%s" % (self.asset_name, m.name)
if self.rename_material == "2": # Full
for i, m in enumerate(self.material_list(objects)):
m.name = "%s_%03i" % (self.asset_name, i)
def export_blend(self, path, objects):
if blender_2_8x():
bpy.data.libraries.write(
path,
set(objects),
relative_remap=True,
compress=True,
fake_user=True
)
else:
bpy.data.libraries.write(
path,
set(objects),
path_remap=PreferencesPanel.get().export_remap,
compress=True,
fake_user=True
)
properties = Properties.get()
textures_to_pack = [ os.path.split(t.name)[1] for t in properties.eobj_pack_textures_list if t.selected ]
# The created file is just a library, instance the object using an external blender run.
# https://blender.stackexchange.com/questions/129592/bpy-data-libraries-write-not-working
run_blend_fix(path, textures_to_pack)
def export_fbx(self, path):
bpy.ops.export_scene.fbx(filepath=path, use_selection=True)
def execute(self, context):
# Get selected objects.
objects = context.selected_objects
# At least one must be selected.
if not objects:
self.report({'ERROR'}, "No object selected")
# Store original state.
original = self.store_object_information(objects)
originalMat = self.store_material_information(objects)
# Optionally reset rotation.
self.clear_rotation(objects)
# Optionally center object.
self.translate_objects(objects, self.calc_offset(objects))
# Optionally rename objects.
self.rename_objects(objects)
# Optionally rename materials.
self.rename_materials(objects)
# Do the export
path = export_file(
ASSET_TYPE_OBJECT,
self.category,
self.asset_name,
Properties.export_type_ext(self.export_type)
)
if self.export_type == '0': # Blend
self.export_blend(path, objects)
self.report({'INFO'}, "BLEND created and fixed.")
elif self.export_type == '1': # FBX
self.export_fbx(path)
self.report({'INFO'}, "FBX created.")
# Restore original state.
self.restore_material_information(originalMat)
self.restore_object_information(original)
# Refresh view.
bpy.ops.asset_wizard.refresh_object_previews_op()
# Put onto render queue.
Properties.get_render_previews().add_job(
ASSET_TYPE_OBJECT,
path
)
return {'FINISHED'}