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.
@@ -0,0 +1,165 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import re
5
+ from dataclasses import dataclass, field
6
+ from pathlib import Path
7
+ from typing import Iterable
8
+
9
+ from .util import log, walk_files
10
+
11
+ BUNDLE_MAGICS = (b"UnityFS", b"UnityWeb", b"UnityRaw")
12
+ BUNDLE_EXTS = {".bundle", ".unity3d", ".ab", ".assetbundle", ".assetsbundle", ".unitypackage"}
13
+ ARCHIVE_EXTS = {".apk", ".zip", ".obb", ".xapk"}
14
+ ASSET_EXTS = {".assets", ".asset"}
15
+ SKIP_EXTS = {
16
+ ".dll", ".exe", ".pdb", ".json", ".xml", ".txt", ".md", ".log", ".ini", ".cfg",
17
+ ".png", ".jpg", ".jpeg", ".dds", ".tga", ".wav", ".mp3", ".ogg", ".mp4", ".bk2",
18
+ ".resS", ".resource", ".config", ".hash", ".info", ".manifest", ".csv", ".bytes",
19
+ ".shader", ".cginc", ".hlsl", ".compute", ".meta", ".pdb", ".mdb", ".so", ".dylib",
20
+ }
21
+ CORE_NAMES = {"globalgamemanagers", "globalgamemanagers.assets", "resources.assets", "data.unity3d"}
22
+ _SPLIT_RE = re.compile(r"^(.*)\.split\d+$")
23
+
24
+ @dataclass
25
+ class InputSet:
26
+ files: list[Path] = field(default_factory=list)
27
+ game_roots: list[Path] = field(default_factory=list)
28
+ data_dirs: list[Path] = field(default_factory=list)
29
+ archives: list[Path] = field(default_factory=list)
30
+ notes: list[str] = field(default_factory=list)
31
+
32
+ @property
33
+ def all_sources(self) -> list[Path]:
34
+ return [*self.files, *self.archives]
35
+
36
+ def sniff_magic(path: Path) -> str | None:
37
+ try:
38
+ with path.open("rb") as fh:
39
+ head = fh.read(8)
40
+ except OSError:
41
+ return None
42
+ if any(head.startswith(m) for m in BUNDLE_MAGICS):
43
+ return "bundle"
44
+ return None
45
+
46
+ def _is_data_dir(path: Path) -> bool:
47
+ return (path / "globalgamemanagers").is_file() or (path / "globalgamemanagers.assets").is_file()
48
+
49
+ def find_data_dirs(root: Path) -> list[Path]:
50
+ found: list[Path] = []
51
+ if _is_data_dir(root):
52
+ found.append(root)
53
+ for entry in sorted(root.iterdir()):
54
+ if entry.is_dir() and _is_data_dir(entry):
55
+ found.append(entry)
56
+ if not found:
57
+ for entry in sorted(root.iterdir()):
58
+ if not entry.is_dir():
59
+ continue
60
+ if _is_data_dir(entry):
61
+ found.append(entry)
62
+ continue
63
+ for sub in sorted(entry.iterdir()):
64
+ if sub.is_dir() and _is_data_dir(sub):
65
+ found.append(sub)
66
+ return found
67
+
68
+ def _core_assets(data_dir: Path) -> Iterable[Path]:
69
+ for entry in data_dir.iterdir():
70
+ if not entry.is_file():
71
+ continue
72
+ name = entry.name
73
+ lower = name.lower()
74
+ if name in CORE_NAMES:
75
+ yield entry
76
+ elif lower.startswith("sharedassets") and lower.endswith(".assets"):
77
+ yield entry
78
+ elif lower.startswith("level") and not lower.endswith((".resS", ".resource")):
79
+ yield entry
80
+ elif lower.endswith(".assets"):
81
+ yield entry
82
+ elif lower == "data.unity3d":
83
+ yield entry
84
+
85
+ def _bundles_under(root: Path) -> Iterable[Path]:
86
+ if not root.is_dir():
87
+ return
88
+ for path in walk_files(root):
89
+ lower = path.name.lower()
90
+ if lower.endswith((".manifest", ".json", ".txt", ".hash", ".info")):
91
+ continue
92
+ if path.suffix.lower() in BUNDLE_EXTS or sniff_magic(path) == "bundle":
93
+ yield path
94
+
95
+ def collect(inputs: list[str], recursive: bool = True) -> InputSet:
96
+ result = InputSet()
97
+ seen: set[Path] = set()
98
+
99
+ def add(path: Path) -> None:
100
+ try:
101
+ resolved = path.resolve()
102
+ except OSError:
103
+ resolved = path
104
+ if resolved in seen or not path.exists():
105
+ return
106
+ seen.add(resolved)
107
+ result.files.append(path)
108
+
109
+ for raw in inputs:
110
+ path = Path(raw)
111
+ if not path.exists():
112
+ result.notes.append(f"input not found: {raw}")
113
+ continue
114
+
115
+ if path.is_file():
116
+ ext = path.suffix.lower()
117
+ if ext in ARCHIVE_EXTS:
118
+ result.archives.append(path)
119
+ else:
120
+ add(path)
121
+ continue
122
+
123
+ data_dirs = find_data_dirs(path)
124
+ if data_dirs:
125
+ result.game_roots.append(path)
126
+ for data_dir in data_dirs:
127
+ result.data_dirs.append(data_dir)
128
+ for asset in _core_assets(data_dir):
129
+ add(asset)
130
+ for bundle_dir in (data_dir / "StreamingAssets", data_dir / "AssetBundles", data_dir / "Bundles"):
131
+ for bundle in _bundles_under(bundle_dir):
132
+ add(bundle)
133
+ continue
134
+
135
+ if recursive:
136
+ for candidate in walk_files(path):
137
+ ext = candidate.suffix.lower()
138
+ if ext in ARCHIVE_EXTS:
139
+ result.archives.append(candidate)
140
+ elif ext in ASSET_EXTS or ext in BUNDLE_EXTS:
141
+ add(candidate)
142
+ elif sniff_magic(candidate) == "bundle":
143
+ add(candidate)
144
+ else:
145
+ for candidate in path.iterdir():
146
+ if candidate.is_file() and (
147
+ candidate.suffix.lower() in ASSET_EXTS | BUNDLE_EXTS or sniff_magic(candidate) == "bundle"
148
+ ):
149
+ add(candidate)
150
+
151
+ normalized: list[Path] = []
152
+ for path in result.files:
153
+ match = _SPLIT_RE.match(str(path))
154
+ if match:
155
+ base = Path(match.group(1))
156
+ path = base
157
+ if path not in normalized:
158
+ normalized.append(path)
159
+ result.files = normalized
160
+
161
+ if not result.files and not result.archives:
162
+ result.notes.append("no Unity .assets files, bundles or archives were found")
163
+ else:
164
+ log.debug("discovered %d asset file(s), %d archive(s)", len(result.files), len(result.archives))
165
+ return result
@@ -0,0 +1,5 @@
1
+ from .common import ExportOptions
2
+ from .glb import export_glb_assets, export_glb_scene
3
+ from .obj import ObjExporter
4
+
5
+ __all__ = ["ExportOptions", "ObjExporter", "export_glb_assets", "export_glb_scene"]
@@ -0,0 +1,95 @@
1
+ from __future__ import annotations
2
+
3
+ import math
4
+ import os
5
+ from dataclasses import dataclass
6
+ from pathlib import Path
7
+ from typing import Iterable, Sequence
8
+
9
+ from ..materials import MaterialData
10
+ from ..mathutil import Vec3, det3
11
+ from ..scene import MeshData
12
+ from ..util import object_path_id, sanitize_filename, sanitize_node_path
13
+
14
+ def renderer_stem(model: object, max_length: int = 110) -> str:
15
+ path = getattr(model, "path", None) or getattr(model, "name", "") or "object"
16
+ stem = sanitize_node_path(str(path))
17
+ path_id = object_path_id(getattr(model, "obj", None))
18
+ if path_id:
19
+ stem = f"{stem}_{path_id}"
20
+ return sanitize_filename(stem, "object", max_length)
21
+
22
+ @dataclass
23
+ class ExportOptions:
24
+ mirror_x: bool = True
25
+ scale: float = 1.0
26
+ textures: bool = True
27
+ normal_mode: str = "auto"
28
+ mr_mode: str = "auto"
29
+ all_meshes: bool = False
30
+ active_only: bool = False
31
+
32
+ def needs_winding_reverse(world_det: float, mirror_x: bool) -> bool:
33
+ if mirror_x:
34
+ return world_det > 0.0
35
+ return world_det < 0.0
36
+
37
+ def usable_normals(mesh: MeshData) -> list[Vec3] | None:
38
+ normals = mesh.normals
39
+ if not normals or len(normals) != len(mesh.vertices):
40
+ return None
41
+ zero = sum(1 for n in normals if abs(n[0]) + abs(n[1]) + abs(n[2]) < 1e-8)
42
+ if zero > max(1, len(normals) // 10):
43
+ return None
44
+ return normals
45
+
46
+ def compute_normals(vertices: Sequence[Vec3], triangles: Iterable[Sequence[int]]) -> list[Vec3]:
47
+ acc: list[list[float]] = [[0.0, 0.0, 0.0] for _ in vertices]
48
+ for tri in triangles:
49
+ if len(tri) < 3:
50
+ continue
51
+ ia, ib, ic = int(tri[0]), int(tri[1]), int(tri[2])
52
+ if max(ia, ib, ic) >= len(vertices):
53
+ continue
54
+ ax, ay, az = vertices[ia]
55
+ bx, by, bz = vertices[ib]
56
+ cx, cy, cz = vertices[ic]
57
+ ux, uy, uz = bx - ax, by - ay, bz - az
58
+ vx, vy, vz = cx - ax, cy - ay, cz - az
59
+ nx, ny, nz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
60
+ for idx in (ia, ib, ic):
61
+ acc[idx][0] += nx
62
+ acc[idx][1] += ny
63
+ acc[idx][2] += nz
64
+ out: list[Vec3] = []
65
+ for nx, ny, nz in acc:
66
+ length = math.sqrt(nx * nx + ny * ny + nz * nz)
67
+ if length < 1e-12:
68
+ out.append((0.0, 0.0, 1.0))
69
+ else:
70
+ out.append((nx / length, ny / length, nz / length))
71
+ return out
72
+
73
+ def flatten_triangles(mesh: MeshData) -> list[tuple[int, int, int]]:
74
+ return [tri for sub in mesh.triangles for tri in sub]
75
+
76
+ def linear_to_srgb(value: float) -> float:
77
+ value = max(0.0, min(1.0, float(value)))
78
+ if value <= 0.0031308:
79
+ return value * 12.92
80
+ return 1.055 * (value ** (1.0 / 2.4)) - 0.055
81
+
82
+ def srgb_tuple(color: Sequence[float]) -> tuple[float, float, float]:
83
+ return (linear_to_srgb(color[0]), linear_to_srgb(color[1]), linear_to_srgb(color[2]))
84
+
85
+ def texture_relpath(texture_file: Path, start: Path) -> str:
86
+ return Path(os.path.relpath(texture_file, start)).as_posix()
87
+
88
+ def material_stem(index: int, material: MaterialData, prefix: str = "mat") -> str:
89
+ from ..util import sanitize_filename
90
+
91
+ name = sanitize_filename(material.name or f"material_{index}", f"material_{index}", 60)
92
+ return f"{prefix}{index}_{name}"
93
+
94
+ def material_key(material: MaterialData) -> tuple:
95
+ return material.key or (material.name, material.shader)