unityripper 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- unityripper/__init__.py +1 -0
- unityripper/__main__.py +3 -0
- unityripper/cli.py +591 -0
- unityripper/discovery.py +165 -0
- unityripper/exporters/__init__.py +5 -0
- unityripper/exporters/common.py +95 -0
- unityripper/exporters/fbx.py +500 -0
- unityripper/exporters/glb.py +654 -0
- unityripper/exporters/obj.py +293 -0
- unityripper/materials.py +230 -0
- unityripper/mathutil.py +133 -0
- unityripper/scene.py +720 -0
- unityripper/textures.py +321 -0
- unityripper/util.py +105 -0
- unityripper-1.0.0.dist-info/METADATA +10 -0
- unityripper-1.0.0.dist-info/RECORD +19 -0
- unityripper-1.0.0.dist-info/WHEEL +5 -0
- unityripper-1.0.0.dist-info/entry_points.txt +2 -0
- unityripper-1.0.0.dist-info/top_level.txt +1 -0
unityripper/scene.py
ADDED
|
@@ -0,0 +1,720 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
import time
|
|
5
|
+
from collections import Counter, defaultdict
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any, Iterable, Sequence
|
|
9
|
+
|
|
10
|
+
import UnityPy
|
|
11
|
+
from UnityPy.helpers.MeshHelper import MeshHandler
|
|
12
|
+
|
|
13
|
+
from .discovery import InputSet
|
|
14
|
+
from .materials import MaterialData, read_material
|
|
15
|
+
from .mathutil import Mat4, Vec2, Vec3, Vec4, IDENTITY, mat_mul, matrix_from_unity, trs_matrix
|
|
16
|
+
from .util import asset_file_of, log, object_identity, object_path_id, sanitize_filename, unique_name
|
|
17
|
+
|
|
18
|
+
RENDERER_TYPES = {"MeshRenderer", "SkinnedMeshRenderer"}
|
|
19
|
+
MESH_TOPOLOGY_TRIANGLES = 0
|
|
20
|
+
MESH_TOPOLOGY_TRIANGLE_STRIP = 1
|
|
21
|
+
MESH_TOPOLOGY_QUADS = 2
|
|
22
|
+
|
|
23
|
+
@dataclass
|
|
24
|
+
class MeshData:
|
|
25
|
+
key: tuple[str, int]
|
|
26
|
+
name: str
|
|
27
|
+
source: str
|
|
28
|
+
vertices: list[Vec3]
|
|
29
|
+
uvs: list[Vec2] | None
|
|
30
|
+
normals: list[Vec3] | None
|
|
31
|
+
colors: list[Vec4] | None
|
|
32
|
+
triangles: list[list[tuple[int, int, int]]]
|
|
33
|
+
submesh_count: int
|
|
34
|
+
vertex_count: int
|
|
35
|
+
triangle_count: int
|
|
36
|
+
joints: list[tuple[int, int, int, int]] | None = None
|
|
37
|
+
weights: list[tuple[float, float, float, float]] | None = None
|
|
38
|
+
error: str | None = None
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def ok(self) -> bool:
|
|
42
|
+
return bool(self.vertices) and self.triangle_count > 0
|
|
43
|
+
|
|
44
|
+
@dataclass
|
|
45
|
+
class MeshAsset:
|
|
46
|
+
mesh: MeshData
|
|
47
|
+
name: str
|
|
48
|
+
materials: list[MaterialData]
|
|
49
|
+
instances: int = 1
|
|
50
|
+
source: str = ""
|
|
51
|
+
scale: float = 1.0
|
|
52
|
+
matrix: Mat4 = IDENTITY
|
|
53
|
+
|
|
54
|
+
@dataclass
|
|
55
|
+
class _AssetGroup:
|
|
56
|
+
|
|
57
|
+
mesh: MeshData
|
|
58
|
+
materials: list[MaterialData]
|
|
59
|
+
instances: int = 0
|
|
60
|
+
worlds: list[Mat4] = field(default_factory=list)
|
|
61
|
+
|
|
62
|
+
@dataclass
|
|
63
|
+
class RendererModel:
|
|
64
|
+
obj: Any
|
|
65
|
+
gameobject: Any
|
|
66
|
+
path: str
|
|
67
|
+
world: Mat4
|
|
68
|
+
mesh: MeshData | None
|
|
69
|
+
materials: list[MaterialData]
|
|
70
|
+
is_skinned: bool
|
|
71
|
+
bones: list[Any] = field(default_factory=list)
|
|
72
|
+
bindposes: list[Mat4] = field(default_factory=list)
|
|
73
|
+
enabled: bool = True
|
|
74
|
+
active: bool = True
|
|
75
|
+
source: str = ""
|
|
76
|
+
|
|
77
|
+
@property
|
|
78
|
+
def name(self) -> str:
|
|
79
|
+
if self.mesh is not None and not self.is_skinned:
|
|
80
|
+
return self.mesh.name
|
|
81
|
+
return self.path.split("/")[-1] if self.path else self.mesh.name if self.mesh else "model"
|
|
82
|
+
|
|
83
|
+
def _reader_key(obj: Any, base: str | None = None) -> tuple[str, int]:
|
|
84
|
+
reader = getattr(obj, "object_reader", None)
|
|
85
|
+
target = reader if reader is not None else obj
|
|
86
|
+
return object_identity(target, base)
|
|
87
|
+
|
|
88
|
+
def _alive(ptr: Any) -> bool:
|
|
89
|
+
if ptr is None:
|
|
90
|
+
return False
|
|
91
|
+
try:
|
|
92
|
+
return object_path_id(ptr) != 0
|
|
93
|
+
except Exception:
|
|
94
|
+
return False
|
|
95
|
+
|
|
96
|
+
def _read(ptr: Any) -> Any:
|
|
97
|
+
return ptr.read() if _alive(ptr) else None
|
|
98
|
+
|
|
99
|
+
def _vec(value: Any, size: int) -> tuple[float, ...]:
|
|
100
|
+
if value is None:
|
|
101
|
+
return tuple(0.0 for _ in range(size))
|
|
102
|
+
try:
|
|
103
|
+
if size == 2:
|
|
104
|
+
return (float(value.x), float(value.y))
|
|
105
|
+
if size == 3:
|
|
106
|
+
return (float(value.x), float(value.y), float(value.z))
|
|
107
|
+
if size == 4:
|
|
108
|
+
return (float(value.x), float(value.y), float(value.z), float(value.w))
|
|
109
|
+
except Exception:
|
|
110
|
+
pass
|
|
111
|
+
try:
|
|
112
|
+
return tuple(float(value[i]) for i in range(size))
|
|
113
|
+
except Exception:
|
|
114
|
+
return tuple(0.0 for _ in range(size))
|
|
115
|
+
|
|
116
|
+
def load_environment(input_set: InputSet, decrypt_key: str | None = None) -> UnityPy.Environment:
|
|
117
|
+
if decrypt_key:
|
|
118
|
+
try:
|
|
119
|
+
UnityPy.set_assetbundle_decrypt_key(bytes.fromhex(decrypt_key))
|
|
120
|
+
except ValueError as exc:
|
|
121
|
+
raise ValueError(f"invalid --key (expected hex): {exc}") from exc
|
|
122
|
+
|
|
123
|
+
env = UnityPy.Environment()
|
|
124
|
+
roots = [*input_set.data_dirs, *input_set.game_roots]
|
|
125
|
+
if roots:
|
|
126
|
+
env.path = str(roots[0])
|
|
127
|
+
elif input_set.files:
|
|
128
|
+
env.path = str(input_set.files[0].parent)
|
|
129
|
+
|
|
130
|
+
errors: list[str] = []
|
|
131
|
+
for path in input_set.files:
|
|
132
|
+
try:
|
|
133
|
+
env.load_file(str(path))
|
|
134
|
+
except Exception as exc:
|
|
135
|
+
message = f"{path.name}: {type(exc).__name__}: {exc}"
|
|
136
|
+
errors.append(message)
|
|
137
|
+
log.warning("failed to load %s", message)
|
|
138
|
+
for archive in input_set.archives:
|
|
139
|
+
try:
|
|
140
|
+
env.load_zip_file(str(archive))
|
|
141
|
+
except Exception as exc:
|
|
142
|
+
message = f"{archive.name}: {type(exc).__name__}: {exc}"
|
|
143
|
+
errors.append(message)
|
|
144
|
+
log.warning("failed to load %s", message)
|
|
145
|
+
|
|
146
|
+
if not env.files:
|
|
147
|
+
detail = "; ".join(errors[:3]) if errors else "no Unity data"
|
|
148
|
+
raise RuntimeError(f"nothing could be loaded ({detail})")
|
|
149
|
+
|
|
150
|
+
input_set.notes.extend(f"load error: {e}" for e in errors)
|
|
151
|
+
log.info("loaded %d file(s), %d object(s)", len(env.files), len(env.objects))
|
|
152
|
+
return env
|
|
153
|
+
|
|
154
|
+
class UnityScene:
|
|
155
|
+
def __init__(self, env: UnityPy.Environment, sources: Sequence[str] = ()):
|
|
156
|
+
self.env = env
|
|
157
|
+
self.sources = list(sources)
|
|
158
|
+
self.renderers: list[RendererModel] = []
|
|
159
|
+
self.meshes: dict[tuple[str, int], MeshData] = {}
|
|
160
|
+
self.materials: dict[tuple[str, int], MaterialData] = {}
|
|
161
|
+
self.issues: list[dict[str, str]] = []
|
|
162
|
+
self._world_cache: dict[tuple[str, int], Mat4] = {}
|
|
163
|
+
self._path_cache: dict[tuple[str, int], str] = {}
|
|
164
|
+
self._active_cache: dict[tuple[str, int], bool] = {}
|
|
165
|
+
self._mesh_by_key: dict[tuple[str, int], Any] = {}
|
|
166
|
+
self._asset_groups: dict[tuple[str, int], _AssetGroup] = {}
|
|
167
|
+
self._assets_scanned = False
|
|
168
|
+
self._object_counts: Counter[str] = Counter()
|
|
169
|
+
|
|
170
|
+
def _issue(self, message: str, obj: Any = None) -> None:
|
|
171
|
+
entry = {"message": message}
|
|
172
|
+
if obj is not None:
|
|
173
|
+
entry["object"] = f"{obj.type.name}#{getattr(obj, 'path_id', 0)}"
|
|
174
|
+
self.issues.append(entry)
|
|
175
|
+
log.warning(message)
|
|
176
|
+
|
|
177
|
+
def _source_of(self, obj: Any) -> str:
|
|
178
|
+
assets_file = asset_file_of(obj)
|
|
179
|
+
name = getattr(assets_file, "name", "") if assets_file is not None else ""
|
|
180
|
+
return Path(str(name)).name if name else ""
|
|
181
|
+
|
|
182
|
+
@staticmethod
|
|
183
|
+
def _resolve_transform(transform: Any) -> Any:
|
|
184
|
+
if transform is None:
|
|
185
|
+
return None
|
|
186
|
+
if not hasattr(transform, "m_LocalScale") and hasattr(transform, "read"):
|
|
187
|
+
try:
|
|
188
|
+
return transform.read()
|
|
189
|
+
except Exception:
|
|
190
|
+
return None
|
|
191
|
+
return transform
|
|
192
|
+
|
|
193
|
+
def _transform_of(self, gameobject: Any) -> Any:
|
|
194
|
+
return self._resolve_transform(getattr(gameobject, "m_Transform", None))
|
|
195
|
+
|
|
196
|
+
def _local_matrix(self, transform: Any) -> Mat4:
|
|
197
|
+
position = _vec(getattr(transform, "m_LocalPosition", None), 3)
|
|
198
|
+
rotation = _vec(getattr(transform, "m_LocalRotation", None), 4)
|
|
199
|
+
scale = _vec(getattr(transform, "m_LocalScale", None), 3)
|
|
200
|
+
return trs_matrix(position, rotation, scale)
|
|
201
|
+
|
|
202
|
+
def world_matrix(self, transform: Any, _depth: int = 0) -> Mat4:
|
|
203
|
+
if transform is None:
|
|
204
|
+
return IDENTITY
|
|
205
|
+
key = _reader_key(transform, getattr(self.env, "path", None))
|
|
206
|
+
cached = self._world_cache.get(key)
|
|
207
|
+
if cached is not None:
|
|
208
|
+
return cached
|
|
209
|
+
if _depth > 512:
|
|
210
|
+
self._issue("transform hierarchy deeper than 512, truncating")
|
|
211
|
+
return self._local_matrix(transform)
|
|
212
|
+
local = self._local_matrix(transform)
|
|
213
|
+
father_ptr = getattr(transform, "m_Father", None)
|
|
214
|
+
if _alive(father_ptr):
|
|
215
|
+
father = _read(father_ptr)
|
|
216
|
+
world = mat_mul(self.world_matrix(father, _depth + 1), local)
|
|
217
|
+
else:
|
|
218
|
+
world = local
|
|
219
|
+
self._world_cache[key] = world
|
|
220
|
+
return world
|
|
221
|
+
|
|
222
|
+
def node_path(self, gameobject: Any) -> str:
|
|
223
|
+
transform = self._transform_of(gameobject)
|
|
224
|
+
if transform is None:
|
|
225
|
+
return str(getattr(gameobject, "m_Name", "object") or "object")
|
|
226
|
+
key = _reader_key(transform, getattr(self.env, "path", None))
|
|
227
|
+
cached = self._path_cache.get(key)
|
|
228
|
+
if cached is not None:
|
|
229
|
+
return cached
|
|
230
|
+
names: list[str] = []
|
|
231
|
+
current = transform
|
|
232
|
+
for _ in range(512):
|
|
233
|
+
go = _read(getattr(current, "m_GameObject", None))
|
|
234
|
+
names.append(str(getattr(go, "m_Name", "") or "object"))
|
|
235
|
+
father_ptr = getattr(current, "m_Father", None)
|
|
236
|
+
if not _alive(father_ptr):
|
|
237
|
+
break
|
|
238
|
+
current = _read(father_ptr)
|
|
239
|
+
if current is None:
|
|
240
|
+
break
|
|
241
|
+
path = "/".join(reversed(names))
|
|
242
|
+
self._path_cache[key] = path
|
|
243
|
+
return path
|
|
244
|
+
|
|
245
|
+
def is_active(self, gameobject: Any) -> bool:
|
|
246
|
+
transform = self._transform_of(gameobject)
|
|
247
|
+
if transform is None:
|
|
248
|
+
return bool(getattr(gameobject, "m_IsActive", 1))
|
|
249
|
+
key = _reader_key(transform, getattr(self.env, "path", None))
|
|
250
|
+
if key in self._active_cache:
|
|
251
|
+
return self._active_cache[key]
|
|
252
|
+
active = True
|
|
253
|
+
current_go = gameobject
|
|
254
|
+
current_tr = transform
|
|
255
|
+
for _ in range(512):
|
|
256
|
+
if not bool(getattr(current_go, "m_IsActive", 1)):
|
|
257
|
+
active = False
|
|
258
|
+
break
|
|
259
|
+
father_ptr = getattr(current_tr, "m_Father", None)
|
|
260
|
+
if not _alive(father_ptr):
|
|
261
|
+
break
|
|
262
|
+
father = _read(father_ptr)
|
|
263
|
+
if father is None:
|
|
264
|
+
break
|
|
265
|
+
current_tr = father
|
|
266
|
+
current_go = _read(getattr(father, "m_GameObject", None))
|
|
267
|
+
if current_go is None:
|
|
268
|
+
break
|
|
269
|
+
self._active_cache[key] = active
|
|
270
|
+
return active
|
|
271
|
+
|
|
272
|
+
def get_mesh(self, mesh_ptr: Any) -> MeshData | None:
|
|
273
|
+
if not _alive(mesh_ptr):
|
|
274
|
+
return None
|
|
275
|
+
key = _reader_key(mesh_ptr, getattr(self.env, "path", None))
|
|
276
|
+
cached = self.meshes.get(key)
|
|
277
|
+
if cached is not None:
|
|
278
|
+
return cached
|
|
279
|
+
try:
|
|
280
|
+
mesh = mesh_ptr.read()
|
|
281
|
+
except Exception as exc:
|
|
282
|
+
self._issue(f"mesh #{key[1]} could not be parsed: {type(exc).__name__}: {exc}", mesh_ptr)
|
|
283
|
+
return None
|
|
284
|
+
|
|
285
|
+
name = str(getattr(mesh, "m_Name", "") or f"mesh_{key[1]}")
|
|
286
|
+
source = self._source_of(getattr(mesh, "object_reader", mesh_ptr))
|
|
287
|
+
error: str | None = None
|
|
288
|
+
vertices: list[Vec3] = []
|
|
289
|
+
uvs: list[Vec2] | None = None
|
|
290
|
+
normals: list[Vec3] | None = None
|
|
291
|
+
colors: list[Vec4] | None = None
|
|
292
|
+
joints: list[tuple[int, int, int, int]] | None = None
|
|
293
|
+
weights: list[tuple[float, float, float, float]] | None = None
|
|
294
|
+
triangles: list[list[tuple[int, int, int]]] = []
|
|
295
|
+
submesh_count = 0
|
|
296
|
+
|
|
297
|
+
try:
|
|
298
|
+
handler = MeshHandler(mesh)
|
|
299
|
+
handler.process()
|
|
300
|
+
raw_vertices = handler.m_Vertices or []
|
|
301
|
+
vertices = [_vec(v, 3) for v in raw_vertices]
|
|
302
|
+
if handler.m_UV0:
|
|
303
|
+
uvs = [_vec(v, 2) for v in handler.m_UV0]
|
|
304
|
+
if handler.m_Normals:
|
|
305
|
+
normals = [_vec(v, 3) for v in handler.m_Normals]
|
|
306
|
+
if handler.m_Colors:
|
|
307
|
+
colors = [_vec(v, 4) for v in handler.m_Colors]
|
|
308
|
+
if handler.m_BoneIndices and handler.m_BoneWeights:
|
|
309
|
+
joints = [tuple(int(x) for x in v[:4]) for v in handler.m_BoneIndices]
|
|
310
|
+
weights = [tuple(float(x) for x in v[:4]) for v in handler.m_BoneWeights]
|
|
311
|
+
submeshes = list(getattr(mesh, "m_SubMeshes", None) or [])
|
|
312
|
+
submesh_count = len(submeshes)
|
|
313
|
+
triangles = self._extract_triangles(handler, submeshes, len(vertices))
|
|
314
|
+
except Exception as exc:
|
|
315
|
+
error = f"{type(exc).__name__}: {exc}"
|
|
316
|
+
self._issue(f"mesh '{name}' failed: {error}", mesh_ptr)
|
|
317
|
+
|
|
318
|
+
if not vertices:
|
|
319
|
+
raw = getattr(mesh, "m_Vertices", None) or []
|
|
320
|
+
vertices = [_vec(v, 3) for v in raw]
|
|
321
|
+
if normals is not None and len(normals) < len(vertices):
|
|
322
|
+
normals = None
|
|
323
|
+
if uvs is not None and len(uvs) < len(vertices):
|
|
324
|
+
uvs = None
|
|
325
|
+
if colors is not None and len(colors) < len(vertices):
|
|
326
|
+
colors = None
|
|
327
|
+
if joints is not None and len(joints) < len(vertices):
|
|
328
|
+
joints = None
|
|
329
|
+
if weights is not None and len(weights) < len(vertices):
|
|
330
|
+
weights = None
|
|
331
|
+
if joints is None or weights is None:
|
|
332
|
+
joints = weights = None
|
|
333
|
+
|
|
334
|
+
triangle_count = sum(len(t) for t in triangles)
|
|
335
|
+
data = MeshData(
|
|
336
|
+
key=key,
|
|
337
|
+
name=name,
|
|
338
|
+
source=source,
|
|
339
|
+
vertices=vertices,
|
|
340
|
+
uvs=uvs,
|
|
341
|
+
normals=normals,
|
|
342
|
+
colors=colors,
|
|
343
|
+
triangles=triangles,
|
|
344
|
+
submesh_count=submesh_count or 1,
|
|
345
|
+
vertex_count=len(vertices),
|
|
346
|
+
triangle_count=triangle_count,
|
|
347
|
+
joints=joints,
|
|
348
|
+
weights=weights,
|
|
349
|
+
error=error,
|
|
350
|
+
)
|
|
351
|
+
self.meshes[key] = data
|
|
352
|
+
log.debug("mesh %s: %d verts, %d tris, %d submeshes", name, len(vertices), triangle_count, data.submesh_count)
|
|
353
|
+
return data
|
|
354
|
+
|
|
355
|
+
@staticmethod
|
|
356
|
+
def _extract_triangles(handler: MeshHandler, submeshes: list[Any], vertex_count: int) -> list[list[tuple[int, int, int]]]:
|
|
357
|
+
if not submeshes:
|
|
358
|
+
indices = handler.m_IndexBuffer
|
|
359
|
+
if not indices:
|
|
360
|
+
return []
|
|
361
|
+
return [
|
|
362
|
+
(indices[i], indices[i + 1], indices[i + 2])
|
|
363
|
+
for i in range(0, len(indices) - 2, 3)
|
|
364
|
+
if all(0 <= indices[i + k] < vertex_count for k in range(3))
|
|
365
|
+
]
|
|
366
|
+
|
|
367
|
+
index_buffer = handler.m_IndexBuffer
|
|
368
|
+
result: list[list[tuple[int, int, int]]] = []
|
|
369
|
+
for submesh in submeshes:
|
|
370
|
+
if not index_buffer:
|
|
371
|
+
result.append([])
|
|
372
|
+
continue
|
|
373
|
+
first = int(getattr(submesh, "firstByte", 0)) // 2
|
|
374
|
+
if not handler.m_Use16BitIndices:
|
|
375
|
+
first //= 2
|
|
376
|
+
count = int(getattr(submesh, "indexCount", 0))
|
|
377
|
+
topology = int(getattr(submesh, "topology", 0))
|
|
378
|
+
indices = index_buffer[first : first + count]
|
|
379
|
+
tris: list[tuple[int, int, int]] = []
|
|
380
|
+
if topology == MESH_TOPOLOGY_TRIANGLES:
|
|
381
|
+
for i in range(0, len(indices) - 2, 3):
|
|
382
|
+
a, b, c = indices[i], indices[i + 1], indices[i + 2]
|
|
383
|
+
if a != b and b != c and a != c and max(a, b, c) < vertex_count:
|
|
384
|
+
tris.append((a, b, c))
|
|
385
|
+
elif topology == MESH_TOPOLOGY_TRIANGLE_STRIP:
|
|
386
|
+
for i in range(len(indices) - 2):
|
|
387
|
+
a, b, c = indices[i], indices[i + 1], indices[i + 2]
|
|
388
|
+
if a == b or b == c or a == c or max(a, b, c) >= vertex_count:
|
|
389
|
+
continue
|
|
390
|
+
tris.append((b, a, c) if i & 1 else (a, b, c))
|
|
391
|
+
elif topology == MESH_TOPOLOGY_QUADS:
|
|
392
|
+
for i in range(0, len(indices) - 3, 4):
|
|
393
|
+
a, b, c, d = indices[i], indices[i + 1], indices[i + 2], indices[i + 3]
|
|
394
|
+
if max(a, b, c, d) < vertex_count:
|
|
395
|
+
tris.append((a, b, c))
|
|
396
|
+
tris.append((a, c, d))
|
|
397
|
+
result.append(tris)
|
|
398
|
+
return result
|
|
399
|
+
|
|
400
|
+
def get_material(self, material_ptr: Any) -> MaterialData:
|
|
401
|
+
if not _alive(material_ptr):
|
|
402
|
+
return MaterialData(key=None, name="Default", shader="")
|
|
403
|
+
key = _reader_key(material_ptr, getattr(self.env, "path", None))
|
|
404
|
+
cached = self.materials.get(key)
|
|
405
|
+
if cached is not None:
|
|
406
|
+
return cached
|
|
407
|
+
try:
|
|
408
|
+
data = read_material(material_ptr, getattr(self.env, "path", None))
|
|
409
|
+
except Exception as exc:
|
|
410
|
+
name = f"material_{key[1]}"
|
|
411
|
+
self._issue(f"material #{key[1]} failed: {type(exc).__name__}: {exc}", material_ptr)
|
|
412
|
+
data = MaterialData(key=key, name=name)
|
|
413
|
+
self.materials[key] = data
|
|
414
|
+
return data
|
|
415
|
+
|
|
416
|
+
def _materials_of(self, renderer: Any) -> list[MaterialData]:
|
|
417
|
+
result = []
|
|
418
|
+
for ptr in getattr(renderer, "m_Materials", None) or []:
|
|
419
|
+
result.append(self.get_material(ptr))
|
|
420
|
+
return result or [MaterialData(key=None, name="Default")]
|
|
421
|
+
|
|
422
|
+
def _mesh_filter_of(self, gameobject: Any) -> Any:
|
|
423
|
+
for pair in getattr(gameobject, "m_Component", None) or []:
|
|
424
|
+
component_ptr = getattr(pair, "component", None)
|
|
425
|
+
if not _alive(component_ptr):
|
|
426
|
+
continue
|
|
427
|
+
try:
|
|
428
|
+
if int(getattr(component_ptr, "type", 0)) == 33:
|
|
429
|
+
return _read(component_ptr)
|
|
430
|
+
except Exception:
|
|
431
|
+
continue
|
|
432
|
+
return None
|
|
433
|
+
|
|
434
|
+
def collect(self, active_only: bool = False) -> None:
|
|
435
|
+
renderers: list[RendererModel] = []
|
|
436
|
+
for obj in self.env.objects:
|
|
437
|
+
type_name = obj.type.name
|
|
438
|
+
self._object_counts[type_name] += 1
|
|
439
|
+
if type_name not in RENDERER_TYPES:
|
|
440
|
+
continue
|
|
441
|
+
try:
|
|
442
|
+
renderer = obj.read()
|
|
443
|
+
gameobject = _read(getattr(renderer, "m_GameObject", None))
|
|
444
|
+
if gameobject is None:
|
|
445
|
+
continue
|
|
446
|
+
transform = self._transform_of(gameobject)
|
|
447
|
+
if transform is None:
|
|
448
|
+
continue
|
|
449
|
+
|
|
450
|
+
is_skinned = type_name == "SkinnedMeshRenderer"
|
|
451
|
+
if is_skinned:
|
|
452
|
+
mesh_ptr = getattr(renderer, "m_Mesh", None)
|
|
453
|
+
else:
|
|
454
|
+
mesh_filter = self._mesh_filter_of(gameobject)
|
|
455
|
+
mesh_ptr = getattr(mesh_filter, "m_Mesh", None)
|
|
456
|
+
|
|
457
|
+
mesh = self.get_mesh(mesh_ptr) if _alive(mesh_ptr) else None
|
|
458
|
+
if mesh is None:
|
|
459
|
+
continue
|
|
460
|
+
|
|
461
|
+
active = self.is_active(gameobject)
|
|
462
|
+
enabled = bool(getattr(renderer, "m_Enabled", 1))
|
|
463
|
+
if active_only and not (active and enabled):
|
|
464
|
+
continue
|
|
465
|
+
|
|
466
|
+
bones: list[Any] = []
|
|
467
|
+
bindposes: list[Mat4] = []
|
|
468
|
+
if is_skinned:
|
|
469
|
+
for bone_ptr in getattr(renderer, "m_Bones", None) or []:
|
|
470
|
+
bone = _read(bone_ptr)
|
|
471
|
+
if bone is not None:
|
|
472
|
+
bones.append(bone)
|
|
473
|
+
mesh_obj = mesh_ptr.read()
|
|
474
|
+
for matrix in getattr(mesh_obj, "m_BindPose", None) or []:
|
|
475
|
+
try:
|
|
476
|
+
bindposes.append(matrix_from_unity(matrix))
|
|
477
|
+
except Exception:
|
|
478
|
+
break
|
|
479
|
+
if len(bindposes) != len(bones):
|
|
480
|
+
bindposes = []
|
|
481
|
+
|
|
482
|
+
model = RendererModel(
|
|
483
|
+
obj=obj,
|
|
484
|
+
gameobject=gameobject,
|
|
485
|
+
path=self.node_path(gameobject),
|
|
486
|
+
world=self.world_matrix(transform),
|
|
487
|
+
mesh=mesh,
|
|
488
|
+
materials=self._materials_of(renderer),
|
|
489
|
+
is_skinned=is_skinned,
|
|
490
|
+
bones=bones,
|
|
491
|
+
bindposes=bindposes,
|
|
492
|
+
enabled=enabled,
|
|
493
|
+
active=active,
|
|
494
|
+
source=self._source_of(obj),
|
|
495
|
+
)
|
|
496
|
+
renderers.append(model)
|
|
497
|
+
except Exception as exc:
|
|
498
|
+
self._issue(f"renderer #{getattr(obj, 'path_id', 0)} failed: {type(exc).__name__}: {exc}", obj)
|
|
499
|
+
|
|
500
|
+
renderers.sort(key=lambda r: (r.path.lower(), object_path_id(r.obj)))
|
|
501
|
+
self.renderers = renderers
|
|
502
|
+
log.info(
|
|
503
|
+
"collected %d renderer(s), %d unique mesh(es), %d material(s)",
|
|
504
|
+
len(renderers), len(self.meshes), len(self.materials),
|
|
505
|
+
)
|
|
506
|
+
|
|
507
|
+
def scan_assets(self, active_only: bool = False) -> None:
|
|
508
|
+
if self._assets_scanned:
|
|
509
|
+
return
|
|
510
|
+
self._assets_scanned = True
|
|
511
|
+
seen_renderers = 0
|
|
512
|
+
started = time.monotonic()
|
|
513
|
+
for obj in self.env.objects:
|
|
514
|
+
type_name = obj.type.name
|
|
515
|
+
self._object_counts[type_name] += 1
|
|
516
|
+
if type_name not in RENDERER_TYPES:
|
|
517
|
+
continue
|
|
518
|
+
seen_renderers += 1
|
|
519
|
+
if seen_renderers % 25000 == 0:
|
|
520
|
+
log.info(
|
|
521
|
+
"asset scan: %d renderer(s), %.1f s, %d unique mesh(es)",
|
|
522
|
+
seen_renderers, time.monotonic() - started, len(self._asset_groups),
|
|
523
|
+
)
|
|
524
|
+
try:
|
|
525
|
+
renderer = obj.read()
|
|
526
|
+
gameobject = _read(getattr(renderer, "m_GameObject", None))
|
|
527
|
+
if gameobject is None:
|
|
528
|
+
continue
|
|
529
|
+
transform = self._transform_of(gameobject)
|
|
530
|
+
if transform is None:
|
|
531
|
+
continue
|
|
532
|
+
if active_only and (
|
|
533
|
+
not self.is_active(gameobject)
|
|
534
|
+
or not bool(getattr(renderer, "m_Enabled", 1))
|
|
535
|
+
):
|
|
536
|
+
continue
|
|
537
|
+
|
|
538
|
+
if type_name == "SkinnedMeshRenderer":
|
|
539
|
+
mesh_ptr = getattr(renderer, "m_Mesh", None)
|
|
540
|
+
else:
|
|
541
|
+
mesh_filter = self._mesh_filter_of(gameobject)
|
|
542
|
+
mesh_ptr = getattr(mesh_filter, "m_Mesh", None)
|
|
543
|
+
|
|
544
|
+
mesh = self.get_mesh(mesh_ptr) if _alive(mesh_ptr) else None
|
|
545
|
+
if mesh is None:
|
|
546
|
+
continue
|
|
547
|
+
group = self._mesh_group_key(mesh)
|
|
548
|
+
world = self.world_matrix(transform)
|
|
549
|
+
entry = self._asset_groups.get(group)
|
|
550
|
+
if entry is None:
|
|
551
|
+
entry = _AssetGroup(
|
|
552
|
+
mesh=mesh,
|
|
553
|
+
materials=self._materials_of(renderer),
|
|
554
|
+
worlds=[world],
|
|
555
|
+
)
|
|
556
|
+
self._asset_groups[group] = entry
|
|
557
|
+
else:
|
|
558
|
+
entry.worlds.append(world)
|
|
559
|
+
entry.instances += 1
|
|
560
|
+
except Exception as exc:
|
|
561
|
+
log.warning(
|
|
562
|
+
"asset scan failed for %s: %s: %s",
|
|
563
|
+
type_name, type(exc).__name__, exc,
|
|
564
|
+
)
|
|
565
|
+
log.info(
|
|
566
|
+
"asset scan complete: %d renderer(s), %d unique mesh(es), %.1f s",
|
|
567
|
+
seen_renderers, len(self._asset_groups), time.monotonic() - started,
|
|
568
|
+
)
|
|
569
|
+
|
|
570
|
+
@staticmethod
|
|
571
|
+
def _mesh_group_key(mesh: MeshData) -> tuple[str, int]:
|
|
572
|
+
path_id = mesh.key[1]
|
|
573
|
+
return (mesh.name.lower(), path_id) if path_id else (mesh.name.lower(), id(mesh))
|
|
574
|
+
|
|
575
|
+
@staticmethod
|
|
576
|
+
def _matrix_scale(m: Mat4) -> float | None:
|
|
577
|
+
sx = math.hypot(m[0], m[4], m[8])
|
|
578
|
+
sy = math.hypot(m[1], m[5], m[9])
|
|
579
|
+
sz = math.hypot(m[2], m[6], m[10])
|
|
580
|
+
if not all(math.isfinite(v) and v > 1e-8 for v in (sx, sy, sz)):
|
|
581
|
+
return None
|
|
582
|
+
return (sx * sy * sz) ** (1.0 / 3.0)
|
|
583
|
+
|
|
584
|
+
@staticmethod
|
|
585
|
+
def _without_translation(m: Mat4) -> Mat4:
|
|
586
|
+
return (
|
|
587
|
+
m[0], m[1], m[2], 0.0,
|
|
588
|
+
m[4], m[5], m[6], 0.0,
|
|
589
|
+
m[8], m[9], m[10], 0.0,
|
|
590
|
+
0.0, 0.0, 0.0, 1.0,
|
|
591
|
+
)
|
|
592
|
+
|
|
593
|
+
@classmethod
|
|
594
|
+
def _representative_matrix(cls, worlds: Sequence[Mat4]) -> tuple[Mat4, float]:
|
|
595
|
+
ranked = sorted(
|
|
596
|
+
(scale, index)
|
|
597
|
+
for index, world in enumerate(worlds)
|
|
598
|
+
if (scale := cls._matrix_scale(world)) is not None
|
|
599
|
+
)
|
|
600
|
+
if not ranked:
|
|
601
|
+
return IDENTITY, 1.0
|
|
602
|
+
median_scale, index = ranked[len(ranked) // 2]
|
|
603
|
+
return cls._without_translation(worlds[index]), median_scale
|
|
604
|
+
|
|
605
|
+
@classmethod
|
|
606
|
+
def _asset_transform(cls, instances: Sequence[RendererModel]) -> tuple[Mat4, float]:
|
|
607
|
+
return cls._representative_matrix([model.world for model in instances])
|
|
608
|
+
|
|
609
|
+
def mesh_assets(
|
|
610
|
+
self,
|
|
611
|
+
all_meshes: bool = False,
|
|
612
|
+
active_only: bool = False,
|
|
613
|
+
fast: bool = False,
|
|
614
|
+
) -> list[MeshAsset]:
|
|
615
|
+
used_names: set[str] = set()
|
|
616
|
+
assets: list[MeshAsset] = []
|
|
617
|
+
|
|
618
|
+
def add(
|
|
619
|
+
mesh: MeshData | None,
|
|
620
|
+
materials: list[MaterialData],
|
|
621
|
+
instances: int,
|
|
622
|
+
matrix: Mat4,
|
|
623
|
+
scale: float,
|
|
624
|
+
group: tuple[str, int],
|
|
625
|
+
) -> None:
|
|
626
|
+
if mesh is None or not mesh.ok:
|
|
627
|
+
return
|
|
628
|
+
stem = sanitize_filename(mesh.name or "mesh")
|
|
629
|
+
name = unique_name(stem, used_names, suffix=str(group[1]) if group[1] else "")
|
|
630
|
+
assets.append(
|
|
631
|
+
MeshAsset(
|
|
632
|
+
mesh=mesh,
|
|
633
|
+
name=name,
|
|
634
|
+
materials=materials,
|
|
635
|
+
instances=instances,
|
|
636
|
+
source=mesh.source,
|
|
637
|
+
scale=scale,
|
|
638
|
+
matrix=matrix,
|
|
639
|
+
)
|
|
640
|
+
)
|
|
641
|
+
|
|
642
|
+
if fast:
|
|
643
|
+
self.scan_assets(active_only)
|
|
644
|
+
for group, entry in self._asset_groups.items():
|
|
645
|
+
matrix, scale = self._representative_matrix(entry.worlds)
|
|
646
|
+
add(
|
|
647
|
+
entry.mesh,
|
|
648
|
+
entry.materials,
|
|
649
|
+
entry.instances,
|
|
650
|
+
matrix,
|
|
651
|
+
scale,
|
|
652
|
+
group,
|
|
653
|
+
)
|
|
654
|
+
if all_meshes:
|
|
655
|
+
known = set(self._asset_groups)
|
|
656
|
+
for obj in self.env.objects:
|
|
657
|
+
if obj.type.name != "Mesh":
|
|
658
|
+
continue
|
|
659
|
+
data = self.get_mesh(obj)
|
|
660
|
+
if data is None:
|
|
661
|
+
continue
|
|
662
|
+
group = self._mesh_group_key(data)
|
|
663
|
+
if group in known:
|
|
664
|
+
continue
|
|
665
|
+
known.add(group)
|
|
666
|
+
add(data, [MaterialData(key=None, name="Default")], 0, IDENTITY, 1.0, group)
|
|
667
|
+
else:
|
|
668
|
+
by_group: dict[tuple[str, int], list[RendererModel]] = defaultdict(list)
|
|
669
|
+
mesh_by_group: dict[tuple[str, int], MeshData] = {}
|
|
670
|
+
for model in self.renderers:
|
|
671
|
+
if model.mesh is None:
|
|
672
|
+
continue
|
|
673
|
+
group = self._mesh_group_key(model.mesh)
|
|
674
|
+
by_group[group].append(model)
|
|
675
|
+
mesh_by_group.setdefault(group, model.mesh)
|
|
676
|
+
|
|
677
|
+
if all_meshes:
|
|
678
|
+
for obj in self.env.objects:
|
|
679
|
+
if obj.type.name != "Mesh":
|
|
680
|
+
continue
|
|
681
|
+
data = self.get_mesh(obj)
|
|
682
|
+
if data is None:
|
|
683
|
+
continue
|
|
684
|
+
group = self._mesh_group_key(data)
|
|
685
|
+
by_group.setdefault(group, [])
|
|
686
|
+
mesh_by_group.setdefault(group, data)
|
|
687
|
+
|
|
688
|
+
for group, instances in by_group.items():
|
|
689
|
+
mesh = mesh_by_group.get(group)
|
|
690
|
+
first = instances[0] if instances else None
|
|
691
|
+
materials = first.materials if first else [MaterialData(key=None, name="Default")]
|
|
692
|
+
matrix, scale = self._asset_transform(instances)
|
|
693
|
+
add(
|
|
694
|
+
mesh,
|
|
695
|
+
materials,
|
|
696
|
+
len(instances),
|
|
697
|
+
matrix,
|
|
698
|
+
scale,
|
|
699
|
+
group,
|
|
700
|
+
)
|
|
701
|
+
|
|
702
|
+
assets.sort(key=lambda a: a.name.lower())
|
|
703
|
+
return assets
|
|
704
|
+
|
|
705
|
+
def scene_models(self, active_only: bool = False) -> list[RendererModel]:
|
|
706
|
+
return [
|
|
707
|
+
r for r in self.renderers
|
|
708
|
+
if r.mesh is not None and r.mesh.ok and (not active_only or (r.active and r.enabled))
|
|
709
|
+
]
|
|
710
|
+
|
|
711
|
+
def object_counts(self) -> dict[str, int]:
|
|
712
|
+
return dict(self._object_counts)
|
|
713
|
+
|
|
714
|
+
def unity_versions(self) -> list[str]:
|
|
715
|
+
versions: set[str] = set()
|
|
716
|
+
for assets_file in self.env.assets:
|
|
717
|
+
version = getattr(assets_file, "version", None)
|
|
718
|
+
if version:
|
|
719
|
+
versions.add(".".join(str(v) for v in version[:3]))
|
|
720
|
+
return sorted(versions)
|