Skip to content

Commit

Permalink
Importing PSKs with poly flags now works
Browse files Browse the repository at this point in the history
  • Loading branch information
cmbasnett committed Feb 29, 2024
1 parent 1411696 commit 15e2c6c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
6 changes: 3 additions & 3 deletions io_scene_psk_psa/psk/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from bpy.types import Armature, Material

from .data import *
from .properties import get_poly_flags
from .properties import triangle_type_and_bit_flags_to_poly_flags
from ..helpers import *


Expand Down Expand Up @@ -146,8 +146,8 @@ def build_psk(context, options: PskBuildOptions) -> PskBuildResult:
except UnicodeEncodeError:
raise RuntimeError(f'Material name "{material.name}" contains characters that cannot be encoded in the Windows-1252 codepage')
psk_material.texture_index = len(psk.materials)
psk_material.poly_flags = get_poly_flags(material.psk)
print(psk_material.name, psk_material.poly_flags)
psk_material.poly_flags = triangle_type_and_bit_flags_to_poly_flags(material.psk.mesh_triangle_type,
material.psk.mesh_triangle_bit_flags)
psk.materials.append(psk_material)

context.window_manager.progress_begin(0, len(input_objects.mesh_objects))
Expand Down
4 changes: 4 additions & 0 deletions io_scene_psk_psa/psk/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from mathutils import Quaternion, Vector, Matrix

from .data import Psk
from .properties import poly_flags_to_triangle_type_and_bit_flags
from ..helpers import rgb_to_srgb, is_bdk_addon_loaded


Expand Down Expand Up @@ -134,6 +135,9 @@ def import_psk(psk: Psk, context, options: PskImportOptions) -> PskImportResult:
else:
# Just create a blank material.
material = bpy.data.materials.new(material_name)
mesh_triangle_type, mesh_triangle_bit_flags = poly_flags_to_triangle_type_and_bit_flags(psk_material.poly_flags)
material.psk.mesh_triangle_type = mesh_triangle_type
material.psk.mesh_triangle_bit_flags = mesh_triangle_bit_flags
material.use_nodes = True
mesh_data.materials.append(material)

Expand Down
38 changes: 24 additions & 14 deletions io_scene_psk_psa/psk/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
from bpy.types import PropertyGroup

mesh_triangle_types_items = (
('MTT_Normal', 'Normal', 'Normal one-sided', 0),
('MTT_NormalTwoSided', 'Normal Two-Sided', 'Normal but two-sided', 1),
('MTT_Translucent', 'Translucent', 'Translucent two-sided', 2),
('MTT_Masked', 'Masked', 'Masked two-sided', 3),
('MTT_Modulate', 'Modulate', 'Modulation blended two-sided', 4),
('MTT_Placeholder', 'Placeholder', 'Placeholder triangle for positioning weapon. Invisible', 8),
('NORMAL', 'Normal', 'Normal one-sided', 0),
('NORMAL_TWO_SIDED', 'Normal Two-Sided', 'Normal but two-sided', 1),
('TRANSLUCENT', 'Translucent', 'Translucent two-sided', 2),
('MASKED', 'Masked', 'Masked two-sided', 3),
('MODULATE', 'Modulate', 'Modulation blended two-sided', 4),
('PLACEHOLDER', 'Placeholder', 'Placeholder triangle for positioning weapon. Invisible', 8),
)

mesh_triangle_bit_flags_items = (
('MTT_Unlit', 'Unlit', 'Full brightness, no lighting', 16),
('MTT_Flat', 'Flat', 'Flat surface, don\'t do bMeshCurvy thing', 32),
('MTT_Environment', 'Environment', 'Environment mapped', 64),
('MTT_NoSmooth', 'No Smooth', 'No bilinear filtering on this poly\'s texture', 128),
('UNLIT', 'Unlit', 'Full brightness, no lighting', 16),
('FLAT', 'Flat', 'Flat surface, don\'t do bMeshCurvy thing', 32),
('ENVIRONMENT', 'Environment', 'Environment mapped', 64),
('NO_SMOOTH', 'No Smooth', 'No bilinear filtering on this poly\'s texture', 128),
)

class PSX_PG_material(PropertyGroup):
Expand All @@ -26,13 +26,23 @@ class PSX_PG_material(PropertyGroup):
mesh_triangle_bit_flags_items_dict = {item[0]: item[3] for item in mesh_triangle_bit_flags_items}


def get_poly_flags(material: PSX_PG_material) -> int:
def triangle_type_and_bit_flags_to_poly_flags(mesh_triangle_type: str, mesh_triangle_bit_flags: set[str]) -> int:
poly_flags = 0
poly_flags |= mesh_triangle_types_items_dict[material.mesh_triangle_type]
for flag in material.mesh_triangle_bit_flags:
poly_flags |= mesh_triangle_bit_flags_items_dict[flag]
poly_flags |= mesh_triangle_types_items_dict.get(mesh_triangle_type, 0)
for flag in mesh_triangle_bit_flags:
poly_flags |= mesh_triangle_bit_flags_items_dict.get(flag, 0)
return poly_flags


def poly_flags_to_triangle_type_and_bit_flags(poly_flags: int) -> (str, set[str]):
try:
triangle_type = next(item[0] for item in mesh_triangle_types_items if item[3] == (poly_flags & 15))
except StopIteration:
triangle_type = 'NORMAL'
triangle_bit_flags = {item[0] for item in mesh_triangle_bit_flags_items if item[3] & poly_flags}
return triangle_type, triangle_bit_flags


classes = (
PSX_PG_material,
)

0 comments on commit 15e2c6c

Please sign in to comment.