Repository navigation
Expand file tree
/
Copy pathbfut_mywrappers.py
More file actions
159 lines (140 loc) · 6.58 KB
/
Copy pathbfut_mywrappers.py
File metadata and controls
159 lines (140 loc) · 6.58 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Copyright (C) 2021 and later Benjamin Futasz <https://github.com/bfut>
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
"""
bfut_mywrappers.py - wrapping i/o functions etc.
"""
import pathlib
import fcecodec as fc
import numpy as np
def ReorderTriagsTransparentDetachedAndToLast(mesh, pid_opaq):
""" Copy original part, delete semi-transparent triags in original,
delete opaque triags in copy, clean-up unreferenced verts, merge parts,
delete temporary & original, move merged part to original index """
pid_transp = mesh.OpCopyPart(pid_opaq)
mesh.OpDeletePartTriags(pid_opaq, np.where(mesh.PGetTriagsFlags(pid_opaq) & 0x8 == 0x8)[0])
mesh.OpDeletePartTriags(pid_transp, np.where(mesh.PGetTriagsFlags(pid_transp) & 0x8 < 0x8)[0])
mesh.OpDelUnrefdVerts()
new_pid = mesh.OpMergeParts(pid_opaq, pid_transp) # last part idx
mesh.PSetName(new_pid, mesh.PGetName(pid_opaq))
mesh.OpDeletePart(pid_transp)
mesh.OpDeletePart(pid_opaq)
new_pid -= 2 # last part idx is now smaller
while new_pid > pid_opaq:
new_pid = mesh.OpMovePart(new_pid) # move merged part to original index
return mesh
def HiBody_ReorderTriagsTransparentToLast(mesh, version):
""" Not implemented for FCE4M because windows are separate parts """
if version in ("3", 3):
mesh = ReorderTriagsTransparentDetachedAndToLast(mesh, 0) # high body
if mesh.MNumParts > 12:
mesh = ReorderTriagsTransparentDetachedAndToLast(mesh, 12) # high headlights
elif version in ("4", 4):
for partname in (":HB", ":OT", ":OL"):
pid = GetMeshPartnameIdx(mesh, partname)
if pid >= 0:
mesh = ReorderTriagsTransparentDetachedAndToLast(mesh, pid)
return mesh
def GetFceVersion(path):
version = fc.GetFceVersion(pathlib.Path(path).read_bytes())
assert version > 0
return version
def PrintFceInfo(path):
buf = pathlib.Path(path).read_bytes()
fc.PrintFceInfo(buf)
def LoadFce(mesh, path):
mesh.IoDecode(pathlib.Path(path).read_bytes())
return mesh
def WriteFce(version, mesh, path, center_parts=False, mesh_function=None):
if mesh_function is not None: # e.g., HiBody_ReorderTriagsTransparentToLast
mesh = mesh_function(mesh, version)
if version in ("3", 3):
buf = mesh.IoEncode_Fce3(center_parts)
elif version in ("4", 4):
buf = mesh.IoEncode_Fce4(center_parts)
else:
buf = mesh.IoEncode_Fce4M(center_parts)
assert fc.ValidateFce(buf) == 1
pathlib.Path(path).write_bytes(buf)
def ExportObj(mesh, objpath, mtlpath, texname,
print_damage, print_dummies, use_part_positions, print_part_positions,
filter_triagflags_0xfff=True):
mesh.IoExportObj(str(objpath), str(mtlpath), str(texname),
print_damage, print_dummies, use_part_positions, print_part_positions,
filter_triagflags_0xfff)
def GetMeshPartnames(mesh):
return [mesh.PGetName(pid) for pid in range(mesh.MNumParts)]
def GetMeshPartnameIdx(mesh, partname, verbose=True):
for pid in range(mesh.MNumParts):
if mesh.PGetName(pid) == partname:
return pid
if verbose:
print(f"GetMeshPartnameIdx: Warning: cannot find '{partname}'")
return -1
def GetPartGlobalOrderVidxs(mesh, pid):
map_verts = mesh.MVertsGetMap_idx2order
part_vidxs = mesh.PGetTriagsVidx(pid)
for i in range(part_vidxs.shape[0]):
# print(part_vidxs[i], map_verts[part_vidxs[i]])
part_vidxs[i] = map_verts[part_vidxs[i]]
return part_vidxs
def FilterTexpageTriags(mesh, drop_texpages: int | list | None = None, select_texpages: int | list | None = None):
# Delete triangles with texpage in drop_texpages
if drop_texpages is not None and select_texpages is None:
if not isinstance(drop_texpages, list):
drop_texpages = [drop_texpages]
for pid in reversed(range(mesh.MNumParts)):
texp = mesh.PGetTriagsTexpages(pid)
mask = np.isin(texp, drop_texpages)
x = mask.nonzero()[0]
assert mesh.OpDeletePartTriags(pid, x)
# Delete triangles with texpage not in select_texpages
elif drop_texpages is None and select_texpages is not None:
print(f"FilterTexpageTriags: select_texpages={select_texpages}")
if not isinstance(select_texpages, list):
select_texpages = [select_texpages]
for pid in reversed(range(mesh.MNumParts)):
print(f"before mesh.PNumTriags(pid)={mesh.PNumTriags(pid)}")
texp = mesh.PGetTriagsTexpages(pid)
mask = np.isin(texp, select_texpages)
mask = np.invert(mask)
x = mask.nonzero()[0]
print(mesh.PGetName(pid), texp.shape, x.shape, np.unique(texp))
assert mesh.OpDeletePartTriags(pid, x)
print(f"after: mesh.PNumTriags(pid)={mesh.PNumTriags(pid)}")
else:
raise ValueError("FilterTexpageTriags: call with either drop_texpages or select_texpages, not both")
# assert mesh.OpDelUnrefdVerts()
return mesh
def DeleteEmptyParts(mesh):
for pid in reversed(range(mesh.MNumParts)):
if mesh.PNumTriags(pid) == 0:
mesh.OpDeletePart(pid)
return mesh
def GetVertsAtIdx(v_arr, vidx: int | list | np.ndarray) -> np.ndarray:
vidx = np.array(vidx)
v_arr = v_arr.reshape(-1, 3)
if np.max(vidx) >= v_arr.shape[0]:
raise IndexError(f"vertex index {np.max(vidx)} is out of bounds for {v_arr.shape[0]} vertices")
return v_arr[vidx].ravel()
def SetVertsAtIdx(v_arr, values, vidx: int | list | np.ndarray, ) -> np.ndarray:
vidx = np.array(vidx)
v_arr = v_arr.reshape(-1, 3)
if np.max(vidx) >= v_arr.shape[0]:
raise IndexError(f"vertex index {np.max(vidx)} is out of bounds for {v_arr.shape[0]} vertices")
v_arr[vidx] = values.reshape(-1, 3)
return v_arr.ravel()