-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpsk_psa_batch_export.py
470 lines (424 loc) · 21.4 KB
/
psk_psa_batch_export.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
bl_info = {
"name": "ActorX to FBX for Unreal",
"description": "Bulk convert ActorX to FBX for UE",
"author": "Zain/Buckminsterfullerene",
"version": (0, 0, 5),
"blender": (3, 4, 0),
"support": "COMMUNITY",
"category": "Import",
}
import re
import os
import json
import bpy
from bpy.types import Scene
from bpy.props import (BoolProperty,
StringProperty,
EnumProperty,
PointerProperty )
from io_import_scene_unreal_psa_psk_280 import pskimport
from io_import_scene_unreal_psa_psk_280 import psaimport
def print(data):
for window in bpy.context.window_manager.windows:
screen = window.screen
for area in screen.areas:
if area.type == 'CONSOLE':
override = {'window': window, 'screen': screen, 'area': area}
bpy.ops.console.scrollback_append(override, text=str(data), type="OUTPUT")
def show_msg(msg):
bpy.ops.Pskfbx.message('INVOKE_DEFAULT', message = msg)
class PSKFBX_AddonProperties(bpy.types.PropertyGroup):
psk_folder_path: bpy.props.StringProperty(name="PSK Folder Path",
description="Select Folder",
default="",
maxlen=1024,
subtype="DIR_PATH")
file_enum : bpy.props.EnumProperty(
name = "File Type",
description = "choose file type",
items= [('PSK',"PSK", ""),
('PSKX',"PSKX", "")
]
)
psa_folder : bpy.props.StringProperty(name="PSA Folder",
description="Select Folder",
default="",
maxlen=1024,
subtype="DIR_PATH")
skeleton_folder : bpy.props.StringProperty(name="Skeleton Folder",
description="Select Folder",
default="",
maxlen=1024,
subtype="DIR_PATH")
psk_file : bpy.props.StringProperty(name="Select PSK",
description="Select PSK to use for PSA",
default="",
maxlen=1024,
subtype="FILE_PATH")
skeleton_enum : bpy.props.EnumProperty(
name = "File Type",
description = "use Folder or select PSK",
items= [('PSK',"SELECT PSK", ""),
('FOLDER',"USE FOLDER", "")
]
)
mesh_bool : bpy.props.BoolProperty(name='Include mesh',
description='If the animation should include the mesh',
default=False,
subtype='NONE')
replace_fbx_psk : bpy.props.BoolProperty(name='Replace existing FBX file',
description='If it should replace existing FBX files',
default=True,
subtype='NONE')
replace_fbx_psa : bpy.props.BoolProperty(name='Replace existing FBX file',
description='If it should replace existing FBX files',
default=True,
subtype='NONE')
######################## PSK RUN ######################################
class IMPORTEXPORT(bpy.types.Operator):
bl_idname = "object.importexport"
bl_label = "import export"
bl_options = {'REGISTER', 'UNDO'}
def importpsk(path, bmesh):
pskimport(
filepath = path,
context = None,
bImportmesh = bmesh,
bImportbone = True,
bSpltiUVdata = False,
fBonesize = 5.0,
fBonesizeRatio = 0.6,
bDontInvertRoot = True,
bReorientBones = False,
bReorientDirectly = False,
bScaleDown = False,
bToSRGB = False,
error_callback = None)
def importpsa(path):
psaimport(path,
context = None,
oArmature = None,
bFilenameAsPrefix = False,
bActionsToTrack = False,
first_frames = 0,
bDontInvertRoot = True,
bUpdateTimelineRange = True,
bRotationOnly = False,
bScaleDown = False,
fcurve_interpolation = 'LINEAR',
bBoneNameCaseSensitiveCmp = True,
error_callback = print
)
def exportfbx(path):
bpy.ops.export_scene.fbx(
filepath=path,
use_selection = False,
global_scale = 1,
apply_unit_scale = True,
apply_scale_options = 'FBX_SCALE_NONE',
use_space_transform = True,
bake_space_transform = True,
object_types = {'ARMATURE', 'MESH'},
use_mesh_modifiers = True,
mesh_smooth_type = 'EDGE',
use_mesh_edges = False,
use_tspace = False,
use_custom_props = False,
add_leaf_bones = False,
primary_bone_axis = 'Y',
secondary_bone_axis = 'X',
use_armature_deform_only = False,
armature_nodetype = 'NULL',
bake_anim = True,
bake_anim_use_all_bones = True,
bake_anim_use_nla_strips = False,
bake_anim_use_all_actions = False,
bake_anim_force_startend_keying = True,
bake_anim_step = 1.0,
bake_anim_simplify_factor = 0.0,
path_mode = 'AUTO',
embed_textures = False,
batch_mode = 'OFF',
use_batch_own_dir = True,
axis_forward = '-Z',
axis_up = 'Y',
)
class PSKFBX_Run(bpy.types.Operator):
bl_idname = "object.pskrun"
bl_label = "Convert PSK to FBX Recursive"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scene = context.scene
args = scene.my_properties
#bpy.context.scene.unit_settings.scale_length = 0.01
root_directory = bpy.path.abspath(args.psk_folder_path)
# Recursively process all files in the root directory and subdirectories
for dirpath, dirnames, filenames in os.walk(root_directory):
for filename in filenames:
# Check if the file is an .psk file
if args.file_enum == 'PSK':
format = '.psk'
if args.file_enum == 'PSKX':
format = '.pskx'
if filename.endswith(format):
# Construct the full file path
path = os.path.join(dirpath, filename)
fbx_path = path.replace(format, '.fbx')
if os.path.exists(fbx_path) and not args.replace_fbx_psk:
print('fbx file already exists' + fbx_path)
continue
# Import the .psk file
IMPORTEXPORT.importpsk(path, True)
#Rename Armature to "Armature"
selected = bpy.context.selected_objects[0]
selected.name = "Armature"
selected.data.name = "Root"
# Export the object to .fbx
IMPORTEXPORT.exportfbx(fbx_path)
for material in bpy.data.materials:
material.user_clear()
bpy.data.materials.remove(material)
for armature in bpy.data.armatures:
armature.user_clear()
bpy.data.armatures.remove(armature)
for mesh in bpy.data.meshes:
mesh.user_clear()
bpy.data.meshes.remove(mesh)
purge_data = set(o.data for o in context.scene.objects if o.data)
bpy.data.batch_remove(context.scene.objects)
bpy.data.batch_remove([o for o in purge_data if not o.users])
#print('exported ' + path + ' successfully')
return {'FINISHED'}
######################### PSA RUN ################################
class PSAFBX_Run(bpy.types.Operator):
bl_idname = "object.psarun"
bl_label = "Convert PSA to FBX Recursive"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
context = context
scene = context.scene
#bpy.context.scene.unit_settings.scale_length = 0.01
args = scene.my_properties
props_pattern = 'Skeleton = Skeleton'
if args.skeleton_enum == 'FOLDER':
root_directory = bpy.path.abspath(args.psa_folder)
# Recursively process all files in the root directory and subdirectories
for dirpath, dirnames, filenames in os.walk(root_directory):
for filename in filenames:
# Check if the file is an .props.txt file
if filename.endswith('.psa'):
# Construct the full file path
psa_path = os.path.join(dirpath, filename)
#print(psa_path)
props_path = (psa_path[:-3] + 'props.txt')
fbx_path = (psa_path[:-3] + 'fbx')
if os.path.exists(fbx_path) and not args.replace_fbx_psa:
print('fbx file already exists' + fbx_path)
continue
if not os.path.exists(props_path):
props_path = (psa_path[:-3] + 'json')
if not os.path.exists(props_path):
#print('props file not found')
continue
#print(props_path)
with open(props_path) as file:
skeleton_info = json.load(file)
skeleton_name = skeleton_info["Properties"]["Skeleton"]["ObjectName"][8:].replace("'", "")
#print(skeleton_name)
root_directory = bpy.path.abspath(args.skeleton_folder)
# Recursively process all files in the root directory and subdirectories
for dir, names, files in os.walk(root_directory):
for file in files:
# Check if the file is an .psk file
if file == (skeleton_name + '.psk'):
# Construct the full file path
skeleton_path = os.path.join(dir, file)
#Import Skeleton as PSK
IMPORTEXPORT.importpsk(skeleton_path, False)
#Rename Armature to "Armature"
selected = bpy.context.selected_objects[0]
selected.name = "Armature"
selected.data.name = "Root"
# Construct PSA path and import
IMPORTEXPORT.importpsa(psa_path)
# Construct FBX path and export
IMPORTEXPORT.exportfbx(fbx_path)
#Clear scene
for action in bpy.data.actions:
action.user_clear()
bpy.data.actions.remove(action)
for armature in bpy.data.armatures:
armature.user_clear()
bpy.data.armatures.remove(armature)
#purge_data = set(o.data for o in context.scene.objects if o.data)
#bpy.data.batch_remove(context.scene.objects)
#bpy.data.batch_remove([o for o in purge_data if not o.users])
print('exported ' + fbx_path + ' successfully')
found = True
break
else:
with open(props_path, 'r') as file:
found = False
for line in file:
if found:
break
if (props_pattern in line):
skeleton_asset_path = line.rsplit("'")
skeleton_name = skeleton_asset_path[1].split(".")[1]
print(skeleton_name)
root_directory = bpy.path.abspath(args.skeleton_folder)
# Recursively process all files in the root directory and subdirectories
for dir, names, files in os.walk(root_directory):
for file in files:
# Check if the file is an .psk file
if file == (skeleton_name + '.psk'):
# Construct the full file path
skeleton_path = os.path.join(dir, file)
#Import Skeleton as PSK
IMPORTEXPORT.importpsk(skeleton_path, False)
#Rename Armature to "Armature"
selected = bpy.context.selected_objects[0]
selected.name = "Armature"
selected.data.name = "Root"
# Construct PSA path and import
IMPORTEXPORT.importpsa(psa_path)
# Construct FBX path and export
IMPORTEXPORT.exportfbx(fbx_path)
#Clear scene
for action in bpy.data.actions:
action.user_clear()
bpy.data.actions.remove(action)
for armature in bpy.data.armatures:
armature.user_clear()
bpy.data.armatures.remove(armature)
#purge_data = set(o.data for o in context.scene.objects if o.data)
#bpy.data.batch_remove(context.scene.objects)
#bpy.data.batch_remove([o for o in purge_data if not o.users])
print('exported ' + fbx_path + ' successfully')
found = True
break
if args.skeleton_enum == 'PSK':
psk_path = args.psk_file
root_directory = bpy.path.abspath(args.psa_folder)
# Recursively process all files in the root directory and subdirectories
for dirpath, dirnames, filenames in os.walk(root_directory):
for filename in filenames:
# Check if the file is an .props.txt file
if filename.endswith('.psa'):
psa_path = os.path.join(dirpath, filename)
fbx_path = psa_path.replace('.psa', '.fbx')
#Import Skeleton as PSK
IMPORTEXPORT.importpsk(psk_path, args.mesh_bool)
#Rename Armature to "Armature"
selected = bpy.context.selected_objects[0]
selected.name = "Armature"
selected.data.name = "Root"
# Construct PSA path and import
IMPORTEXPORT.importpsa(psa_path)
# Construct FBX path and export
IMPORTEXPORT.exportfbx(fbx_path)
#Clear scene
scene = context.scene
for action in bpy.data.actions:
action.user_clear()
bpy.data.actions.remove(action)
for material in bpy.data.materials:
material.user_clear()
bpy.data.materials.remove(material)
for armature in bpy.data.armatures:
armature.user_clear()
bpy.data.armatures.remove(armature)
for mesh in bpy.data.meshes:
mesh.user_clear()
bpy.data.meshes.remove(mesh)
purge_data = set(o.data for o in context.scene.objects if o.data)
bpy.data.batch_remove(context.scene.objects)
bpy.data.batch_remove([o for o in purge_data if not o.users])
print('exported ' + psa_path + ' successfully')
return {'FINISHED'}
########## PANELS ###################
class PSKFBX_AddonPanel(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "PSK2FBX"
bl_label = "PSK TO FBX"
def draw(self, context):
layout = self.layout
scene = context.scene
props = context.scene.my_properties
layout.prop(props, "psk_folder_path")
layout.prop(props, "file_enum")
layout.prop(props, "replace_fbx_psk")
layout.operator("object.pskrun", text = "Convert PSK to UE FBX Recursive")
class PSAFBX_AddonPanel(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "PSK2FBX"
bl_label = "PSA TO FBX"
def draw(self, context):
layout = self.layout
scene = context.scene
props = context.scene.my_properties
layout.prop(props, "psa_folder")
if props.skeleton_enum == 'FOLDER':
layout.prop(props, "skeleton_folder")
layout.prop(props, "skeleton_enum")
else:
layout.prop(props, "psk_file")
layout.prop(props, "skeleton_enum")
layout.prop(props, "mesh_bool")
layout.prop(props, "replace_fbx_psa")
layout.operator("object.psarun", text = "Convert PSA to UE FBX Recursive")
class PSKFBX_OT_show_message(bpy.types.Operator):
bl_idname = "pskfbx.message"
bl_label = "PSK2FBX"
bl_options = {'REGISTER', 'INTERNAL'}
message : StringProperty(default = 'Message')
lines = []
line0 = None
def execute(self, context):
self.lines = self.message.split("\n")
maxlen = 0
for line in self.lines:
if len(line) > maxlen:
maxlen = len(line)
print(self.message)
self.report({'WARNING'}, self.message)
return {'FINISHED'}
def invoke(self, context, event):
self.lines = self.message.split("\n")
maxlen = 0
for line in self.lines:
if len(line) > maxlen:
maxlen = len(line)
self.line0 = self.lines.pop(0)
return context.window_manager.invoke_props_dialog(self, width = 100 + 6*maxlen)
def cancel(self, context):
# print('cancel')
self.execute(self)
def draw(self, context):
layout = self.layout
sub = layout.column()
sub.label(text = self.line0, icon = 'ERROR')
for line in self.lines:
sub.label(text = line)
classes = (
PSKFBX_AddonProperties,
IMPORTEXPORT,
PSKFBX_Run,
PSAFBX_Run,
PSKFBX_AddonPanel,
PSAFBX_AddonPanel,
PSKFBX_OT_show_message
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.my_properties = bpy.props.PointerProperty(type=PSKFBX_AddonProperties)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
del bpy.types.Scene.my_properties
if __name__ == "__main__":
register()
bpy.app.handlers.load_post.append(register)