Skip to content

Commit 9b0aefa

Browse files
committed
MeshHelper - fix get_triangles for Strip and Quad
1 parent 0ed5f2f commit 9b0aefa

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

UnityPy/helpers/MeshHelper.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ def flat_list_to_tuples(data: Sequence[T], item_size: int) -> List[tuple[T, ...]
4141
return [tuple(data[i : i + item_size]) for i in range(0, len(data), item_size)]
4242

4343

44-
def vector_list_to_tuples(data: List[Union[Vector2f, Vector3f, Vector4f]]) -> List[tuple]:
44+
def vector_list_to_tuples(
45+
data: List[Union[Vector2f, Vector3f, Vector4f]],
46+
) -> List[tuple]:
4547
if isinstance(data[0], Vector2f):
4648
return [(v.x, v.y) for v in data]
4749
elif isinstance(data[0], Vector3f):
@@ -396,9 +398,7 @@ def read_vertex_data(
396398
component_data = struct.unpack(
397399
f">{count}{component_dtype}", componentBytes
398400
)
399-
component_data = flat_list_to_tuples(
400-
component_data, channel_dimension
401-
)
401+
component_data = flat_list_to_tuples(component_data, channel_dimension)
402402

403403
self.assign_channel_vertex_data(chn, component_data)
404404

@@ -653,16 +653,17 @@ def get_triangles(self) -> List[List[Tuple[int, ...]]]:
653653
indexCount = m_SubMesh.indexCount
654654
topology = m_SubMesh.topology
655655

656-
triangles: List[int]
656+
triangles: List[Tuple[int, ...]]
657657

658658
if topology == MeshTopology.Triangles:
659-
triangles = self.m_IndexBuffer[firstIndex : firstIndex + indexCount]
659+
triangles = self.m_IndexBuffer[firstIndex : firstIndex + indexCount] # type: ignore
660+
triangles = [triangles[i : i + 3] for i in range(0, len(triangles), 3)] # type: ignore
660661
elif (
661662
self.version[0] < 4 or topology == MeshTopology.TriangleStrip
662663
): # TriangleStrip
663664
# todo: use as_strided, then fix winding, finally remove degenerates
664665
triIndex = 0
665-
triangles = list((indexCount - 2) * 3)
666+
triangles = [None] * (indexCount - 2) # type: ignore
666667

667668
for i in range(indexCount - 2):
668669
a, b, c = self.m_IndexBuffer[firstIndex + i : firstIndex + i + 3]
@@ -682,11 +683,10 @@ def get_triangles(self) -> List[List[Tuple[int, ...]]]:
682683
elif topology == MeshTopology.Quads:
683684
# one quad is two triangles, so // 4 * 2 = // 2
684685
# TODO: use as_strided
685-
triangles = list(indexCount // 2, 3)
686+
triangles = [None] * (indexCount // 2) # type: ignore
686687
triIndex = 0
687-
for a, b, c, d in self.m_IndexBuffer[
688-
firstIndex : firstIndex + indexCount : 4
689-
]:
688+
for i in range(firstIndex, firstIndex+indexCount,4):
689+
a,b,c,d = self.m_IndexBuffer[i:i+4]
690690
triangles[triIndex] = a, b, c
691691
triangles[triIndex + 1] = a, c, d
692692
triIndex += 2
@@ -695,7 +695,6 @@ def get_triangles(self) -> List[List[Tuple[int, ...]]]:
695695
"Failed getting triangles. Submesh topology is lines or points."
696696
)
697697

698-
triangles = [triangles[i : i + 3] for i in range(0, len(triangles), 3)]
699698
submeshes.append(triangles)
700699

701700
return submeshes

0 commit comments

Comments
 (0)