-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.py
More file actions
379 lines (303 loc) · 11.6 KB
/
export.py
File metadata and controls
379 lines (303 loc) · 11.6 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
"""
Utilitários para exportação e conversão de arquivos 3D (bpy-based).
Suporta GLB/GLTF e PLY. Suporte a OBJ foi removido.
"""
from __future__ import annotations
import contextlib
import warnings
from pathlib import Path
from typing import Any
import numpy as np
from diffusers.utils import export_to_gif, export_to_ply
from ..defaults import get_export_origin, get_export_rotation_x_rad
def _require_bpy():
"""Lazy-import bpy com mensagem clara se em falta."""
try:
import bpy
return bpy
except ImportError:
raise ImportError("bpy é necessário. Instale com: pip install bpy") from None
def _load_as_bpy(path: str | Path) -> list:
"""Carrega ficheiro 3D via bpy (GLB/GLTF ou PLY), retorna lista de objectos mesh.
Limpa a cena antes do import para evitar poluição de objectos.
"""
from gamedev_shared.bpy_mesh import load_any
return load_any(path)
def _export_glb_bpy(objects: Any, output_path: Path) -> None:
"""Exporta GLB via bpy + weld pass para limpar vértices duplicados."""
import logging as _log_mod
from gamedev_shared.bpy_mesh import save_glb
save_glb(objects, output_path)
try:
from gamedev_shared.mesh_utils import weld_glb as _weld_glb
_weld_glb(str(output_path))
except ImportError:
pass
except Exception as exc: # noqa: BLE001
_log_mod.getLogger(__name__).warning("weld_glb pós-export falhou: %s", exc)
def _apply_rotation_bpy(obj: Any) -> Any:
"""Rotação X configurável no objecto bpy (defeito 0 = sem rotação)."""
angle = float(get_export_rotation_x_rad())
if angle == 0.0:
return obj
_require_bpy()
import mathutils
from math import cos, sin
c, s = cos(angle), sin(angle)
rx = mathutils.Matrix(((1, 0, 0, 0), (0, c, -s, 0), (0, s, c, 0), (0, 0, 0, 1)))
obj.matrix_world = rx @ obj.matrix_world
return obj
def _apply_origin_bpy(obj: Any, mode: str) -> Any:
"""Reposiciona a malha para uma origem consistente **após** rotação Y-up.
- ``feet``: base da AABB em Y=0, centro em X e Z.
- ``center``: centro da AABB em (0, 0, 0).
- ``none``: sem translação.
"""
if mode == "none":
return obj
from gamedev_shared.bpy_mesh import get_bounds
(bx0, by0, bz0), (bx1, by1, bz1) = get_bounds(obj)
if mode == "feet":
tx = -(bx0 + bx1) * 0.5
ty = -by0
tz = -(bz0 + bz1) * 0.5
elif mode == "center":
tx = -(bx0 + bx1) * 0.5
ty = -(by0 + by1) * 0.5
tz = -(bz0 + bz1) * 0.5
else:
raise ValueError(f"Modo de origem desconhecido: {mode!r}")
_require_bpy()
import mathutils
translate = mathutils.Matrix.Translation((tx, ty, tz))
obj.matrix_world = translate @ obj.matrix_world
return obj
def _numpy_to_bpy_object(vertices: np.ndarray, faces: np.ndarray) -> Any:
"""Cria objecto bpy a partir de arrays numpy (vértices + faces).
Usado na fronteira com código vendored (hy3dshape / diffusers) que devolve
arrays em vez de objectos bpy.
"""
bpy = _require_bpy()
mesh_data = bpy.data.meshes.new("imported")
mesh_data.from_pydata(vertices.tolist(), [], faces.tolist())
mesh_data.update()
obj = bpy.data.objects.new("imported", mesh_data)
bpy.context.collection.objects.link(obj)
return obj
def save_mesh(
mesh_input: np.ndarray | Any,
output_path: str | Path,
format: str | None = None,
rotate: bool = True,
origin_mode: str | None = None,
) -> Path:
"""
Salva mesh em formato PLY ou GLB.
Aceita array numpy (legado / diffusers) ou objecto com ``.vertices``/``.faces``
(ex-trimesh, bpy). ``origin_mode``: ``feet`` | ``center`` | ``none``
(defeito: env / ``get_export_origin()``).
"""
if origin_mode is None:
origin_mode = get_export_origin()
output_path = Path(output_path)
if format is None:
format = output_path.suffix.lstrip(".")
format = format.lower()
output_path.parent.mkdir(parents=True, exist_ok=True)
# --- Caminho: objecto com .vertices/.faces (ex-trimesh, bpy) ---
has_mesh_attrs = hasattr(mesh_input, "vertices") and hasattr(mesh_input, "faces")
if has_mesh_attrs:
try:
from gamedev_shared.bpy_mesh import clear_scene
clear_scene()
verts = np.asarray(mesh_input.vertices, dtype=np.float64)
faces = np.asarray(mesh_input.faces, dtype=np.int32)
obj = _numpy_to_bpy_object(verts, faces)
if rotate:
_apply_rotation_bpy(obj)
_apply_origin_bpy(obj, origin_mode)
if format == "glb":
_export_glb_bpy(obj, output_path)
return output_path
if format == "ply":
bpy = _require_bpy()
bpy.ops.export_mesh.ply(filepath=str(output_path), use_selection=True)
return output_path
raise ValueError(f"Formato não suportado: {format}")
except ImportError:
if hasattr(mesh_input, "export"):
mesh_input.export(str(output_path), file_type=format)
return output_path
raise
# --- Caminho: numpy array (legado diffusers / hy3dshape) ---
mesh_array = mesh_input
if format == "ply":
temp_ply = output_path.with_suffix(".ply")
export_to_ply(mesh_array, str(temp_ply))
if rotate:
_apply_rotation(temp_ply, origin_mode=origin_mode)
else:
_apply_origin_only_path(temp_ply, origin_mode=origin_mode)
if output_path.suffix.lower() == ".ply":
return temp_ply
# Converter PLY → GLB (já com rotação + origem)
return convert_mesh(temp_ply, output_path, rotate=False, origin_mode="none")
if format == "glb":
temp_ply = output_path.with_suffix(".temp.ply")
export_to_ply(mesh_array, str(temp_ply))
try:
objs = _load_as_bpy(temp_ply)
if not objs:
raise ValueError(f"Mesh vazia: {temp_ply}")
obj = objs[0]
if rotate:
_apply_rotation_bpy(obj)
_apply_origin_bpy(obj, origin_mode)
_export_glb_bpy(obj, output_path)
finally:
if temp_ply.exists():
temp_ply.unlink()
return output_path
raise ValueError(f"Formato não suportado: {format}")
def save_gif(
frames: list,
output_path: str | Path,
) -> Path:
"""
Salva frames como GIF animado.
Args:
frames: Lista de frames PIL.Image
output_path: Caminho de saída
Returns:
Caminho do arquivo salvo
"""
output_path = Path(output_path)
output_path.parent.mkdir(parents=True, exist_ok=True)
export_to_gif(frames, str(output_path))
return output_path
def convert_mesh(
input_path: str | Path,
output_path: str | Path,
rotate: bool = False,
origin_mode: str | None = None,
) -> Path:
"""
Converte mesh entre formatos usando bpy.
Args:
input_path: Arquivo de entrada
output_path: Arquivo de saída
rotate: Aplicar rotação
origin_mode: ``feet`` | ``center`` | ``none`` (defeito: ``get_export_origin()``)
Returns:
Caminho do arquivo convertido
"""
if origin_mode is None:
origin_mode = get_export_origin()
input_path = Path(input_path)
output_path = Path(output_path)
if not input_path.exists():
raise FileNotFoundError(f"Arquivo não encontrado: {input_path}")
objs = _load_as_bpy(input_path)
if not objs:
raise ValueError(f"Mesh vazia: {input_path}")
for obj in objs:
if rotate:
_apply_rotation_bpy(obj)
_apply_origin_bpy(obj, origin_mode)
output_format = output_path.suffix.lstrip(".").lower()
if output_format == "glb":
_export_glb_bpy(objs, output_path)
elif output_format == "ply":
bpy = _require_bpy()
bpy.ops.object.select_all(action="DESELECT")
for o in objs:
o.select_set(True)
bpy.ops.export_mesh.ply(filepath=str(output_path), use_selection=True)
else:
raise ValueError(f"Formato de saída não suportado: {output_format}")
return output_path
def _apply_rotation(ply_path: Path, *, origin_mode: str | None = None) -> None:
"""Aplica rotação Hunyuan→Y-up e reposiciona a origem (PLY in-place)."""
if origin_mode is None:
origin_mode = get_export_origin()
try:
objs = _load_as_bpy(ply_path)
if not objs:
return
for obj in objs:
_apply_rotation_bpy(obj)
_apply_origin_bpy(obj, origin_mode)
bpy = _require_bpy()
bpy.ops.object.select_all(action="DESELECT")
for o in objs:
o.select_set(True)
bpy.ops.export_mesh.ply(filepath=str(ply_path), use_selection=True)
except Exception as e:
warnings.warn(f"Não foi possível aplicar rotação/origem: {e}", stacklevel=2)
def _apply_origin_only_path(path: Path, *, origin_mode: str | None = None) -> None:
"""Só translada origem (sem rotação), útil quando ``rotate=False`` no legado numpy."""
if origin_mode is None:
origin_mode = get_export_origin()
if origin_mode == "none":
return
try:
objs = _load_as_bpy(path)
if not objs:
return
for obj in objs:
_apply_origin_bpy(obj, origin_mode)
ext = path.suffix.lower().lstrip("") or "ply"
bpy = _require_bpy()
bpy.ops.object.select_all(action="DESELECT")
for o in objs:
o.select_set(True)
if ext == "ply":
bpy.ops.export_mesh.ply(filepath=str(path), use_selection=True)
else:
bpy.ops.export_scene.gltf(filepath=str(path), export_format="GLB", use_selection=True)
except Exception as e:
warnings.warn(f"Não foi possível aplicar origem: {e}", stacklevel=2)
def get_mesh_info(mesh_path: str | Path) -> dict:
"""
Obtém informações sobre um arquivo mesh.
Args:
mesh_path: Caminho do arquivo mesh
Returns:
Dicionário com informações
"""
from gamedev_shared.bpy_mesh import face_count, get_bounds, vertex_count
mesh_path = Path(mesh_path)
if not mesh_path.exists():
raise FileNotFoundError(f"Arquivo não encontrado: {mesh_path}")
objs = _load_as_bpy(mesh_path)
if not objs:
raise ValueError(f"Mesh vazia: {mesh_path}")
# Agregação de múltiplos objectos (cenas multi-mesh)
total_verts = sum(vertex_count(o) for o in objs)
total_faces = sum(face_count(o) for o in objs)
# Bounds globais
all_bounds = [get_bounds(o) for o in objs]
if all_bounds:
(min_x, min_y, min_z) = all_bounds[0][0]
(max_x, max_y, max_z) = all_bounds[0][1]
for (bx0, by0, bz0), (bx1, by1, bz1) in all_bounds[1:]:
min_x, min_y, min_z = min(min_x, bx0), min(min_y, by0), min(min_z, bz0)
max_x, max_y, max_z = max(max_x, bx1), max(max_y, by1), max(max_z, bz1)
bounds_list = [[min_x, min_y, min_z], [max_x, max_y, max_z]]
else:
bounds_list = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
info = {
"path": str(mesh_path),
"format": mesh_path.suffix.lstrip(".").lower(),
"vertices": total_verts,
"faces": total_faces,
"bounds": bounds_list,
"is_watertight": None,
"volume": None,
}
return info
# ── Backward-compatible aliases (remover após migrar módulos irmãos) ──
_load_as_trimesh = _load_as_bpy
_export_glb_with_normals = _export_glb_bpy
_apply_rotation_trimesh = _apply_rotation_bpy
_apply_origin_trimesh = _apply_origin_bpy