1
0
mirror of https://github.com/DarklightGames/io_scene_psk_psa.git synced 2025-02-17 18:19:15 +01:00

The PSK export context is now consistently checked and proper errors are thrown before getting into the file dialog.

This commit is contained in:
Colin Basnett 2021-08-02 22:42:39 -07:00
parent fd0b494d53
commit 63ccc92a9f
2 changed files with 36 additions and 52 deletions

View File

@ -5,47 +5,53 @@ from .data import *
# https://github.com/bwrsandman/blender-addons/blob/master/io_export_unreal_psk_psa.py # https://github.com/bwrsandman/blender-addons/blob/master/io_export_unreal_psk_psa.py
class PskInputObjects(object):
def __init__(self):
self.mesh_objects = []
self.armature_object = None
class PskBuilder(object): class PskBuilder(object):
def __init__(self): def __init__(self):
# TODO: add options in here
pass pass
def build(self, context) -> Psk: @staticmethod
# TODO: it would be nice to be able to do this with MULTIPLE meshes so long as they both have the same armature def get_input_objects(context) -> PskInputObjects:
mesh_objects =[] input_objects = PskInputObjects()
for object in context.view_layer.objects.selected: for obj in context.view_layer.objects.selected:
if object.type != 'MESH': if obj.type != 'MESH':
raise RuntimeError(f'Selected object "{object.name}" is not a mesh') raise RuntimeError(f'Selected object "{obj.name}" is not a mesh')
mesh_objects.append(object)
if len(mesh_objects) == 0: input_objects.mesh_objects = context.view_layer.objects.selected
if len(input_objects.mesh_objects) == 0:
raise RuntimeError('At least one mesh must be selected') raise RuntimeError('At least one mesh must be selected')
for object in mesh_objects: for obj in input_objects.mesh_objects:
if len(object.data.materials) == 0: if len(obj.data.materials) == 0:
raise RuntimeError(f'Mesh "{object.name}" must have at least one material') raise RuntimeError(f'Mesh "{obj.name}" must have at least one material')
# ensure that there is exactly one armature modifier object shared between all selected meshes # ensure that there is exactly one armature modifier object shared between all selected meshes
armature_modifier_objects = set() armature_modifier_objects = set()
for object in mesh_objects: for obj in input_objects.mesh_objects:
modifiers = [x for x in object.modifiers if x.type == 'ARMATURE'] modifiers = [x for x in obj.modifiers if x.type == 'ARMATURE']
if len(modifiers) != 1: if len(modifiers) != 1:
raise RuntimeError(f'Mesh "{object.name}" must have one armature modifier') raise RuntimeError(f'Mesh "{obj.name}" must have one armature modifier')
armature_modifier_objects.add(modifiers[0].object) armature_modifier_objects.add(modifiers[0].object)
if len(armature_modifier_objects) > 1: if len(armature_modifier_objects) > 1:
raise RuntimeError('All selected meshes must have the same armature modifier') raise RuntimeError('All selected meshes must have the same armature modifier')
armature_object = list(armature_modifier_objects)[0] input_objects.armature_object = list(armature_modifier_objects)[0]
if armature_object is None: if input_objects.armature_object is None:
raise RuntimeError('Armature modifier has no linked object') raise RuntimeError('Armature modifier has no linked object')
# TODO: probably requires at least one bone? not sure return input_objects
wedge_count = sum([len(m.data.loops) for m in mesh_objects]) def build(self, context) -> Psk:
print(wedge_count) input_objects = PskBuilder.get_input_objects(context)
wedge_count = sum([len(m.data.loops) for m in input_objects.mesh_objects])
if wedge_count <= 65536: if wedge_count <= 65536:
wedge_type = Psk.Wedge16 wedge_type = Psk.Wedge16
else: else:
@ -55,7 +61,7 @@ class PskBuilder(object):
materials = OrderedDict() materials = OrderedDict()
bones = list(armature_object.data.bones) bones = list(input_objects.armature_object.data.bones)
for bone in bones: for bone in bones:
psk_bone = Psk.Bone() psk_bone = Psk.Bone()
psk_bone.name = bytes(bone.name, encoding='utf-8') psk_bone.name = bytes(bone.name, encoding='utf-8')
@ -77,8 +83,8 @@ class PskBuilder(object):
parent_tail = quat_parent @ bone.parent.tail parent_tail = quat_parent @ bone.parent.tail
location = (parent_tail - parent_head) + bone.head location = (parent_tail - parent_head) + bone.head
else: else:
location = armature_object.matrix_local @ bone.head location = input_objects.armature_object.matrix_local @ bone.head
rot_matrix = bone.matrix @ armature_object.matrix_local.to_3x3() rot_matrix = bone.matrix @ input_objects.armature_object.matrix_local.to_3x3()
rotation = rot_matrix.to_quaternion() rotation = rot_matrix.to_quaternion()
psk_bone.location.x = location.x psk_bone.location.x = location.x
@ -97,7 +103,7 @@ class PskBuilder(object):
weight_offset = 0 weight_offset = 0
# TODO: if there is an edge-split modifier, we need to apply it (maybe?) # TODO: if there is an edge-split modifier, we need to apply it (maybe?)
for object in mesh_objects: for object in input_objects.mesh_objects:
# VERTICES # VERTICES
for vertex in object.data.vertices: for vertex in object.data.vertices:
point = Vector3() point = Vector3()
@ -154,7 +160,7 @@ class PskBuilder(object):
# WEIGHTS # WEIGHTS
# TODO: bone ~> vg might not be 1:1, provide a nice error message if this is the case # TODO: bone ~> vg might not be 1:1, provide a nice error message if this is the case
armature = armature_object.data armature = input_objects.armature_object.data
bone_names = [x.name for x in armature.bones] bone_names = [x.name for x in armature.bones]
vertex_group_names = [x.name for x in object.vertex_groups] vertex_group_names = [x.name for x in object.vertex_groups]
bone_indices = [bone_names.index(name) for name in vertex_group_names] bone_indices = [bone_names.index(name) for name in vertex_group_names]

View File

@ -19,33 +19,11 @@ class PskExportOperator(Operator, ExportHelper):
default='') default='')
def invoke(self, context, event): def invoke(self, context, event):
# object = context.view_layer.objects.active try:
# PskBuilder.get_input_objects(context)
# if object.type != 'MESH': except RuntimeError as e:
# self.report({'ERROR_INVALID_CONTEXT'}, 'The selected object must be a mesh.') self.report({'ERROR_INVALID_CONTEXT'}, str(e))
# return {'CANCELLED'} return {'CANCELLED'}
#
# if len(object.data.materials) == 0:
# self.report({'ERROR_INVALID_CONTEXT'}, 'Mesh must have at least one material')
# return {'CANCELLED'}
#
# # ensure that there is exactly one armature modifier
# modifiers = [x for x in object.modifiers if x.type == 'ARMATURE']
#
# if len(modifiers) != 1:
# self.report({'ERROR_INVALID_CONTEXT'}, 'Mesh must have one armature modifier')
# return {'CANCELLED'}
#
# armature_modifier = modifiers[0]
# armature_object = armature_modifier.object
#
# if object.modifiers[-1] != armature_modifier:
# self.report({'ERROR_INVALID_CONTEXT'}, 'Armature modifier must be the last modifier in the stack')
# return {'CANCELLED'}
#
# if armature_object is None:
# self.report({'ERROR_INVALID_CONTEXT'}, 'Armature modifier has no linked object')
# return {'CANCELLED'}
context.window_manager.fileselect_add(self) context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'} return {'RUNNING_MODAL'}