forked from K0lb3/UnityPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeshExporter.py
More file actions
85 lines (73 loc) · 2.57 KB
/
Copy pathMeshExporter.py
File metadata and controls
85 lines (73 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from UnityPy.classes import Mesh
def export_mesh(m_Mesh: Mesh, format="obj") -> str:
if format == "obj":
return export_mesh_obj(m_Mesh)
raise NotImplementedError(f"Export format {format} not implemented")
def export_mesh_obj(m_Mesh, material_names: list = None):
if m_Mesh.m_VertexCount <= 0:
return False
sb = [f"g {m_Mesh.name}\r\n"]
if material_names:
sb.append(f"mtllib {m_Mesh.name}.mtl\r\n")
# region Vertices
if not m_Mesh.m_Vertices:
return False
c = 3
if len(m_Mesh.m_Vertices) == m_Mesh.m_VertexCount * 4:
c = 4
for v in range(int(m_Mesh.m_VertexCount)):
sb.append(
"v {0:.7G} {1:.7G} {2:.7G}\r\n".format(
-m_Mesh.m_Vertices[v * c],
m_Mesh.m_Vertices[v * c + 1],
m_Mesh.m_Vertices[v * c + 2],
).replace("nan", "0")
)
# endregion
# region UV
if m_Mesh.m_UV0:
if len(m_Mesh.m_UV0) == m_Mesh.m_VertexCount * 2:
c = 2
elif len(m_Mesh.m_UV0) == m_Mesh.m_VertexCount * 3:
c = 3
for v in range(int(m_Mesh.m_VertexCount)):
sb.append(
"vt {0:.7G} {1:.7G}\r\n".format(
m_Mesh.m_UV0[v * c], m_Mesh.m_UV0[v * c + 1]
).replace("nan", "0")
)
# endregion
# region Normals
if m_Mesh.m_Normals:
if len(m_Mesh.m_Normals) == m_Mesh.m_VertexCount * 3:
c = 3
elif len(m_Mesh.m_Normals) == m_Mesh.m_VertexCount * 4:
c = 4
for v in range(int(m_Mesh.m_VertexCount)):
sb.append(
"vn {0:.7G} {1:.7G} {2:.7G}\r\n".format(
-m_Mesh.m_Normals[v * c],
m_Mesh.m_Normals[v * c + 1],
m_Mesh.m_Normals[v * c + 2],
).replace("nan", "0")
)
# endregion
# region Face
sum = 0
for i in range(len(m_Mesh.m_SubMeshes)):
sb.append(f"g {m_Mesh.name}_{i}\r\n")
if material_names and i < len(material_names) and material_names[i]:
sb.append(f"usemtl {material_names[i]}\r\n")
indexCount = m_Mesh.m_SubMeshes[i].indexCount
end = sum + indexCount // 3
for f in range(sum, end):
sb.append(
"f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\r\n".format(
m_Mesh.m_Indices[f * 3 + 2] + 1,
m_Mesh.m_Indices[f * 3 + 1] + 1,
m_Mesh.m_Indices[f * 3] + 1,
)
)
sum = end
# endregion
return "".join(sb)