1
0
mirror of https://github.com/DarklightGames/io_scene_psk_psa.git synced 2025-02-07 22:41:26 +01:00

Added option to swap between exporting raw mesh data or mesh data after non-armature modifiers.

This commit is contained in:
Colin Basnett 2022-05-11 15:57:22 -07:00
parent 50f6dca565
commit 0d779b8174
2 changed files with 39 additions and 20 deletions

View File

@ -16,6 +16,7 @@ class PskBuilderOptions(object):
def __init__(self): def __init__(self):
self.bone_filter_mode = 'ALL' self.bone_filter_mode = 'ALL'
self.bone_group_indices = [] self.bone_group_indices = []
self.use_raw_mesh_data = True
class PskBuilder(object): class PskBuilder(object):
@ -120,11 +121,11 @@ class PskBuilder(object):
psk.bones.append(psk_bone) psk.bones.append(psk_bone)
for mesh_object in input_objects.mesh_objects: for input_mesh_object in input_objects.mesh_objects:
# MATERIALS # MATERIALS
material_indices = [] material_indices = []
for i, material in enumerate(mesh_object.data.materials): for i, material in enumerate(input_mesh_object.data.materials):
if material is None: if material is None:
raise RuntimeError('Material cannot be empty (index ' + str(i) + ')') raise RuntimeError('Material cannot be empty (index ' + str(i) + ')')
if material.name in materials: if material.name in materials:
@ -140,20 +141,33 @@ class PskBuilder(object):
material_index = psk_material.texture_index material_index = psk_material.texture_index
material_indices.append(material_index) material_indices.append(material_index)
if options.use_raw_mesh_data:
mesh_object = input_mesh_object
mesh_data = input_mesh_object.data
else:
# Create a copy of the mesh object after non-armature modifiers are applied.
# Temporarily deactivate any armature modifiers on the input mesh object.
active_armature_modifiers = [x for x in filter(lambda x: x.type == 'ARMATURE' and x.is_active, input_mesh_object.modifiers)]
for modifier in active_armature_modifiers:
modifier.show_viewport = False
depsgraph = context.evaluated_depsgraph_get() depsgraph = context.evaluated_depsgraph_get()
bm = bmesh.new() bm = bmesh.new()
bm.from_object(mesh_object, depsgraph) bm.from_object(input_mesh_object, depsgraph)
# TODO: make a new temporary mesh then delete it!
mesh_data = bpy.data.meshes.new('') mesh_data = bpy.data.meshes.new('')
bm.to_mesh(mesh_data) bm.to_mesh(mesh_data)
del bm del bm
mesh_object = bpy.data.objects.new('', mesh_data)
mesh_object.matrix_world = input_mesh_object.matrix_world
# Create a copy of the mesh object
mesh_object_copy = bpy.data.objects.new('', mesh_data)
# Copy the vertex groups # Copy the vertex groups
for vertex_group in mesh_object.vertex_groups: for vertex_group in mesh_object.vertex_groups:
mesh_object_copy.vertex_groups.new(name=vertex_group.name) mesh_object.vertex_groups.new(name=vertex_group.name)
# Reactivate previously active armature modifiers
for modifier in active_armature_modifiers:
modifier.show_viewport = True
vertex_offset = len(psk.points) vertex_offset = len(psk.points)
@ -214,7 +228,7 @@ class PskBuilder(object):
# Because the vertex groups may contain entries for which there is no matching bone in the armature, # Because the vertex groups may contain entries for which there is no matching bone in the armature,
# we must filter them out and not export any weights for these vertex groups. # we must filter them out and not export any weights for these vertex groups.
bone_names = [x.name for x in bones] bone_names = [x.name for x in bones]
vertex_group_names = [x.name for x in mesh_object_copy.vertex_groups] vertex_group_names = [x.name for x in mesh_object.vertex_groups]
vertex_group_bone_indices = dict() vertex_group_bone_indices = dict()
for vertex_group_index, vertex_group_name in enumerate(vertex_group_names): for vertex_group_index, vertex_group_name in enumerate(vertex_group_names):
try: try:
@ -233,7 +247,7 @@ class PskBuilder(object):
break break
except ValueError: except ValueError:
bone = bone.parent bone = bone.parent
for vertex_group_index, vertex_group in enumerate(mesh_object_copy.vertex_groups): for vertex_group_index, vertex_group in enumerate(mesh_object.vertex_groups):
if vertex_group_index not in vertex_group_bone_indices: if vertex_group_index not in vertex_group_bone_indices:
# Vertex group has no associated bone, skip it. # Vertex group has no associated bone, skip it.
continue continue
@ -251,7 +265,8 @@ class PskBuilder(object):
w.weight = weight w.weight = weight
psk.weights.append(w) psk.weights.append(w)
bpy.data.objects.remove(mesh_object_copy) if not options.use_raw_mesh_data:
bpy.data.objects.remove(mesh_object)
bpy.data.meshes.remove(mesh_data) bpy.data.meshes.remove(mesh_data)
del mesh_data del mesh_data

View File

@ -1,6 +1,6 @@
from typing import Type from typing import Type
from bpy.props import StringProperty, CollectionProperty, IntProperty, EnumProperty from bpy.props import BoolProperty, StringProperty, CollectionProperty, IntProperty, EnumProperty
from bpy.types import Operator, PropertyGroup from bpy.types import Operator, PropertyGroup
from bpy_extras.io_utils import ExportHelper from bpy_extras.io_utils import ExportHelper
@ -116,6 +116,8 @@ class PskExportOperator(Operator, ExportHelper):
scene = context.scene scene = context.scene
pg = scene.psk_export pg = scene.psk_export
layout.prop(pg, 'use_raw_mesh_data')
# BONES # BONES
box = layout.box() box = layout.box()
box.label(text='Bones', icon='BONE_DATA') box.label(text='Bones', icon='BONE_DATA')
@ -138,6 +140,7 @@ class PskExportOperator(Operator, ExportHelper):
options = PskBuilderOptions() options = PskBuilderOptions()
options.bone_filter_mode = pg.bone_filter_mode options.bone_filter_mode = pg.bone_filter_mode
options.bone_group_indices = [x.index for x in pg.bone_group_list if x.is_selected] options.bone_group_indices = [x.index for x in pg.bone_group_list if x.is_selected]
options.use_raw_mesh_data = pg.use_raw_mesh_data
try: try:
psk = builder.build(context, options) psk = builder.build(context, options)
exporter = PskExporter(psk) exporter = PskExporter(psk)
@ -161,6 +164,7 @@ class PskExportPropertyGroup(PropertyGroup):
) )
bone_group_list: CollectionProperty(type=BoneGroupListItem) bone_group_list: CollectionProperty(type=BoneGroupListItem)
bone_group_list_index: IntProperty(default=0) bone_group_list_index: IntProperty(default=0)
use_raw_mesh_data: BoolProperty(default=False, name='Raw Mesh Data', description='No modifiers will be evaluated as part of the exported mesh')
classes = ( classes = (