Skip to content

Commit

Permalink
Fix loading of MDL v4 and v6
Browse files Browse the repository at this point in the history
  • Loading branch information
REDxEYE committed May 4, 2024
1 parent f7515cb commit 4cdd1d6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
5 changes: 2 additions & 3 deletions blender_bindings/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from SourceIO.blender_bindings.models.model_tags import MODEL_HANDLERS, choose_model_importer
from SourceIO.library.shared.content_providers.content_manager import ContentManager
from SourceIO.library.utils import Buffer
from . import mdl4, mdl6, mdl10, mdl36, mdl44, mdl49,md3_15
from . import mdl4, mdl6, mdl10, mdl36, mdl44, mdl49, md3_15
from ..operators.import_settings_base import ModelOptions
from ..shared.model_container import ModelContainer
from ...library.shared.app_id import SteamAppId
Expand All @@ -19,13 +19,12 @@ def import_model(model_path: Path, buffer: Buffer,
options: ModelOptions,
override_steam_id: Optional[SteamAppId] = None) -> Optional[ModelContainer]:
ident, version = buffer.read_fmt("4sI")
print(ident, version)
logger.info(f"Detected magic: {ident!r}, version:{version}")
cp = content_manager.get_content_provider_from_asset_path(model_path)
handler = choose_model_importer(ident, version, (override_steam_id or ((cp.steam_id or None) if cp else None)))
if handler is None:
logger.error(f"No handler found for ident {ident} version: {version}")
return None
print(handler)
buffer.seek(0)
container = handler(model_path, buffer, content_manager, options)
return container
5 changes: 3 additions & 2 deletions library/models/mdl/v6/mdl_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ def from_buffer(cls, buffer: Buffer):
for sequence in sequences:
sequence_animations = []
sequence: StudioSequence
for _ in range(header.bone_count):
sequence_animations.append(StudioAnimation.from_buffer(buffer, sequence.frame_count))
with buffer.read_from_offset(sequence.anim_offset):
for _ in range(header.bone_count):
sequence_animations.append(StudioAnimation.from_buffer(buffer, sequence.frame_count))
animations.append(sequence_animations)
# sequence = self.sequences[0]
# for n, bone in enumerate(self.bones):
Expand Down
11 changes: 6 additions & 5 deletions library/models/mdl/v6/structs/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ class StudioAnimation:
@classmethod
def from_buffer(cls, buffer: Buffer, frame_count: int):
pos_count, pos_offset, rot_count, rot_offset = buffer.read_fmt('4I')
buffer.seek(pos_offset)
raw_pos_data = np.frombuffer(buffer.read(pos_count * raw_pos_dtype.itemsize), raw_pos_dtype)

buffer.seek(rot_offset)
raw_rot_data = np.frombuffer(buffer.read(rot_count * raw_rot_dtype.itemsize), raw_rot_dtype)
with buffer.save_current_offset():
buffer.seek(pos_offset)
raw_pos_data = np.frombuffer(buffer.read(pos_count * raw_pos_dtype.itemsize), raw_pos_dtype)

buffer.seek(rot_offset)
raw_rot_data = np.frombuffer(buffer.read(rot_count * raw_rot_dtype.itemsize), raw_rot_dtype)
# buffer.skip(2)
frames = np.zeros((frame_count, 2, 3,), np.float32)

total_count = frames.shape[0]
Expand Down
2 changes: 1 addition & 1 deletion library/models/mdl/v6/structs/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def from_buffer(cls, buffer: Buffer):
pivot_count, pivot_offset,
motion_type, motion_bone,
unused_2,
) = buffer.read_fmt('<fI9I')
) = buffer.read_fmt('fI9I')
linear_movement = buffer.read_fmt('3f')
blend_count = buffer.read_uint32()
anim_offset = buffer.read_uint32()
Expand Down
17 changes: 14 additions & 3 deletions library/models/mdl/v6/structs/studioheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ def from_buffer(cls, buffer: Buffer):
assert version in [6, 10], f'MDL version {version} are not supported by GoldSrc importer'
name = buffer.read_ascii_string(64)
file_size = buffer.read_int32()
hdr_data = buffer.read_fmt('16I')
buffer.skip(12 * 4)
return cls(version, name, file_size, *hdr_data)

(
bone_count, bone_offset,
bone_controllers_count, bone_controllers_offset,
sequence_count, sequence_offset,
texture_count, texture_offset, texture_data_offset,
skin_ref_count, skin_families_count, skin_offset,
body_part_count, body_part_offset,
) = buffer.read_fmt('14I')

buffer.skip(14 * 4)
return cls(version, name, file_size, bone_count, bone_offset, bone_controllers_count, bone_controllers_offset,
sequence_count, sequence_offset, 0, 0, texture_count, texture_offset, texture_data_offset,
skin_ref_count, skin_families_count, skin_offset, body_part_count, body_part_offset)

0 comments on commit 4cdd1d6

Please sign in to comment.