-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_panel.py
410 lines (306 loc) · 13.1 KB
/
export_panel.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
bl_info = {
"name": "Unity Export",
"description": "A way to expedite the process of exporting multiple groups of objects into separate files.",
"author": "João Ferreira (@TUTAMKHAMON)",
"version": (1, 3),
"blender": (2, 80, 0),
"location": "View3D > Sidebar > Asset Exporter",
"support": "COMMUNITY",
"warning": "", #TODO: if any major bug is detected, update with info
"doc_url": "https://github.com/TUTAMKHAMON/BlenderAssetExportPanel",
"category": "Import-Export",
}
import bpy
class ASSET_EXPORT_obj_list(bpy.types.PropertyGroup):
obj: bpy.props.PointerProperty(type=bpy.types.Object)
class ASSET_EXPORT_entry(bpy.types.PropertyGroup):
name: bpy.props.StringProperty(name='Object name', default='Unnamed')
export: bpy.props.BoolProperty(name='Export boolean', default=True)
objs: bpy.props.CollectionProperty(type=ASSET_EXPORT_obj_list)
class ASSET_EXPORT_data(bpy.types.PropertyGroup):
export_path: bpy.props.StringProperty(name='Export path', default='/')
selected_rendered: bpy.props.IntProperty(name='Render export group')
current_render_name: bpy.props.StringProperty(name='Render file name')
rendered_objs: bpy.props.CollectionProperty(
type=ASSET_EXPORT_entry,
description='Unity Export: Rendered objects list')
selected_collision: bpy.props.IntProperty(name='Collision export group')
current_collision_name: bpy.props.StringProperty(name='Collision file name')
collision_objs: bpy.props.CollectionProperty(
type=ASSET_EXPORT_entry,
description='Unity Export: Collision objects list')
class ASSET_EXPORT_Add_Export(bpy.types.Operator):
bl_idname = 'tae.add_export'
bl_label = ''
bl_options = {'REGISTER'}
export_name: bpy.props.StringProperty()
list: bpy.props.StringProperty()
@classmethod
def description(cls, context, properties):
return "Add objects to the " + properties.list + " export list"
def execute(self, context):
obj = context.object
if self.list == 'render':
collection = bpy.context.scene.asset_export.rendered_objs
selected = bpy.context.scene.asset_export.selected_rendered
elif self.list == 'collision':
collection = bpy.context.scene.asset_export.collision_objs
selected = bpy.context.scene.asset_export.selected_collision
else:
return {'CANCELLED'}
newRO = collection.add()
newRO.name = self.export_name
for o in context.selected_objects:
if o.type == 'MESH':
go = newRO.objs.add()
go.obj = o
selected = collection.keys().__len__() - 1
return {'FINISHED'}
class ASSET_EXPORT_Remove_Export(bpy.types.Operator):
bl_idname = 'tae.remove_export'
bl_label = ''
bl_options = {'REGISTER'}
del_index: bpy.props.IntProperty()
list: bpy.props.StringProperty()
@classmethod
def description(self, context, properties):
return "Remove selected object from the " + properties.list + " export list"
def execute(self, context):
if self.list == 'render':
collection = bpy.context.scene.asset_export.rendered_objs
elif self.list == 'collision':
collection = bpy.context.scene.asset_export.collision_objs
else:
return {'CANCELLED'}
collection.remove(self.del_index)
return {'FINISHED'}
class ASSET_EXPORT_Select_Export(bpy.types.Operator):
bl_idname = 'tae.select_export'
bl_label = 'Select export'
index: bpy.props.IntProperty()
list: bpy.props.StringProperty()
def execute(self, context):
for obj in context.selected_objects:
obj.select_set(False)
if self.list == 'render':
collection = context.scene.asset_export.rendered_objs[self.index].objs
elif self.list == 'collision':
collection = context.scene.asset_export.collision_objs[self.index].objs
else:
return {'CANCELLED'}
for obj in collection:
obj.obj.select_set(True)
bpy.context.view_layer.objects.active = obj.obj
return {'FINISHED'}
class ASSET_EXPORT_UL_list(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
if active_propname == 'selected_rendered':
list = 'render'
selected_idx = context.scene.asset_export.selected_rendered
elif active_propname == 'selected_collision':
list = 'collision'
selected_idx = context.scene.asset_export.selected_collision
selected = selected_idx == index
if self.layout_type in {'DEFAULT', 'COMPACT'}:
# layout.prop(ma, "name", text="", emboss=False, icon_value=icon)
row = layout.row(align=True)
row.prop(item, 'export', icon_only=True, icon='CHECKBOX_HLT' if item.export else 'CHECKBOX_DEHLT',
emboss=False, toggle=0)
row.prop(item, 'name', text="", emboss=False, icon='MESH_DATA')
# row.label(text=item.name, translate=False, icon='MESH_DATA')
row.alignment = 'RIGHT'
# if selected:
# row.label(text='', icon='FILE_REFRESH')
op = row.operator(ASSET_EXPORT_Select_Export.bl_idname, text=str(item.objs.__len__()), emboss=False)
op.index = index
op.list = list
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label(text="", icon='MESH_DATA')
class ASSET_EXPORT_Choose_Folder(bpy.types.Operator):
"""Select save location"""
bl_idname = 'tae.choose_folder'
bl_label = 'Choose folder'
use_filter_folder = True
directory: bpy.props.StringProperty(name="Outdit Path", description="Export location")
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
bpy.context.scene.asset_export.export_path = self.directory
return {'FINISHED'}
# from bpy_extras.io_utils import ExportHelper
class ASSET_EXPORT_Export_Modal(bpy.types.Operator):
"""Open export window dialog"""
bl_idname = "tae.select_path"
bl_label = "Select save location"
use_filter_folder = True
directory: bpy.props.StringProperty(name="Outdit Path", description="Export location")
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def draw(self, context):
layout = self.layout
row = layout.row()
col = layout.column()
row = col.row()
row.label(text="Rendered Meshes", icon='SHADING_RENDERED')
row.alignment = 'RIGHT'
row.label(text=str(sum(o.export == True for o in context.scene.asset_export.rendered_objs)) + '/' + str(
context.scene.asset_export.rendered_objs.keys().__len__()))
col.template_list(
"ASSET_EXPORT_UL_list", "rendered",
bpy.context.scene.asset_export, 'rendered_objs',
bpy.context.scene.asset_export, 'selected_rendered')
layout.separator(factor=2)
col = layout.column()
row = col.row()
row.label(text="Collision Meshes", icon='SHADING_WIRE')
row.alignment = 'RIGHT'
row.label(text=str(sum(o.export == True for o in context.scene.asset_export.collision_objs)) + '/' + str(
context.scene.asset_export.collision_objs.keys().__len__()))
col.template_list(
"ASSET_EXPORT_UL_list", "collision",
bpy.context.scene.asset_export, 'collision_objs',
bpy.context.scene.asset_export, 'selected_collision')
def execute(self, context):
bpy.context.scene.asset_export.export_path = self.directory
bpy.ops.tae.export_all('INVOKE_DEFAULT')
return {'FINISHED'}
class ASSET_EXPORT_Export_All(bpy.types.Operator):
"""Quick export to previous chosen path"""
bl_idname = "tae.export_all"
bl_label = "Export all enabled"
bl_options = {'REGISTER'}
def execute(self, context):
filepath = bpy.context.scene.asset_export.export_path
r_objs = bpy.context.scene.asset_export.rendered_objs
for idx in range(r_objs.keys().__len__()):
exp = r_objs[idx]
if exp.export:
bpy.ops.tae.select_export(index=idx, list='render')
bpy.ops.export_scene.fbx(
filepath=filepath + 'RDR-' + exp.name + '.fbx',
check_existing=True,
use_selection=True,
axis_forward='-Z',
axis_up='Y',
object_types={'MESH'},
use_mesh_modifiers=True,
bake_anim=False)
c_objs = bpy.context.scene.asset_export.collision_objs
for idx in range(c_objs.keys().__len__()):
exp = c_objs[idx]
if exp.export:
bpy.ops.tae.select_export(index=idx, list='collision')
offset = bpy.context.object.location.copy()
bpy.ops.transform.translate(value=-offset)
bpy.ops.export_scene.obj(
filepath=filepath + 'COL-' + exp.name + '.obj',
check_existing=True,
use_selection=True,
axis_forward='-Z',
axis_up='Y',
use_mesh_modifiers=True,
use_blen_objects=True,
use_triangles=True,
use_animation=False,
use_uvs=False,
use_materials=False,
use_nurbs=False,
use_vertex_groups=False,
use_normals=False)
bpy.ops.transform.translate(value=offset)
return {'FINISHED'}
class ASSET_EXPORT_Panel(bpy.types.Panel):
"""Creates a Panel in the context menu in View3D"""
bl_idname = "TOOLS_PT_asset_export"
bl_label = "Batch exporter panel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_context = "objectmode"
bl_category = "Asset Exporter"
# bl_options = {'HIDE_HEADER'}
def draw(self, context):
layout = self.layout
obj = context.object
##########################
col = layout.column()
row = col.row()
row.label(text="Rendered Meshes", icon='SHADING_RENDERED')
row.alignment = 'RIGHT'
row.label(text=str(sum(o.export == True for o in context.scene.asset_export.rendered_objs)) + '/' + str(
context.scene.asset_export.rendered_objs.keys().__len__()))
col.template_list("ASSET_EXPORT_UL_list", "rendered", bpy.context.scene.asset_export, 'rendered_objs',
bpy.context.scene.asset_export, 'selected_rendered')
row = col.row(align=True)
row.prop(context.scene.asset_export, 'current_render_name', text="")
op = row.operator(ASSET_EXPORT_Add_Export.bl_idname, icon='ADD', text="")
op.list = 'render'
op.export_name = context.scene.asset_export.current_render_name
op = row.operator(ASSET_EXPORT_Remove_Export.bl_idname, icon='REMOVE', text="")
op.list = 'render'
op.del_index = context.scene.asset_export.selected_rendered
##########################
layout.separator(factor=2)
##########################
col = layout.column()
row = col.row()
row.label(text="Collision Meshes", icon='SHADING_WIRE')
row.alignment = 'RIGHT'
row.label(text=str(sum(o.export == True for o in context.scene.asset_export.collision_objs)) + '/' + str(
context.scene.asset_export.collision_objs.keys().__len__()))
col.template_list("ASSET_EXPORT_UL_list", "collision", bpy.context.scene.asset_export, 'collision_objs',
bpy.context.scene.asset_export, 'selected_collision')
row = col.row(align=True)
row.prop(context.scene.asset_export, 'current_collision_name', text="")
op = row.operator(ASSET_EXPORT_Add_Export.bl_idname, icon='ADD', text="")
op.list = 'collision'
op.export_name = context.scene.asset_export.current_collision_name
op = row.operator(ASSET_EXPORT_Remove_Export.bl_idname, icon='REMOVE', text="")
op.list = 'collision'
op.del_index = context.scene.asset_export.selected_rendered
###########################
layout.separator(factor=2)
###########################
# col = layout.column()
# row = col.row()
# row.label(text='Export')
# row = col.row()
# row.menu("INFO_MT_add", text='test')
# col = layout.column(align=True)
# row = col.row(align=True)
# row.prop(context.scene.asset_export, 'export_path', text='')
# op = row.operator(ASSET_EXPORT_Choose_Folder.bl_idname, icon='FILE_FOLDER', text='')
# row = col.row(align=True)
# row.enabled = context.scene.asset_export.export_path != ''
# op = row.operator(ASSET_EXPORT_Export_All.bl_idname, icon='EXPORT', text='Export All')
col = layout.column()
row = col.row(align=True)
op = row.operator(ASSET_EXPORT_Export_Modal.bl_idname, icon='FILEBROWSER', text='Export As')
op = row.operator(ASSET_EXPORT_Export_All.bl_idname, icon='EXPORT', text='')
def register():
bpy.utils.register_class(ASSET_EXPORT_obj_list)
bpy.utils.register_class(ASSET_EXPORT_entry)
bpy.utils.register_class(ASSET_EXPORT_data)
bpy.types.Scene.asset_export = bpy.props.PointerProperty(type=ASSET_EXPORT_data)
bpy.utils.register_class(ASSET_EXPORT_Add_Export)
bpy.utils.register_class(ASSET_EXPORT_Remove_Export)
bpy.utils.register_class(ASSET_EXPORT_Select_Export)
bpy.utils.register_class(ASSET_EXPORT_Choose_Folder)
bpy.utils.register_class(ASSET_EXPORT_UL_list)
bpy.utils.register_class(ASSET_EXPORT_Export_Modal)
bpy.utils.register_class(ASSET_EXPORT_Export_All)
bpy.utils.register_class(ASSET_EXPORT_Panel)
def unregister():
bpy.utils.unregister_class(ASSET_EXPORT_obj_list)
bpy.utils.unregister_class(ASSET_EXPORT_entry)
bpy.utils.unregister_class(ASSET_EXPORT_data)
bpy.utils.unregister_class(ASSET_EXPORT_Add_Export)
bpy.utils.unregister_class(ASSET_EXPORT_Remove_Export)
bpy.utils.unregister_class(ASSET_EXPORT_Select_Export)
bpy.utils.unregister_class(ASSET_EXPORT_Choose_Folder)
bpy.utils.unregister_class(ASSET_EXPORT_UL_list)
bpy.utils.unregister_class(ASSET_EXPORT_Export_Modal)
bpy.utils.unregister_class(ASSET_EXPORT_Export_All)
bpy.utils.unregister_class(ASSET_EXPORT_Panel)