Several Improvements

- Fixes decode issues.
This commit is contained in:
Yusarina
2024-06-14 01:16:14 +01:00
parent db455e6b61
commit 03c1fee622
+12 -14
View File
@@ -21,10 +21,10 @@ def read_pmx_header(file):
rigid_body_index_size = struct.unpack('<b', file.read(1))[0] rigid_body_index_size = struct.unpack('<b', file.read(1))[0]
# Read model name and comments # Read model name and comments
model_name = file.read(struct.unpack('<i', file.read(4))[0]).decode('utf-16-le') model_name = str(file.read(struct.unpack('<i', file.read(4))[0]), 'utf-16-le', errors='replace')
model_english_name = file.read(struct.unpack('<i', file.read(4))[0]).decode('utf-16-le') model_english_name = str(file.read(struct.unpack('<i', file.read(4))[0]), 'utf-16-le', errors='replace')
model_comment = file.read(struct.unpack('<i', file.read(4))[0]).decode('utf-16-le') model_comment = str(file.read(struct.unpack('<i', file.read(4))[0]), 'utf-16-le', errors='replace')
model_english_comment = file.read(struct.unpack('<i', file.read(4))[0]).decode('utf-16-le') model_english_comment = str(file.read(struct.unpack('<i', file.read(4))[0]), 'utf-16-le', errors='replace')
return version, encoding, additional_uvs, vertex_index_size, texture_index_size, material_index_size, bone_index_size, morph_index_size, rigid_body_index_size, model_name, model_english_name, model_comment, model_english_comment return version, encoding, additional_uvs, vertex_index_size, texture_index_size, material_index_size, bone_index_size, morph_index_size, rigid_body_index_size, model_name, model_english_name, model_comment, model_english_comment
@@ -46,8 +46,8 @@ def read_vertex(file, vertex_index_size):
return position, normal, uv, bone_indices, bone_weights, edge_scale return position, normal, uv, bone_indices, bone_weights, edge_scale
def read_material(file, texture_index_size): def read_material(file, texture_index_size):
material_name = file.read(struct.unpack('<i', file.read(4))[0]).decode('utf-16-le') material_name = str(file.read(struct.unpack('<i', file.read(4))[0]), 'utf-16-le', errors='replace')
material_english_name = file.read(struct.unpack('<i', file.read(4))[0]).decode('utf-16-le') material_english_name = str(file.read(struct.unpack('<i', file.read(4))[0]), 'utf-16-le', errors='replace')
diffuse_color = struct.unpack('<4f', file.read(16)) diffuse_color = struct.unpack('<4f', file.read(16))
specular_color = struct.unpack('<3f', file.read(12)) specular_color = struct.unpack('<3f', file.read(12))
@@ -67,13 +67,13 @@ def read_material(file, texture_index_size):
else: else:
toon_texture_index = struct.unpack('<b', file.read(1))[0] toon_texture_index = struct.unpack('<b', file.read(1))[0]
comment = file.read(struct.unpack('<i', file.read(4))[0]).decode('utf-16-le') comment = str(file.read(struct.unpack('<i', file.read(4))[0]), 'utf-16-le', errors='replace')
return material_name, material_english_name, diffuse_color, specular_color, specular_strength, ambient_color, flag, edge_color, edge_size, texture_index, sphere_texture_index, sphere_mode, toon_sharing_flag, toon_texture_index, comment return material_name, material_english_name, diffuse_color, specular_color, specular_strength, ambient_color, flag, edge_color, edge_size, texture_index, sphere_texture_index, sphere_mode, toon_sharing_flag, toon_texture_index, comment
def read_bone(file, bone_index_size): def read_bone(file, bone_index_size):
bone_name = file.read(struct.unpack('<i', file.read(4))[0]).decode('utf-16-le') bone_name = str(file.read(struct.unpack('<i', file.read(4))[0]), 'utf-16-le', errors='replace')
bone_english_name = file.read(struct.unpack('<i', file.read(4))[0]).decode('utf-16-le') bone_english_name = str(file.read(struct.unpack('<i', file.read(4))[0]), 'utf-16-le', errors='replace')
position = struct.unpack('<3f', file.read(12)) position = struct.unpack('<3f', file.read(12))
parent_bone_index = struct.unpack(f'<{bone_index_size}B', file.read(bone_index_size))[0] parent_bone_index = struct.unpack(f'<{bone_index_size}B', file.read(bone_index_size))[0]
@@ -115,8 +115,8 @@ def read_bone(file, bone_index_size):
return bone_name, bone_english_name, position, parent_bone_index, layer, flag, tail_position, inherit_bone_parent_index, inherit_bone_parent_influence, fixed_axis, local_x_vector, local_z_vector, external_key, ik_target_bone_index, ik_loop_count, ik_limit_radian, ik_links return bone_name, bone_english_name, position, parent_bone_index, layer, flag, tail_position, inherit_bone_parent_index, inherit_bone_parent_influence, fixed_axis, local_x_vector, local_z_vector, external_key, ik_target_bone_index, ik_loop_count, ik_limit_radian, ik_links
def read_morph(file, morph_index_size): def read_morph(file, morph_index_size):
morph_name = file.read(struct.unpack('<i', file.read(4))[0]).decode('utf-16-le') morph_name = str(file.read(struct.unpack('<i', file.read(4))[0]), 'utf-16-le', errors='replace')
morph_english_name = file.read(struct.unpack('<i', file.read(4))[0]).decode('utf-16-le') morph_english_name = str(file.read(struct.unpack('<i', file.read(4))[0]), 'utf-16-le', errors='replace')
panel = struct.unpack('<b', file.read(1))[0] panel = struct.unpack('<b', file.read(1))[0]
morph_type = struct.unpack('<b', file.read(1))[0] morph_type = struct.unpack('<b', file.read(1))[0]
@@ -196,9 +196,7 @@ def import_pmx(filepath):
texture_count = struct.unpack('<i', file.read(4))[0] texture_count = struct.unpack('<i', file.read(4))[0]
textures = [] textures = []
for _ in range(texture_count): for _ in range(texture_count):
texture_path_length = struct.unpack('<i', file.read(4))[0] texture_path = str(file.read(struct.unpack('<i', file.read(4))[0]), 'utf-16-le', errors='replace')
texture_path_data = file.read(texture_path_length)
texture_path = texture_path_data.decode('utf-16-le', errors='replace').rstrip('\0')
textures.append(texture_path) textures.append(texture_path)
# Read materials # Read materials