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/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "1.0.0"
|
unityripper/__main__.py
ADDED
unityripper/cli.py
ADDED
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import logging
|
|
6
|
+
import os
|
|
7
|
+
import sys
|
|
8
|
+
import traceback
|
|
9
|
+
from collections import Counter
|
|
10
|
+
from datetime import datetime, timezone
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Any, Sequence
|
|
13
|
+
|
|
14
|
+
from . import __version__
|
|
15
|
+
from .discovery import InputSet, collect
|
|
16
|
+
from .exporters.common import ExportOptions
|
|
17
|
+
from .exporters.glb import export_glb_assets, export_glb_assets_split, export_glb_scene
|
|
18
|
+
from .exporters.obj import ObjExporter
|
|
19
|
+
from .materials import MaterialData
|
|
20
|
+
from .scene import MeshAsset, RENDERER_TYPES, RendererModel, UnityScene, load_environment
|
|
21
|
+
from .textures import TextureStore, texture_format_name
|
|
22
|
+
from .util import asset_file_of, format_size, log, object_identity, object_path_id, plural, sanitize_filename
|
|
23
|
+
|
|
24
|
+
def _add_common_load_options(parser: argparse.ArgumentParser) -> None:
|
|
25
|
+
parser.add_argument("inputs", nargs="+", help="game folder, *_Data folder, .assets file, bundle, .apk or .zip")
|
|
26
|
+
parser.add_argument("--active-only", action="store_true", help="skip inactive GameObjects and disabled renderers")
|
|
27
|
+
parser.add_argument("--key", default=None, help="hex AES key for encrypted AssetBundles")
|
|
28
|
+
parser.add_argument("-v", "--verbose", action="count", default=0, help="more logging (-vv for debug)")
|
|
29
|
+
|
|
30
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
31
|
+
parser = argparse.ArgumentParser(
|
|
32
|
+
prog="unityripper",
|
|
33
|
+
description="Extract 3D models, materials and textures from Unity games (Mono and IL2CPP builds).",
|
|
34
|
+
)
|
|
35
|
+
parser.add_argument("--version", action="version", version=f"unityripper {__version__}")
|
|
36
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
37
|
+
|
|
38
|
+
extract = sub.add_parser("extract", help="export models to OBJ / GLB / FBX")
|
|
39
|
+
_add_common_load_options(extract)
|
|
40
|
+
extract.add_argument("-o", "--output", required=True, help="output directory")
|
|
41
|
+
extract.add_argument(
|
|
42
|
+
"--format",
|
|
43
|
+
default="obj,glb",
|
|
44
|
+
help="comma separated list: obj, glb, fbx (default: obj,glb)",
|
|
45
|
+
)
|
|
46
|
+
extract.add_argument(
|
|
47
|
+
"--mode",
|
|
48
|
+
choices=("assets", "scene"),
|
|
49
|
+
default="assets",
|
|
50
|
+
help="assets: one model per unique mesh at the origin; scene: every renderer with transforms and skinning",
|
|
51
|
+
)
|
|
52
|
+
extract.add_argument("--all-meshes", action="store_true", help="also export meshes that no renderer references")
|
|
53
|
+
extract.add_argument(
|
|
54
|
+
"--split",
|
|
55
|
+
action="store_true",
|
|
56
|
+
help="scene mode: one OBJ and one FBX per renderer instead of merged scene files "
|
|
57
|
+
"(assets mode already writes one file per model)",
|
|
58
|
+
)
|
|
59
|
+
extract.add_argument("--no-textures", action="store_true", help="skip texture decoding")
|
|
60
|
+
extract.add_argument(
|
|
61
|
+
"--all-textures",
|
|
62
|
+
action="store_true",
|
|
63
|
+
help="also export every Texture2D in the game, not only textures used by exported materials",
|
|
64
|
+
)
|
|
65
|
+
extract.add_argument(
|
|
66
|
+
"--normal-maps",
|
|
67
|
+
choices=("auto", "raw", "off"),
|
|
68
|
+
default="auto",
|
|
69
|
+
help="auto: rebuild DXT5nm normal maps; raw: save as stored; off: no normal maps",
|
|
70
|
+
)
|
|
71
|
+
extract.add_argument(
|
|
72
|
+
"--metallic-roughness",
|
|
73
|
+
choices=("auto", "off"),
|
|
74
|
+
default="auto",
|
|
75
|
+
help="repack Unity metallic/smoothness maps for glTF",
|
|
76
|
+
)
|
|
77
|
+
extract.add_argument("--keep-axes", action="store_true", help="do not mirror X (keep Unity's left-handed axes)")
|
|
78
|
+
extract.add_argument("--scale", type=float, default=1.0, help="uniform export scale (default: 1.0)")
|
|
79
|
+
extract.add_argument("--no-manifest", action="store_true", help="do not write manifest.json")
|
|
80
|
+
extract.add_argument("--scene-name", default=None, help="name for scene mode output files")
|
|
81
|
+
|
|
82
|
+
listing = sub.add_parser("list", help="list meshes, renderers and textures")
|
|
83
|
+
_add_common_load_options(listing)
|
|
84
|
+
listing.add_argument("--type", default=None, help="filter object types (Mesh, Texture2D, Material, ...)")
|
|
85
|
+
listing.add_argument("--json", action="store_true", help="machine readable output")
|
|
86
|
+
listing.add_argument("--limit", type=int, default=500, help="maximum rows (default: 500)")
|
|
87
|
+
|
|
88
|
+
info = sub.add_parser("info", help="show files, Unity versions and object counts")
|
|
89
|
+
_add_common_load_options(info)
|
|
90
|
+
info.add_argument("--json", action="store_true", help="machine readable output")
|
|
91
|
+
|
|
92
|
+
return parser
|
|
93
|
+
|
|
94
|
+
def _setup_logging(verbose: int) -> None:
|
|
95
|
+
level = logging.WARNING
|
|
96
|
+
if verbose == 1:
|
|
97
|
+
level = logging.INFO
|
|
98
|
+
elif verbose >= 2:
|
|
99
|
+
level = logging.DEBUG
|
|
100
|
+
logging.basicConfig(level=level, format="%(message)s", stream=sys.stderr)
|
|
101
|
+
|
|
102
|
+
def _load(
|
|
103
|
+
inputs: Sequence[str],
|
|
104
|
+
key: str | None,
|
|
105
|
+
active_only: bool,
|
|
106
|
+
collect_scene: bool = True,
|
|
107
|
+
) -> tuple[InputSet, UnityScene]:
|
|
108
|
+
input_set = collect(list(inputs))
|
|
109
|
+
for note in input_set.notes:
|
|
110
|
+
log.warning(note)
|
|
111
|
+
if not input_set.all_sources:
|
|
112
|
+
raise RuntimeError("no Unity assets found in the given paths")
|
|
113
|
+
env = load_environment(input_set, decrypt_key=key)
|
|
114
|
+
scene = UnityScene(env, [str(p) for p in input_set.all_sources])
|
|
115
|
+
if collect_scene:
|
|
116
|
+
scene.collect(active_only=active_only)
|
|
117
|
+
return input_set, scene
|
|
118
|
+
|
|
119
|
+
def _unique_object_counts(scene: Any) -> Counter[str]:
|
|
120
|
+
counts: Counter[str] = Counter()
|
|
121
|
+
seen: set[tuple[str, str, int]] = set()
|
|
122
|
+
for obj in scene.env.objects:
|
|
123
|
+
type_name = obj.type.name
|
|
124
|
+
identity = (type_name, *object_identity(obj, getattr(scene.env, "path", None)))
|
|
125
|
+
if identity in seen:
|
|
126
|
+
continue
|
|
127
|
+
seen.add(identity)
|
|
128
|
+
counts[type_name] += 1
|
|
129
|
+
return counts
|
|
130
|
+
|
|
131
|
+
def _unique_file_count(env: Any) -> int:
|
|
132
|
+
base = getattr(env, "path", None)
|
|
133
|
+
seen: set[str] = set()
|
|
134
|
+
for entry in env.files:
|
|
135
|
+
name = str(getattr(entry, "name", None) or entry)
|
|
136
|
+
path = Path(name)
|
|
137
|
+
if not path.is_absolute() and base:
|
|
138
|
+
path = Path(str(base)) / path
|
|
139
|
+
seen.add(os.path.normcase(os.path.normpath(str(path))))
|
|
140
|
+
return len(seen)
|
|
141
|
+
|
|
142
|
+
def _cmd_info(args: argparse.Namespace) -> int:
|
|
143
|
+
input_set, scene = _load(args.inputs, args.key, args.active_only, collect_scene=False)
|
|
144
|
+
counts = _unique_object_counts(scene)
|
|
145
|
+
renderers = counts.get("MeshRenderer", 0) + counts.get("SkinnedMeshRenderer", 0)
|
|
146
|
+
data = {
|
|
147
|
+
"tool": f"unityripper {__version__}",
|
|
148
|
+
"inputs": [str(p) for p in input_set.all_sources],
|
|
149
|
+
"data_dirs": [str(p) for p in input_set.data_dirs],
|
|
150
|
+
"unity_versions": scene.unity_versions(),
|
|
151
|
+
"files": _unique_file_count(scene.env),
|
|
152
|
+
"objects": sum(counts.values()),
|
|
153
|
+
"renderers": renderers,
|
|
154
|
+
"meshes": counts.get("Mesh", 0),
|
|
155
|
+
"textures": counts.get("Texture2D", 0),
|
|
156
|
+
"materials": counts.get("Material", 0),
|
|
157
|
+
"types": dict(counts.most_common()),
|
|
158
|
+
}
|
|
159
|
+
if args.json:
|
|
160
|
+
print(json.dumps(data, indent=2, ensure_ascii=False))
|
|
161
|
+
return 0
|
|
162
|
+
print(f"unityripper {__version__}")
|
|
163
|
+
print(f"inputs: {len(data['inputs'])}")
|
|
164
|
+
for path in data["inputs"][:20]:
|
|
165
|
+
print(f" {path}")
|
|
166
|
+
if len(data["inputs"]) > 20:
|
|
167
|
+
print(f" ... and {len(data['inputs']) - 20} more")
|
|
168
|
+
print(f"unity version: {', '.join(data['unity_versions']) or 'unknown'}")
|
|
169
|
+
print(f"files: {data['files']}")
|
|
170
|
+
print(f"objects: {data['objects']}")
|
|
171
|
+
print(f"renderers: {data['renderers']}")
|
|
172
|
+
print(f"meshes: {data['meshes']}")
|
|
173
|
+
print(f"materials: {data['materials']}")
|
|
174
|
+
print(f"textures: {data['textures']}")
|
|
175
|
+
print("object types:")
|
|
176
|
+
for name, count in counts.most_common(25):
|
|
177
|
+
print(f" {name:<24} {count}")
|
|
178
|
+
return 0
|
|
179
|
+
|
|
180
|
+
def _cmd_list(args: argparse.Namespace) -> int:
|
|
181
|
+
wanted = {t.strip() for t in args.type.split(",")} if args.type else None
|
|
182
|
+
needs_scene = wanted is None or bool(wanted & RENDERER_TYPES)
|
|
183
|
+
_, scene = _load(
|
|
184
|
+
args.inputs,
|
|
185
|
+
args.key,
|
|
186
|
+
args.active_only,
|
|
187
|
+
collect_scene=needs_scene,
|
|
188
|
+
)
|
|
189
|
+
rows: list[dict[str, Any]] = []
|
|
190
|
+
for obj in scene.env.objects:
|
|
191
|
+
type_name = obj.type.name
|
|
192
|
+
if wanted and type_name not in wanted:
|
|
193
|
+
continue
|
|
194
|
+
if type_name == "Mesh":
|
|
195
|
+
mesh = scene.get_mesh(obj)
|
|
196
|
+
if mesh is None:
|
|
197
|
+
continue
|
|
198
|
+
rows.append(
|
|
199
|
+
{
|
|
200
|
+
"type": "Mesh",
|
|
201
|
+
"name": mesh.name,
|
|
202
|
+
"vertices": mesh.vertex_count,
|
|
203
|
+
"triangles": mesh.triangle_count,
|
|
204
|
+
"submeshes": mesh.submesh_count,
|
|
205
|
+
"source": mesh.source,
|
|
206
|
+
}
|
|
207
|
+
)
|
|
208
|
+
elif type_name in {"Texture2D", "Sprite"}:
|
|
209
|
+
try:
|
|
210
|
+
texture = obj.read()
|
|
211
|
+
fmt_name = texture_format_name(getattr(texture, "m_TextureFormat", None))
|
|
212
|
+
rows.append(
|
|
213
|
+
{
|
|
214
|
+
"type": type_name,
|
|
215
|
+
"name": str(getattr(texture, "m_Name", "") or ""),
|
|
216
|
+
"width": int(getattr(texture, "m_Width", 0) or 0),
|
|
217
|
+
"height": int(getattr(texture, "m_Height", 0) or 0),
|
|
218
|
+
"format": fmt_name,
|
|
219
|
+
"source": Path(str(getattr(asset_file_of(obj), "name", ""))).name,
|
|
220
|
+
}
|
|
221
|
+
)
|
|
222
|
+
except Exception:
|
|
223
|
+
continue
|
|
224
|
+
elif type_name in {"MeshRenderer", "SkinnedMeshRenderer"}:
|
|
225
|
+
model = next(
|
|
226
|
+
(
|
|
227
|
+
m
|
|
228
|
+
for m in scene.renderers
|
|
229
|
+
if m.obj.path_id == obj.path_id and asset_file_of(m.obj) is asset_file_of(obj)
|
|
230
|
+
),
|
|
231
|
+
None,
|
|
232
|
+
)
|
|
233
|
+
if model is None:
|
|
234
|
+
continue
|
|
235
|
+
rows.append(
|
|
236
|
+
{
|
|
237
|
+
"type": type_name,
|
|
238
|
+
"name": model.path,
|
|
239
|
+
"mesh": model.mesh.name if model.mesh else "",
|
|
240
|
+
"vertices": model.mesh.vertex_count if model.mesh else 0,
|
|
241
|
+
"triangles": model.mesh.triangle_count if model.mesh else 0,
|
|
242
|
+
"materials": [m.name for m in model.materials],
|
|
243
|
+
"skinned": model.is_skinned,
|
|
244
|
+
"active": model.active,
|
|
245
|
+
"source": model.source,
|
|
246
|
+
}
|
|
247
|
+
)
|
|
248
|
+
elif wanted:
|
|
249
|
+
name = ""
|
|
250
|
+
try:
|
|
251
|
+
name = str(getattr(obj.read(), "m_Name", "") or "")
|
|
252
|
+
except Exception:
|
|
253
|
+
name = ""
|
|
254
|
+
rows.append(
|
|
255
|
+
{
|
|
256
|
+
"type": type_name,
|
|
257
|
+
"name": name,
|
|
258
|
+
"source": Path(str(getattr(asset_file_of(obj), "name", ""))).name,
|
|
259
|
+
}
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
rows = rows[: args.limit]
|
|
263
|
+
if args.json:
|
|
264
|
+
print(json.dumps(rows, indent=2, ensure_ascii=False))
|
|
265
|
+
return 0
|
|
266
|
+
|
|
267
|
+
if not rows:
|
|
268
|
+
print("nothing found")
|
|
269
|
+
return 1
|
|
270
|
+
if wanted and wanted == {"Mesh"}:
|
|
271
|
+
print(f"{'name':<44} {'verts':>8} {'tris':>8} {'sub':>4} source")
|
|
272
|
+
for row in rows:
|
|
273
|
+
print(
|
|
274
|
+
f"{row['name'][:44]:<44} {row['vertices']:>8} {row['triangles']:>8} "
|
|
275
|
+
f"{row['submeshes']:>4} {row['source']}"
|
|
276
|
+
)
|
|
277
|
+
elif wanted and wanted <= {"Texture2D", "Sprite"}:
|
|
278
|
+
print(f"{'name':<44} {'size':>12} {'format':>16} source")
|
|
279
|
+
for row in rows:
|
|
280
|
+
size = f"{row['width']}x{row['height']}"
|
|
281
|
+
print(f"{row['name'][:44]:<44} {size:>12} {row['format']:>16} {row['source']}")
|
|
282
|
+
else:
|
|
283
|
+
for row in rows:
|
|
284
|
+
if row["type"] == "Mesh":
|
|
285
|
+
print(f"Mesh {row['name']} {row['vertices']}v {row['triangles']}t [{row['source']}]")
|
|
286
|
+
elif row["type"] in {"MeshRenderer", "SkinnedMeshRenderer"}:
|
|
287
|
+
kind = "Skinned" if row["skinned"] else "Mesh"
|
|
288
|
+
print(
|
|
289
|
+
f"{kind} {row['name']} mesh={row['mesh']} {row['vertices']}v {row['triangles']}t "
|
|
290
|
+
f"materials={', '.join(row['materials'])}"
|
|
291
|
+
)
|
|
292
|
+
else:
|
|
293
|
+
print(f"{row['type']} {row['name']} {row.get('width', '')}x{row.get('height', '')} {row.get('format', '')}")
|
|
294
|
+
return 0
|
|
295
|
+
|
|
296
|
+
def _asset_manifest(asset: MeshAsset, files: dict[str, str]) -> dict[str, Any]:
|
|
297
|
+
return {
|
|
298
|
+
"name": asset.name,
|
|
299
|
+
"mesh": asset.mesh.name,
|
|
300
|
+
"vertices": asset.mesh.vertex_count,
|
|
301
|
+
"triangles": asset.mesh.triangle_count,
|
|
302
|
+
"submeshes": asset.mesh.submesh_count,
|
|
303
|
+
"instances": asset.instances,
|
|
304
|
+
"scale": round(asset.scale, 6),
|
|
305
|
+
"materials": [m.name for m in asset.materials],
|
|
306
|
+
"source": asset.source,
|
|
307
|
+
"files": files,
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
def _scene_manifest(model: RendererModel, files: dict[str, str]) -> dict[str, Any]:
|
|
311
|
+
return {
|
|
312
|
+
"name": model.name,
|
|
313
|
+
"path": model.path,
|
|
314
|
+
"mesh": model.mesh.name if model.mesh else "",
|
|
315
|
+
"vertices": model.mesh.vertex_count if model.mesh else 0,
|
|
316
|
+
"triangles": model.mesh.triangle_count if model.mesh else 0,
|
|
317
|
+
"skinned": model.is_skinned,
|
|
318
|
+
"bones": len(model.bones),
|
|
319
|
+
"materials": [m.name for m in model.materials],
|
|
320
|
+
"active": model.active,
|
|
321
|
+
"enabled": model.enabled,
|
|
322
|
+
"source": model.source,
|
|
323
|
+
"files": files,
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
def _export_all_textures(scene: UnityScene, textures: TextureStore) -> int:
|
|
327
|
+
exported = 0
|
|
328
|
+
seen: set[tuple[str, int]] = set()
|
|
329
|
+
for _ in range(4):
|
|
330
|
+
pending = []
|
|
331
|
+
for obj in scene.env.objects:
|
|
332
|
+
if obj.type.name != "Texture2D":
|
|
333
|
+
continue
|
|
334
|
+
identity = object_identity(obj, getattr(scene.env, "path", None))
|
|
335
|
+
if identity in seen:
|
|
336
|
+
continue
|
|
337
|
+
seen.add(identity)
|
|
338
|
+
pending.append(obj)
|
|
339
|
+
if not pending:
|
|
340
|
+
break
|
|
341
|
+
for obj in pending:
|
|
342
|
+
path_id = object_path_id(obj)
|
|
343
|
+
try:
|
|
344
|
+
name = str(getattr(obj.read(), "m_Name", "") or f"texture_{path_id}")
|
|
345
|
+
except Exception:
|
|
346
|
+
name = f"texture_{path_id}"
|
|
347
|
+
if textures.save(obj, hint=name, role="albedo") is not None:
|
|
348
|
+
exported += 1
|
|
349
|
+
log.info("exported %d texture object(s), %d PNG file(s)", exported, len(textures.assets))
|
|
350
|
+
return exported
|
|
351
|
+
|
|
352
|
+
def _parse_formats(value: str) -> list[str]:
|
|
353
|
+
formats = [part.strip().lower() for part in value.split(",") if part.strip()]
|
|
354
|
+
valid = {"obj", "glb", "fbx"}
|
|
355
|
+
unknown = [f for f in formats if f not in valid]
|
|
356
|
+
if unknown:
|
|
357
|
+
raise ValueError(f"unknown format(s): {', '.join(unknown)} (valid: obj, glb, fbx)")
|
|
358
|
+
if not formats:
|
|
359
|
+
raise ValueError("no export format selected")
|
|
360
|
+
return formats
|
|
361
|
+
|
|
362
|
+
def _rel(path: Path, output: Path) -> str:
|
|
363
|
+
return path.relative_to(output).as_posix()
|
|
364
|
+
|
|
365
|
+
def _cmd_extract(args: argparse.Namespace) -> int:
|
|
366
|
+
formats = _parse_formats(args.format)
|
|
367
|
+
input_set, scene = _load(
|
|
368
|
+
args.inputs,
|
|
369
|
+
args.key,
|
|
370
|
+
args.active_only,
|
|
371
|
+
collect_scene=args.mode != "assets",
|
|
372
|
+
)
|
|
373
|
+
output = Path(args.output)
|
|
374
|
+
output.mkdir(parents=True, exist_ok=True)
|
|
375
|
+
|
|
376
|
+
options = ExportOptions(
|
|
377
|
+
mirror_x=not args.keep_axes,
|
|
378
|
+
scale=float(args.scale),
|
|
379
|
+
textures=not args.no_textures,
|
|
380
|
+
normal_mode=args.normal_maps,
|
|
381
|
+
mr_mode=args.metallic_roughness,
|
|
382
|
+
all_meshes=args.all_meshes,
|
|
383
|
+
active_only=args.active_only,
|
|
384
|
+
)
|
|
385
|
+
textures = TextureStore(
|
|
386
|
+
output,
|
|
387
|
+
normal_mode=args.normal_maps,
|
|
388
|
+
enabled=not args.no_textures,
|
|
389
|
+
base_path=getattr(scene.env, "path", None),
|
|
390
|
+
)
|
|
391
|
+
|
|
392
|
+
scene_name = args.scene_name
|
|
393
|
+
if not scene_name:
|
|
394
|
+
if input_set.game_roots:
|
|
395
|
+
scene_name = sanitize_filename(input_set.game_roots[0].name, "scene")
|
|
396
|
+
elif input_set.data_dirs:
|
|
397
|
+
scene_name = sanitize_filename(input_set.data_dirs[0].parent.name or "scene", "scene")
|
|
398
|
+
else:
|
|
399
|
+
scene_name = "scene"
|
|
400
|
+
scene_stem = sanitize_filename(scene_name, "scene")
|
|
401
|
+
|
|
402
|
+
exported_models: list[dict[str, Any]] = []
|
|
403
|
+
written_files: list[str] = []
|
|
404
|
+
renderer_count = 0
|
|
405
|
+
|
|
406
|
+
if args.mode == "assets":
|
|
407
|
+
assets = scene.mesh_assets(
|
|
408
|
+
all_meshes=args.all_meshes,
|
|
409
|
+
active_only=args.active_only,
|
|
410
|
+
fast=True,
|
|
411
|
+
)
|
|
412
|
+
renderer_count = sum(asset.instances for asset in assets)
|
|
413
|
+
if not assets:
|
|
414
|
+
if not (args.all_textures and textures.enabled):
|
|
415
|
+
log.error("no exportable meshes found")
|
|
416
|
+
return 1
|
|
417
|
+
log.warning("no exportable meshes found, exporting textures only")
|
|
418
|
+
log.info("exporting %d unique mesh(es)", len(assets))
|
|
419
|
+
exported_models = [_asset_manifest(asset, {}) for asset in assets]
|
|
420
|
+
by_name = {model["name"]: model for model in exported_models}
|
|
421
|
+
|
|
422
|
+
if assets and "obj" in formats:
|
|
423
|
+
obj = ObjExporter(output, textures, options)
|
|
424
|
+
for name, path in obj.export_assets(assets):
|
|
425
|
+
rel = _rel(path, output)
|
|
426
|
+
written_files.append(rel)
|
|
427
|
+
model = by_name.get(name)
|
|
428
|
+
if model is not None:
|
|
429
|
+
model["files"]["obj"] = rel
|
|
430
|
+
model["files"]["mtl"] = _rel(path.with_suffix(".mtl"), output)
|
|
431
|
+
if assets and "glb" in formats:
|
|
432
|
+
for name, path in export_glb_assets_split(output / "glb", assets, textures, options):
|
|
433
|
+
rel = _rel(path, output)
|
|
434
|
+
written_files.append(rel)
|
|
435
|
+
model = by_name.get(name)
|
|
436
|
+
if model is not None:
|
|
437
|
+
model["files"]["glb"] = rel
|
|
438
|
+
if assets and "fbx" in formats:
|
|
439
|
+
from .exporters.fbx import export_fbx_assets
|
|
440
|
+
|
|
441
|
+
for name, path in export_fbx_assets(output / "fbx", assets, textures, options):
|
|
442
|
+
rel = _rel(path, output)
|
|
443
|
+
written_files.append(rel)
|
|
444
|
+
model = by_name.get(name)
|
|
445
|
+
if model is not None:
|
|
446
|
+
model["files"]["fbx"] = rel
|
|
447
|
+
else:
|
|
448
|
+
models = scene.scene_models(active_only=args.active_only)
|
|
449
|
+
renderer_count = len(scene.renderers)
|
|
450
|
+
if not models:
|
|
451
|
+
if not (args.all_textures and textures.enabled):
|
|
452
|
+
log.error("no exportable renderers found")
|
|
453
|
+
return 1
|
|
454
|
+
log.warning("no exportable renderers found, exporting textures only")
|
|
455
|
+
log.info("exporting scene with %d renderer(s)", len(models))
|
|
456
|
+
files: dict[str, str] = {}
|
|
457
|
+
obj_files: dict[int, str] = {}
|
|
458
|
+
fbx_files: dict[int, str] = {}
|
|
459
|
+
|
|
460
|
+
if models and "obj" in formats:
|
|
461
|
+
obj = ObjExporter(output, textures, options)
|
|
462
|
+
if args.split:
|
|
463
|
+
for model, path in obj.export_scene_split(models):
|
|
464
|
+
rel = _rel(path, output)
|
|
465
|
+
obj_files[id(model)] = rel
|
|
466
|
+
written_files.append(rel)
|
|
467
|
+
written_files.append(_rel(path.with_suffix(".mtl"), output))
|
|
468
|
+
else:
|
|
469
|
+
obj_path = obj.export_scene(models, name=scene_stem)
|
|
470
|
+
rel = _rel(obj_path, output)
|
|
471
|
+
written_files.append(rel)
|
|
472
|
+
written_files.append(_rel(obj_path.with_suffix(".mtl"), output))
|
|
473
|
+
for model in models:
|
|
474
|
+
obj_files[id(model)] = rel
|
|
475
|
+
if models and "glb" in formats:
|
|
476
|
+
glb_path = export_glb_scene(
|
|
477
|
+
output / f"{scene_stem}.glb", models, textures, options, scene_name=scene_name
|
|
478
|
+
)
|
|
479
|
+
if glb_path is not None:
|
|
480
|
+
files["glb"] = _rel(glb_path, output)
|
|
481
|
+
written_files.append(files["glb"])
|
|
482
|
+
if models and "fbx" in formats:
|
|
483
|
+
from .exporters.fbx import export_fbx_scene, export_fbx_scene_split
|
|
484
|
+
|
|
485
|
+
if args.split:
|
|
486
|
+
for model, path in export_fbx_scene_split(output / "fbx", models, textures, options):
|
|
487
|
+
rel = _rel(path, output)
|
|
488
|
+
fbx_files[id(model)] = rel
|
|
489
|
+
written_files.append(rel)
|
|
490
|
+
else:
|
|
491
|
+
fbx_path = export_fbx_scene(
|
|
492
|
+
output / f"{scene_stem}.fbx", models, textures, options, scene_name=scene_name
|
|
493
|
+
)
|
|
494
|
+
if fbx_path is not None:
|
|
495
|
+
rel = _rel(fbx_path, output)
|
|
496
|
+
written_files.append(rel)
|
|
497
|
+
for model in models:
|
|
498
|
+
fbx_files[id(model)] = rel
|
|
499
|
+
|
|
500
|
+
exported_models = []
|
|
501
|
+
for model in models:
|
|
502
|
+
model_files = dict(files)
|
|
503
|
+
if id(model) in obj_files:
|
|
504
|
+
model_files["obj"] = obj_files[id(model)]
|
|
505
|
+
model_files["mtl"] = obj_files[id(model)].rsplit(".", 1)[0] + ".mtl"
|
|
506
|
+
if id(model) in fbx_files:
|
|
507
|
+
model_files["fbx"] = fbx_files[id(model)]
|
|
508
|
+
exported_models.append(_scene_manifest(model, model_files))
|
|
509
|
+
|
|
510
|
+
if args.all_textures and textures.enabled:
|
|
511
|
+
_export_all_textures(scene, textures)
|
|
512
|
+
|
|
513
|
+
if not args.no_manifest:
|
|
514
|
+
type_counts = _unique_object_counts(scene)
|
|
515
|
+
manifest = {
|
|
516
|
+
"tool": f"unityripper {__version__}",
|
|
517
|
+
"generated": datetime.now(timezone.utc).isoformat(),
|
|
518
|
+
"mode": args.mode,
|
|
519
|
+
"unity_versions": scene.unity_versions(),
|
|
520
|
+
"inputs": [str(p) for p in input_set.all_sources],
|
|
521
|
+
"options": {
|
|
522
|
+
"formats": formats,
|
|
523
|
+
"mirror_x": options.mirror_x,
|
|
524
|
+
"scale": options.scale,
|
|
525
|
+
"textures": options.textures,
|
|
526
|
+
"all_textures": bool(args.all_textures),
|
|
527
|
+
"normal_maps": options.normal_mode,
|
|
528
|
+
},
|
|
529
|
+
"counts": {
|
|
530
|
+
"renderers": renderer_count,
|
|
531
|
+
"meshes": type_counts.get("Mesh", 0),
|
|
532
|
+
"materials": type_counts.get("Material", 0),
|
|
533
|
+
"textures": type_counts.get("Texture2D", 0),
|
|
534
|
+
"exported_models": len(exported_models),
|
|
535
|
+
"exported_textures": len(textures.assets),
|
|
536
|
+
},
|
|
537
|
+
"models": exported_models,
|
|
538
|
+
"textures": [
|
|
539
|
+
{
|
|
540
|
+
"name": t.name,
|
|
541
|
+
"file": str(t.file.relative_to(output)),
|
|
542
|
+
"width": t.width,
|
|
543
|
+
"height": t.height,
|
|
544
|
+
"format": t.format_name,
|
|
545
|
+
}
|
|
546
|
+
for t in textures.assets
|
|
547
|
+
],
|
|
548
|
+
"materials": [
|
|
549
|
+
{"name": m.name, "shader": m.shader, "alpha": m.alpha_mode}
|
|
550
|
+
for m in scene.materials.values()
|
|
551
|
+
],
|
|
552
|
+
"issues": [*input_set.notes, *scene.issues, *textures.errors],
|
|
553
|
+
}
|
|
554
|
+
manifest_path = output / "manifest.json"
|
|
555
|
+
manifest_path.write_text(json.dumps(manifest, indent=2, ensure_ascii=False), encoding="utf-8")
|
|
556
|
+
written_files.append("manifest.json")
|
|
557
|
+
|
|
558
|
+
texture_count = len(textures.assets)
|
|
559
|
+
print(
|
|
560
|
+
f"done: {len(exported_models)} {plural(len(exported_models), 'model', 'models', 'models')}, "
|
|
561
|
+
f"{texture_count} {plural(texture_count, 'texture', 'textures', 'textures')}, "
|
|
562
|
+
f"{len(written_files) + texture_count} file(s) -> {output}"
|
|
563
|
+
)
|
|
564
|
+
if scene.issues or textures.errors:
|
|
565
|
+
print(f"warnings: {len(scene.issues) + len(textures.errors)} (details in manifest.json)")
|
|
566
|
+
return 0
|
|
567
|
+
|
|
568
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
569
|
+
parser = build_parser()
|
|
570
|
+
args = parser.parse_args(argv)
|
|
571
|
+
_setup_logging(getattr(args, "verbose", 0))
|
|
572
|
+
try:
|
|
573
|
+
if args.command == "extract":
|
|
574
|
+
return _cmd_extract(args)
|
|
575
|
+
if args.command == "list":
|
|
576
|
+
return _cmd_list(args)
|
|
577
|
+
if args.command == "info":
|
|
578
|
+
return _cmd_info(args)
|
|
579
|
+
parser.print_help()
|
|
580
|
+
return 2
|
|
581
|
+
except KeyboardInterrupt:
|
|
582
|
+
print("interrupted", file=sys.stderr)
|
|
583
|
+
return 130
|
|
584
|
+
except Exception as exc:
|
|
585
|
+
log.error("%s: %s", type(exc).__name__, exc)
|
|
586
|
+
if getattr(args, "verbose", 0) >= 2:
|
|
587
|
+
traceback.print_exc()
|
|
588
|
+
return 2
|
|
589
|
+
|
|
590
|
+
if __name__ == "__main__":
|
|
591
|
+
raise SystemExit(main())
|