Compare commits

...

11 Commits
1.0.0 ... 1.1.0

Author SHA1 Message Date
Colin Basnett
4c4900e388 Merge remote-tracking branch 'origin/master' 2021-08-14 23:18:59 -07:00
Colin Basnett
08f4960e1f Update README.md 2021-08-14 23:15:38 -07:00
Colin Basnett
f08ecc457f Update README.md 2021-08-14 23:15:22 -07:00
Colin Basnett
0d03e49da3 Removed some unhelpful comments and TODOs. 2021-08-14 22:59:05 -07:00
Colin Basnett
32cadbfe31 Merge pull request #6 from DarklightGames/feature-static-meshes
Added the ability for users to export "static meshes" for meshes don't have armature modifiers
2021-08-14 22:02:19 -07:00
Colin Basnett
a76215569d Added the ability for users to export "static meshes" for meshes that don't have armature modifiers 2021-08-14 18:00:16 -07:00
Colin Basnett
e51013eec7 Incremented version. 2021-08-14 16:39:23 -07:00
Colin Basnett
94da7b4acf Merge branch 'feature-multiple-meshes' 2021-08-14 14:29:55 -07:00
Colin Basnett
63ccc92a9f The PSK export context is now consistently checked and proper errors are thrown before getting into the file dialog. 2021-08-02 22:42:39 -07:00
Colin Basnett
fd0b494d53 * Multiple meshes can now be exported as a single PSK
* Mesh object transforms are now properly accounted for when exporting a PSK
* Fixed a bug where the PSA export dialog wouldn't auto-select valid actions
2021-08-01 13:33:21 -07:00
Colin Basnett
c531256e92 PSA Export:
* Actions with no f-curve data will not be automatically selected for export

PSK Export:
* Now enforcing a rule that the mesh's armature modifier must be the last in the stack (if it isn't, mesh skinning will be absent in resultant PSK)
* Fixed a crash that would occur when attempting to attempt to export a mesh with an empty material slot
2020-04-12 20:41:14 -07:00
6 changed files with 216 additions and 146 deletions

View File

@@ -1,3 +1,25 @@
# io_export_psk_psa This Blender add-on allows you to export meshes and animations to the [PSK and PSA file formats](https://wiki.beyondunreal.com/PSK_%26_PSA_file_formats).
If you are looking for a PSK/PSA importer, use [this one](https://github.com/Befzz/blender3d_import_psk_psa)! # Installation
1. Download the zip file for the latest version from the [releases](https://github.com/DarklightGames/io_export_psk_psa/releases) page.
2. Open Blender 2.80 or later.
3. Navigate to the Blender Preferences (Edit > Preferences).
4. Select the "Add-ons" tab.
5. Click the "Install..." button.
6. Select the .zip file that you downloaded earlier and click "Install Add-on".
7. Enable the newly added "Import-Export: PSK/PSA Exporter" addon.
# Usage
## Exporting a PSK
1. Select the mesh objects you wish to export.
3. Navigate to File > Export > Unreal PSK (.psk)
4. Enter the file name and click "Export".
## Exporting a PSA
1. Select the armature objects you wish to export.
2. Navigate to File > Export > Unreal PSA (.psa)
3. Enter the file name and click "Export".
# FAQ
## Can I use this addon to import PSK and PSA files?
Currently, no. If you are looking for a PSK/PSA importer, use [this one](https://github.com/Befzz/blender3d_import_psk_psa)!

View File

@@ -1,7 +1,7 @@
bl_info = { bl_info = {
"name": "PSK/PSA Exporter", "name": "PSK/PSA Exporter",
"author": "Colin Basnett", "author": "Colin Basnett",
"version": ( 1, 0, 0 ), "version": ( 1, 1, 0 ),
"blender": ( 2, 80, 0 ), "blender": ( 2, 80, 0 ),
"location": "File > Export > PSK Export (.psk)", "location": "File > Export > PSK Export (.psk)",
"description": "PSK/PSA Export (.psk)", "description": "PSK/PSA Export (.psk)",

View File

@@ -56,16 +56,17 @@ class PsaExportOperator(Operator, ExportHelper):
row.template_list('PSA_UL_ActionList', 'asd', scene, 'psa_action_list', scene, 'psa_action_list_index', rows=len(context.scene.psa_action_list)) row.template_list('PSA_UL_ActionList', 'asd', scene, 'psa_action_list', scene, 'psa_action_list_index', rows=len(context.scene.psa_action_list))
def is_action_for_armature(self, action): def is_action_for_armature(self, action):
bone_names = [x.name for x in self.armature.data.bones] if len(action.fcurves) == 0:
print(bone_names) return False
bone_names = set([x.name for x in self.armature.data.bones])
for fcurve in action.fcurves: for fcurve in action.fcurves:
match = re.match('pose\.bones\["(.+)"\].\w+', fcurve.data_path) match = re.match(r'pose\.bones\["(.+)"\].\w+', fcurve.data_path)
if not match: if not match:
continue continue
bone_name = match.group(1) bone_name = match.group(1)
if bone_name not in bone_names: if bone_name in bone_names:
return False return True
return True return False
def invoke(self, context, event): def invoke(self, context, event):
if context.view_layer.objects.active.type != 'ARMATURE': if context.view_layer.objects.active.type != 'ARMATURE':

View File

@@ -1,158 +1,197 @@
import bpy import bpy
import bmesh import bmesh
from collections import OrderedDict
from .data import * from .data import *
# 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
@staticmethod
def get_input_objects(context) -> PskInputObjects:
input_objects = PskInputObjects()
for obj in context.view_layer.objects.selected:
if obj.type != 'MESH':
raise RuntimeError(f'Selected object "{obj.name}" is not a mesh')
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')
for obj in input_objects.mesh_objects:
if len(obj.data.materials) == 0:
raise RuntimeError(f'Mesh "{obj.name}" must have at least one material')
# Ensure that there are either no armature modifiers (static mesh)
# or that there is exactly one armature modifier object shared between
# all selected meshes
armature_modifier_objects = set()
for obj in input_objects.mesh_objects:
modifiers = [x for x in obj.modifiers if x.type == 'ARMATURE']
if len(modifiers) == 0:
continue
elif len(modifiers) == 2:
raise RuntimeError(f'Mesh "{obj.name}" must have only one armature modifier')
armature_modifier_objects.add(modifiers[0].object)
if len(armature_modifier_objects) > 1:
raise RuntimeError('All selected meshes must have the same armature modifier')
elif len(armature_modifier_objects) == 1:
input_objects.armature_object = list(armature_modifier_objects)[0]
return input_objects
def build(self, context) -> Psk: def build(self, context) -> Psk:
object = context.view_layer.objects.active input_objects = PskBuilder.get_input_objects(context)
wedge_count = sum([len(m.data.loops) for m in input_objects.mesh_objects])
if object.type != 'MESH': if wedge_count <= 65536:
raise RuntimeError('Selected object must be a mesh')
if len(object.data.materials) == 0:
raise RuntimeError('Mesh must have at least one material')
# ensure that there is exactly one armature modifier
modifiers = [x for x in object.modifiers if x.type == 'ARMATURE']
if len(modifiers) != 1:
raise RuntimeError('Mesh must have one armature modifier')
armature_modifier = modifiers[0]
armature_object = armature_modifier.object
if armature_object is None:
raise RuntimeError('Armature modifier has no linked object')
# TODO: probably requires at least one bone? not sure
mesh_data = object.data
# TODO: if there is an edge-split modifier, we need to apply it (maybe?)
# TODO: duplicate all the data
mesh = bpy.data.meshes.new('export')
# copy the contents of the mesh
bm = bmesh.new()
bm.from_mesh(mesh_data)
bmesh.ops.triangulate(bm, faces=bm.faces)
bm.to_mesh(mesh)
bm.free()
del bm
psk = Psk()
# VERTICES
for vertex in object.data.vertices:
point = Vector3()
point.x = vertex.co.x
point.y = vertex.co.y
point.z = vertex.co.z
psk.points.append(point)
# WEDGES
uv_layer = object.data.uv_layers.active.data
if len(object.data.loops) <= 65536:
wedge_type = Psk.Wedge16 wedge_type = Psk.Wedge16
else: else:
wedge_type = Psk.Wedge32 wedge_type = Psk.Wedge32
psk.wedges = [wedge_type() for _ in range(len(object.data.loops))]
for loop_index, loop in enumerate(object.data.loops): psk = Psk()
wedge = psk.wedges[loop_index]
wedge.material_index = 0
wedge.point_index = loop.vertex_index
wedge.u, wedge.v = uv_layer[loop_index].uv
wedge.v = 1.0 - wedge.v
psk.wedges.append(wedge)
# MATERIALS materials = OrderedDict()
for i, m in enumerate(object.data.materials):
if m is None:
raise RuntimeError('Material cannot be empty (index ' + str(i) + ')')
material = Psk.Material()
material.name = bytes(m.name, encoding='utf-8')
material.texture_index = i
psk.materials.append(material)
# FACES if input_objects.armature_object is None:
# TODO: this is making the assumption that the mesh is triangulated # Static mesh (no armature)
object.data.calc_loop_triangles()
poly_groups, groups = object.data.calc_smooth_groups(use_bitflags=True)
for f in object.data.loop_triangles:
face = Psk.Face()
face.material_index = f.material_index
face.wedge_index_1 = f.loops[2]
face.wedge_index_2 = f.loops[1]
face.wedge_index_3 = f.loops[0]
face.smoothing_groups = poly_groups[f.polygon_index]
psk.faces.append(face)
# update the material index of the wedges
for i in range(3):
psk.wedges[f.loops[i]].material_index = f.material_index
# https://github.com/bwrsandman/blender-addons/blob/master/io_export_unreal_psk_psa.py
bones = list(armature_object.data.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('static', encoding='utf-8')
psk_bone.flags = 0 psk_bone.flags = 0
psk_bone.children_count = len(bone.children) psk_bone.children_count = 0
psk_bone.parent_index = 0
try: psk_bone.location = Vector3(0, 0, 0)
psk_bone.parent_index = bones.index(bone.parent) psk_bone.rotation = Quaternion(0, 0, 0, 1)
except ValueError:
psk_bone.parent_index = 0
if bone.parent is not None:
rotation = bone.matrix.to_quaternion()
rotation.x = -rotation.x
rotation.y = -rotation.y
rotation.z = -rotation.z
quat_parent = bone.parent.matrix.to_quaternion().inverted()
parent_head = quat_parent @ bone.parent.head
parent_tail = quat_parent @ bone.parent.tail
location = (parent_tail - parent_head) + bone.head
else:
location = armature_object.matrix_local @ bone.head
rot_matrix = bone.matrix @ armature_object.matrix_local.to_3x3()
rotation = rot_matrix.to_quaternion()
psk_bone.location.x = location.x
psk_bone.location.y = location.y
psk_bone.location.z = location.z
psk_bone.rotation.x = rotation.x
psk_bone.rotation.y = rotation.y
psk_bone.rotation.z = rotation.z
psk_bone.rotation.w = rotation.w
psk.bones.append(psk_bone) psk.bones.append(psk_bone)
else:
bones = list(input_objects.armature_object.data.bones)
for bone in bones:
psk_bone = Psk.Bone()
psk_bone.name = bytes(bone.name, encoding='utf-8')
psk_bone.flags = 0
psk_bone.children_count = len(bone.children)
# WEIGHTS
# TODO: bone ~> vg might not be 1:1, provide a nice error message if this is the case
armature = armature_object.data
bone_names = [x.name for x in armature.bones]
vertex_group_names = [x.name for x in object.vertex_groups]
bone_indices = [bone_names.index(name) for name in vertex_group_names]
for vertex_group_index, vertex_group in enumerate(object.vertex_groups):
bone_index = bone_indices[vertex_group_index]
for vertex_index in range(len(object.data.vertices)):
try: try:
weight = vertex_group.weight(vertex_index) psk_bone.parent_index = bones.index(bone.parent)
except RuntimeError: except ValueError:
continue psk_bone.parent_index = 0
if weight == 0.0:
continue if bone.parent is not None:
w = Psk.Weight() rotation = bone.matrix.to_quaternion()
w.bone_index = bone_index rotation.x = -rotation.x
w.point_index = vertex_index rotation.y = -rotation.y
w.weight = weight rotation.z = -rotation.z
psk.weights.append(w) quat_parent = bone.parent.matrix.to_quaternion().inverted()
parent_head = quat_parent @ bone.parent.head
parent_tail = quat_parent @ bone.parent.tail
location = (parent_tail - parent_head) + bone.head
else:
location = input_objects.armature_object.matrix_local @ bone.head
rot_matrix = bone.matrix @ input_objects.armature_object.matrix_local.to_3x3()
rotation = rot_matrix.to_quaternion()
psk_bone.location.x = location.x
psk_bone.location.y = location.y
psk_bone.location.z = location.z
psk_bone.rotation.x = rotation.x
psk_bone.rotation.y = rotation.y
psk_bone.rotation.z = rotation.z
psk_bone.rotation.w = rotation.w
psk.bones.append(psk_bone)
vertex_offset = 0
wedge_offset = 0
weight_offset = 0
# TODO: if there is an edge-split modifier, we need to apply it (maybe?)
for object in input_objects.mesh_objects:
# VERTICES
for vertex in object.data.vertices:
point = Vector3()
v = object.matrix_world @ vertex.co
point.x = v.x
point.y = v.y
point.z = v.z
psk.points.append(point)
# WEDGES
uv_layer = object.data.uv_layers.active.data
psk.wedges.extend([wedge_type() for _ in range(len(object.data.loops))])
for loop_index, loop in enumerate(object.data.loops):
wedge = psk.wedges[wedge_offset + loop_index]
wedge.material_index = 0 # NOTE: this material index is set properly while building the faces
wedge.point_index = loop.vertex_index + vertex_offset
wedge.u, wedge.v = uv_layer[loop_index].uv
wedge.v = 1.0 - wedge.v
psk.wedges.append(wedge)
# MATERIALS
material_indices = []
for i, m in enumerate(object.data.materials):
if m is None:
raise RuntimeError('Material cannot be empty (index ' + str(i) + ')')
if m.name in materials:
material_index = list(materials.keys()).index(m.name)
else:
material = Psk.Material()
material.name = bytes(m.name, encoding='utf-8')
material.texture_index = len(psk.materials)
psk.materials.append(material)
materials[m.name] = m
material_index = material.texture_index
material_indices.append(material_index)
# FACES
object.data.calc_loop_triangles()
poly_groups, groups = object.data.calc_smooth_groups(use_bitflags=True)
for f in object.data.loop_triangles:
face = Psk.Face()
face.material_index = material_indices[f.material_index]
face.wedge_index_1 = f.loops[2] + wedge_offset
face.wedge_index_2 = f.loops[1] + wedge_offset
face.wedge_index_3 = f.loops[0] + wedge_offset
face.smoothing_groups = poly_groups[f.polygon_index]
psk.faces.append(face)
# update the material index of the wedges
for i in range(3):
psk.wedges[wedge_offset + f.loops[i]].material_index = face.material_index
# WEIGHTS
# TODO: bone ~> vg might not be 1:1, provide a nice error message if this is the case
if input_objects.armature_object is not None:
armature = input_objects.armature_object.data
bone_names = [x.name for x in armature.bones]
vertex_group_names = [x.name for x in object.vertex_groups]
bone_indices = [bone_names.index(name) for name in vertex_group_names]
for vertex_group_index, vertex_group in enumerate(object.vertex_groups):
bone_index = bone_indices[vertex_group_index]
for vertex_index in range(len(object.data.vertices)):
try:
weight = vertex_group.weight(vertex_index)
except RuntimeError:
continue
if weight == 0.0:
continue
w = Psk.Weight()
w.bone_index = bone_index
w.point_index = vertex_offset + vertex_index
w.weight = weight
psk.weights.append(w)
vertex_offset += len(psk.points)
wedge_offset += len(psk.wedges)
weight_offset += len(psk.weights)
return psk return psk

View File

@@ -19,13 +19,11 @@ class PskExporter(object):
fp.write(datum) fp.write(datum)
def export(self, path: str): def export(self, path: str):
# TODO: add logic somewhere to assert lengths of ctype structs
with open(path, 'wb') as fp: with open(path, 'wb') as fp:
self.write_section(fp, b'ACTRHEAD') self.write_section(fp, b'ACTRHEAD')
self.write_section(fp, b'PNTS0000', Vector3, self.psk.points) self.write_section(fp, b'PNTS0000', Vector3, self.psk.points)
# WEDGES # WEDGES
# TODO: this really should be on the level of the builder, not the exporter
if len(self.psk.wedges) <= 65536: if len(self.psk.wedges) <= 65536:
wedge_type = Psk.Wedge16 wedge_type = Psk.Wedge16
else: else:

View File

@@ -18,6 +18,16 @@ class PskExportOperator(Operator, ExportHelper):
maxlen=1024, maxlen=1024,
default='') default='')
def invoke(self, context, event):
try:
PskBuilder.get_input_objects(context)
except RuntimeError as e:
self.report({'ERROR_INVALID_CONTEXT'}, str(e))
return {'CANCELLED'}
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context): def execute(self, context):
builder = PskBuilder() builder = PskBuilder()
psk = builder.build(context) psk = builder.build(context)