-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
345 lines (274 loc) · 12.9 KB
/
core.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
import bpy
import os
import bpy.utils.previews
from . import modifiers
from . import operators
from . import bundles
from . import settings
from .settings import mode_bundle_types, mode_pivot_types
from . import addon_updater_ops
def set_path(self, value):
# checks if the provided path is inside a subdirectory of the current file to save it as a relative path
if bpy.data.is_saved:
value = os.path.realpath(bpy.path.abspath(value))
file_path = os.path.dirname(os.path.realpath(bpy.path.abspath(bpy.data.filepath)))
if os.path.commonprefix([os.path.realpath(bpy.path.abspath(value)), file_path]) == file_path:
value = bpy.path.relpath(value)
self.real_path = value
def get_path(self):
return self.real_path
def get_modifier_for_ctx(index):
if index < 0:
return bpy.context.scene.BGE_Settings.scene_modifiers
else:
return bundles.get_bundles()[index].override_modifiers
preset_enum_items = []
def get_preset_enum(self, context):
global preset_enum_items
prefs = context.preferences.addons[__name__.split('.')[0]].preferences
presets = settings.get_presets(context.scene.BGE_Settings.export_format)
if context.scene.BGE_Settings.export_format == prefs.export_format and prefs.export_preset in presets.keys():
index = list(presets.keys()).index(prefs.export_preset)
preset_enum_items = settings.create_preset_enum(presets)
preset_enum_items[0], preset_enum_items[index] = preset_enum_items[index], preset_enum_items[0]
return preset_enum_items
else:
preset_enum_items = settings.create_preset_enum(presets)
return preset_enum_items
class BGE_BatchInfo(bpy.types.PropertyGroup):
path: bpy.props.StringProperty(subtype='FILE_PATH')
class BGE_Settings(bpy.types.PropertyGroup):
real_path: bpy.props.StringProperty(default="")
path: bpy.props.StringProperty(
name="Output Path",
default="",
description="Define the path where to export or import from",
subtype='DIR_PATH',
get=get_path,
set=set_path
)
padding: bpy.props.FloatProperty(
name="Padding",
default=0.15,
min=0,
description="Padding for fences",
subtype='DISTANCE'
)
bundles: bpy.props.CollectionProperty(
type=bundles.Bundle
)
bundle_index: bpy.props.IntProperty(
name="Bundles",
description="Bundles"
)
batches: bpy.props.CollectionProperty(
type=BGE_BatchInfo
)
batch_index: bpy.props.IntProperty(
name="Batches",
description="batches"
)
show_bundle_objects: bpy.props.BoolProperty(
name="Show bundle objects",
default=True,
)
show_bundle_info: bpy.props.BoolProperty(
name="Show bundle info",
default=False,
)
mode_bundle: bpy.props.EnumProperty(items=mode_bundle_types, name="Bundle Mode", default=bpy.context.preferences.addons[__name__.split('.')[0]].preferences.mode_bundle)
mode_pivot: bpy.props.EnumProperty(items=mode_pivot_types, name="Pivot From", default=bpy.context.preferences.addons[__name__.split('.')[0]].preferences.mode_pivot)
scene_modifiers: bpy.props.PointerProperty(type=modifiers.BGE_modifiers) # sometimes this variable may point to an old version, maybe force reload modules will fix it
export_format: bpy.props.EnumProperty(items=settings.export_formats, default=bpy.context.preferences.addons[__name__.split('.')[0]].preferences.export_format)
def default_export_preset_changed(self, value):
self.internal_export_preset = preset_enum_items[value][0]
def get_export_preset(self):
try:
return next(i for i, x in enumerate(preset_enum_items) if x[0] == self.internal_export_preset)
except StopIteration:
if 'export_preset' in self:
return self['export_preset']
return 0
internal_export_preset: bpy.props.StringProperty(default='BGE_unreal')
export_preset: bpy.props.EnumProperty(items=get_preset_enum, get=get_export_preset, set=default_export_preset_changed)
class BGE_PT_core_panel(bpy.types.Panel):
bl_idname = "BGE_PT_core_panel"
bl_label = "Main Settings"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "Bundle Exporter"
def draw(self, context):
addon_updater_ops.check_for_update_background()
addon_updater_ops.update_notice_box_ui(self, context)
layout = self.layout
box = layout.box()
#row = box.row(align=True)
#row.operator('bge.load_preferences', text='', icon='RECOVER_LAST')
#row.label(text='Settings', icon='PREFERENCES')
col = box.column(align=False)
row = col.row(align=True)
if context.scene.BGE_Settings.path == "":
row.alert = True
row.prop(context.scene.BGE_Settings, "path", text="")
if context.scene.BGE_Settings.path != "":
row = row.row(align=True)
row.operator("wm.path_open", text="", icon='FILE_TICK').filepath = context.scene.BGE_Settings.path
#row = col.split(factor=0.3, align=True)
col.prop(context.scene.BGE_Settings, "export_format", text='Format', icon='FILE_CACHE')
col.prop(context.scene.BGE_Settings, "export_preset", text='Preset', icon='PRESET')
row = col.row(align=True)
row.prop(context.scene.BGE_Settings, "mode_bundle", text="Bundle by")
row.operator("wm.url_open", text="", icon='QUESTION').url = "http://renderhjs.net/fbxbundle/#settings_bundle"
row = col.row(align=True)
row.prop(context.scene.BGE_Settings, "mode_pivot", text="Pivot at", expand=False)
row.operator("wm.url_open", text="", icon='QUESTION').url = "http://renderhjs.net/fbxbundle/#settings_pivot"
col = box.column(align=True)
row = col.row(align=True)
row.prop(bpy.context.scene.unit_settings, "system", text='')
row.prop(bpy.context.scene.unit_settings, "scale_length", text='')
row = col.row(align=True)
row.prop(context.scene.BGE_Settings, "padding", text="Padding", expand=True)
# Warnings
col.alert = True
if context.space_data.local_view:
box = col.box()
box.label(text="Can't export in local view mode.", icon='CANCEL')
if context.active_object and context.active_object.mode != 'OBJECT':
box = col.box()
box.label(text="Requires object mode to export.", icon='CANCEL')
if context.scene.BGE_Settings.path == "":
box = col.box()
box.label(text="No output path defined.", icon='CANCEL')
elif context.scene.BGE_Settings.mode_bundle == 'COLLECTION' and len(bpy.data.collections) == 0:
box = col.box()
box.label(text="No groups available", icon='CANCEL')
class BGE_PT_modifiers_panel(bpy.types.Panel):
bl_idname = "BGE_PT_modifiers_panel"
bl_label = "Export Modifiers"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "Bundle Exporter"
#bl_context = "objectmode"
def draw(self, context):
self.layout.operator_menu_enum(operators.BGE_OT_add_bundle_modifier.bl_idname, 'option')
modifiers.draw(self.layout, context, -1, draw_only_active=True)
class BGE_UL_bundles(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
col = layout.column()
row = col.row()
icon = 'RESTRICT_SELECT_ON'
if item.is_bundle_obj_selected():
icon = 'RESTRICT_SELECT_OFF'
row.operator(operators.op_bundles.BGE_OT_select.bl_idname, text='', icon=icon).index = index
row.alert = not item.is_key_valid()
row.prop(item, 'key', text='', icon="FILE_3D", emboss=False)
row.label(text='', icon=next(x[3] for x in mode_bundle_types if x[0] == item.mode_bundle))
row.label(text='', icon=next(x[3] for x in mode_pivot_types if x[0] == item.mode_pivot))
row.operator(operators.op_bundles.BGE_OT_remove.bl_idname, text='', icon='CANCEL').index = index
def invoke(self, context, event):
pass
class BGE_UL_batch_files(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
layout.prop(item, 'path', text='')
layout.operator('bge.delete_batch',icon='X', text='').index=index
def invoke(self, context, event):
pass
class BGE_PT_batch_export_panel(bpy.types.Panel):
bl_idname = "BGE_PT_batch_export_panel"
bl_label = "Batch Export"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "Bundle Exporter"
#bl_context = "objectmode"
def draw(self, context):
row = self.layout.row()
row.template_list("BGE_UL_batch_files", "", bpy.context.scene.BGE_Settings, "batches", bpy.context.scene.BGE_Settings, "batch_index", rows=2)
col = row.column()
col.operator('bge.add_batch', text='', icon='ADD')
self.layout.operator('bge.batch_export')
class BGE_PT_files_panel(bpy.types.Panel):
bl_idname = "BGE_PT_files_panel"
bl_label = "Bundles"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "Bundle Exporter"
#bl_context = "objectmode"
# bl_options = {'HIDE_HEADER'}
def draw(self, context):
layout = self.layout
# Get bundles
bundle_list = bundles.get_bundles()
selected_bundles = [x for x in bundle_list if x.is_bundle_obj_selected()]
icon = 'EXPORT'
col = layout.column(align=True)
row = col.row(align=True)
row.scale_y = 1.2
row.operator(operators.BGE_OT_fence_draw.bl_idname, text="Draw Fences", icon='AXIS_TOP')
row.operator(operators.BGE_OT_fence_clear.bl_idname, text="", icon='PANEL_CLOSE')
col.template_list("BGE_UL_bundles", "", bpy.context.scene.BGE_Settings, "bundles", bpy.context.scene.BGE_Settings, "bundle_index", rows=2)
row = col.row(align=True)
split = row.split(factor=0.2, align=True)
split.scale_y = 1.4
split.operator(operators.BGE_OT_create_bundle.bl_idname, text="", icon='ADD')
split.operator(operators.BGE_OT_file_export_scene_selected.bl_idname, text="Selected ({}x)".format(len(selected_bundles)), icon=icon)
split.operator(operators.BGE_OT_file_export.bl_idname, text="All ({}x)".format(len(bundles.get_bundles(only_valid=True))), icon=icon)
bundle_index = bpy.context.scene.BGE_Settings.bundle_index
if bpy.context.scene.BGE_Settings.bundle_index < len(bundle_list) and len(bundle_list) > 0:
num_objects = len(bundle_list[bundle_index].objects)
box = layout.box()
row = box.row(align=True)
row.prop(
bpy.context.scene.BGE_Settings,
'show_bundle_info',
icon="TRIA_DOWN" if bpy.context.scene.BGE_Settings.show_bundle_info else "TRIA_RIGHT",
icon_only=True,
text='',
emboss=False
)
row.label(text=bundle_list[bundle_index].filename, icon='FILE_3D')
row = row.row()
row.alignment = 'RIGHT'
row.operator(operators.op_bundles.BGE_OT_select.bl_idname, emboss=False, text='x{}'.format(num_objects)).index = bundle_index
row.prop(bundle_list[bundle_index], 'mode_bundle', icon_only=True, text='', emboss=False)
row.prop(bundle_list[bundle_index], 'mode_pivot', icon_only=True, text='', emboss=False)
row.operator(operators.BGE_OT_file_export_selected.bl_idname, text="Export", icon=icon)
if bpy.context.scene.BGE_Settings.show_bundle_info:
row = box.row()
row.separator()
sub_box = row.column(align=True)
objs = bundle_list[bundle_index].objects
for x in objs:
icon = 'OUTLINER_OB_MESH'
if x.type == 'ARMATURE':
icon = 'OUTLINER_OB_ARMATURE'
elif x.type == 'EMPTY':
icon = 'OUTLINER_OB_EMPTY'
sub_box.label(text=x.name, icon=icon)
box.operator_menu_enum(operators.BGE_OT_override_bundle_modifier.bl_idname, 'option')
modifiers.draw(box, context, bundle_index, draw_only_active=True)
layout.separator()
addon_keymaps = []
classes = [
BGE_BatchInfo,
BGE_Settings,
BGE_UL_bundles,
BGE_UL_batch_files,
BGE_PT_core_panel,
BGE_PT_modifiers_panel,
BGE_PT_files_panel,
BGE_PT_batch_export_panel,
]
def register():
print('--> REGISTER_CORE')
from bpy.utils import register_class
register_class(bundles.Bundle)
for cls in classes:
register_class(cls)
bpy.types.Scene.BGE_Settings = bpy.props.PointerProperty(type=BGE_Settings)
def unregister():
print('### UNREGISTER CORE')
from bpy.utils import unregister_class
unregister_class(bundles.Bundle)
for cls in reversed(classes):
unregister_class(cls)
del bpy.types.Scene.BGE_Settings