-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeshToObj.py
More file actions
494 lines (404 loc) · 19 KB
/
Copy pathMeshToObj.py
File metadata and controls
494 lines (404 loc) · 19 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/usr/bin/env python3
"""
Ogre Mesh/Skeleton to OBJ Converter
Converts Ogre .mesh/.skeleton files to OBJ format via XML intermediate
"""
import os
import sys
import subprocess
import xml.etree.ElementTree as ET
from pathlib import Path
import argparse
class OgreXMLConverter:
"""Handles batch conversion of Ogre binary files to XML using OgreXMLConverter"""
def __init__(self, ogre_tools_path=None):
self.converter = self._find_converter(ogre_tools_path)
def _find_converter(self, tools_path):
"""Find OgreXMLConverter executable"""
possible_names = ['OgreXMLConverter', 'OgreXMLConverter.exe']
if tools_path:
for name in possible_names:
path = Path(tools_path) / name
if path.exists():
return str(path)
# Try system PATH
for name in possible_names:
try:
# On Windows, 'where' is used; on Unix, 'which'
cmd = ['where' if os.name == 'nt' else 'which', name]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
return result.stdout.strip().splitlines()[0]
except Exception:
pass
return 'OgreXMLConverter' # Hope it's in PATH
def convert_to_xml(self, input_file, output_dir=None):
"""Convert a single .mesh or .skeleton file to XML"""
input_path = Path(input_file)
if output_dir:
output_path = Path(output_dir) / input_path.name
else:
output_path = input_path
# OgreXMLConverter adds .xml to the filename
xml_output = str(output_path) + '.xml'
try:
cmd = [self.converter, str(input_path)]
if output_dir:
# Add output directory parameter
cmd = [self.converter, str(input_path), '-d', str(output_dir)]
print(f"Running: {' '.join(cmd)}")
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
print(f"✓ Converted {input_path.name} to XML")
print(f" Output should be at: {xml_output}")
# Check if file was actually created
if Path(xml_output).exists():
return xml_output
else:
# Try alternative naming
alt_xml = str(output_path.with_suffix('')) + '.xml'
if Path(alt_xml).exists():
return alt_xml
print(f" Warning: Expected XML file not found at {xml_output}")
return None
except subprocess.CalledProcessError as e:
print(f"✗ Failed to convert {input_path.name}")
print(f" stdout: {e.stdout}")
print(f" stderr: {e.stderr}")
return None
except FileNotFoundError:
print(f"✗ OgreXMLConverter not found at: {self.converter}")
print(f" Please specify path with --ogre-tools")
return None
def batch_convert(self, input_dir, output_dir=None, extensions=['.mesh', '.skeleton']):
"""Convert all Ogre files in a directory"""
input_path = Path(input_dir)
converted_files = []
if output_dir:
Path(output_dir).mkdir(parents=True, exist_ok=True)
for ext in extensions:
for file in input_path.glob(f'*{ext}'):
xml_file = self.convert_to_xml(file, output_dir)
if xml_file:
converted_files.append(xml_file)
return converted_files
class OgreXMLToOBJ:
"""Converts Ogre XML mesh files to OBJ format"""
def __init__(self):
self.vertices = [] # list[(x,y,z)]
self.normals = [] # list[(nx,ny,nz)]
self.uvs = [] # list[(u,v)]
self.submeshes = [] # list[{material, faces, index}]
def parse_mesh_xml(self, xml_file):
"""Parse Ogre mesh XML file"""
tree = ET.parse(xml_file)
root = tree.getroot()
# Parse shared geometry if exists
shared_geom = root.find('sharedgeometry')
if shared_geom is not None:
self._parse_geometry(shared_geom, is_shared=True)
# Parse submeshes
submeshes = root.find('submeshes')
if submeshes is not None:
for idx, submesh in enumerate(submeshes.findall('submesh')):
self._parse_submesh(submesh, idx)
def _parse_geometry(self, geom_elem, is_shared=False, offset=0):
"""
Parse geometry section (vertices, normals, UVs).
Handles multiple <vertexbuffer> elements by merging them per vertex index.
"""
# Try to get vertex count from this element
vcount_attr = geom_elem.get('vertexcount')
if vcount_attr is not None:
vertex_count = int(vcount_attr)
else:
# Fallback: count vertices in the first vertexbuffer
first_vb = geom_elem.find('vertexbuffer')
if first_vb is not None:
vertex_count = len(first_vb.findall('vertex'))
else:
return 0
# Per-vertex data containers
local_verts = [None] * vertex_count
local_normals = [None] * vertex_count
local_uvs = [None] * vertex_count
any_normals = False
any_uvs = False
# Merge data from all vertexbuffers
for vb in geom_elem.findall('vertexbuffer'):
has_positions = vb.get('positions', 'false').lower() == 'true'
has_normals = vb.get('normals', 'false').lower() == 'true'
tex_coords = int(vb.get('texture_coords', '0') or 0)
has_texcoords = tex_coords > 0
for i, vertex in enumerate(vb.findall('vertex')):
if i >= vertex_count:
break # Safety
# Position
if has_positions:
pos = vertex.find('position')
if pos is not None:
x = float(pos.get('x', 0))
y = float(pos.get('y', 0))
z = float(pos.get('z', 0))
local_verts[i] = (x, y, z)
# Normal
if has_normals:
normal = vertex.find('normal')
if normal is not None:
nx = float(normal.get('x', 0))
ny = float(normal.get('y', 0))
nz = float(normal.get('z', 0))
local_normals[i] = (nx, ny, nz)
any_normals = True
# UVs (first texcoord set)
if has_texcoords:
texcoord = vertex.find('texcoord')
if texcoord is None:
# Some exporters might use texcoord0, texcoord1, etc.
for child in vertex:
if child.tag.startswith('texcoord'):
texcoord = child
break
if texcoord is not None:
u = float(texcoord.get('u', 0))
v = float(texcoord.get('v', 0))
# Flip V for OBJ (OpenGL-style to OBJ-style)
local_uvs[i] = (u, 1.0 - v)
any_uvs = True
# Fill in any missing data with safe defaults
for i in range(vertex_count):
if local_verts[i] is None:
local_verts[i] = (0.0, 0.0, 0.0)
if any_normals:
for i in range(vertex_count):
if local_normals[i] is None:
local_normals[i] = (0.0, 1.0, 0.0)
else:
local_normals = []
if any_uvs:
for i in range(vertex_count):
if local_uvs[i] is None:
local_uvs[i] = (0.0, 0.0)
else:
local_uvs = []
# Append to global arrays
self.vertices.extend(local_verts)
if any_normals:
self.normals.extend(local_normals)
if any_uvs:
self.uvs.extend(local_uvs)
return vertex_count
def _parse_submesh(self, submesh_elem, submesh_idx):
"""Parse a submesh"""
material = submesh_elem.get('material', f'material_{submesh_idx}')
uses_shared = submesh_elem.get('usesharedvertices', 'false').lower() == 'true'
vertex_offset = len(self.vertices)
# Parse local geometry if not using shared
if not uses_shared:
geom = submesh_elem.find('geometry')
if geom is not None:
vertex_offset = len(self.vertices)
self._parse_geometry(geom, is_shared=False, offset=vertex_offset)
else:
# Shared geometry starts at the beginning of global vertex list
vertex_offset = 0
# Parse faces (indices)
faces_elem = submesh_elem.find('faces')
local_faces = []
if faces_elem is not None:
for face in faces_elem.findall('face'):
v1 = int(face.get('v1')) + vertex_offset + 1 # OBJ is 1-indexed
v2 = int(face.get('v2')) + vertex_offset + 1
v3 = int(face.get('v3')) + vertex_offset + 1
local_faces.append((v1, v2, v3))
self.submeshes.append({
'material': material,
'faces': local_faces,
'index': submesh_idx,
})
def write_obj(self, output_file, mtl_file=None):
"""Write OBJ file"""
out_path = Path(output_file)
base_name = out_path.stem
total_faces = sum(len(sm['faces']) for sm in self.submeshes)
with open(output_file, 'w', encoding='utf-8') as f:
f.write("# Converted from Ogre mesh format\n")
f.write(f"# Vertices: {len(self.vertices)}\n")
f.write(f"# Faces: {total_faces}\n\n")
if mtl_file:
f.write(f"mtllib {Path(mtl_file).name}\n\n")
# Vertices
for v in self.vertices:
f.write(f"v {v[0]:.6f} {v[1]:.6f} {v[2]:.6f}\n")
f.write("\n")
# UVs
for uv in self.uvs:
f.write(f"vt {uv[0]:.6f} {uv[1]:.6f}\n")
f.write("\n")
# Normals
for n in self.normals:
f.write(f"vn {n[0]:.6f} {n[1]:.6f} {n[2]:.6f}\n")
f.write("\n")
have_uvs = len(self.uvs) == len(self.vertices) and len(self.uvs) > 0
have_normals = len(self.normals) == len(self.vertices) and len(self.normals) > 0
# Faces by submesh as separate OBJ objects
for idx, submesh in enumerate(self.submeshes):
obj_name = f"{base_name}_part{idx}"
f.write(f"# Submesh - {submesh['material']}\n")
f.write(f"o {obj_name}\n")
f.write(f"g {submesh['material']}\n")
if mtl_file:
f.write(f"usemtl {submesh['material']}\n")
for face in submesh['faces']:
if have_uvs and have_normals:
f.write(
f"f {face[0]}/{face[0]}/{face[0]} "
f"{face[1]}/{face[1]}/{face[1]} "
f"{face[2]}/{face[2]}/{face[2]}\n"
)
elif have_uvs:
f.write(
f"f {face[0]}/{face[0]} "
f"{face[1]}/{face[1]} "
f"{face[2]}/{face[2]}\n"
)
elif have_normals:
f.write(
f"f {face[0]}//{face[0]} "
f"{face[1]}//{face[1]} "
f"{face[2]}//{face[2]}\n"
)
else:
f.write(f"f {face[0]} {face[1]} {face[2]}\n")
f.write("\n")
def write_mtl(self, output_file):
"""Write MTL file and auto-wire textures by recursively searching all subfolders.
Searches for textures matching base OBJ name with suffixes:
_d (diffuse), _e (emissive), _n (normal), _s (specular)
Example: cvapc_d.tga, cvapc_e.tga, cvapc_n.tga, cvapc_s.tga
"""
out_path = Path(output_file)
base_name = out_path.stem # e.g. 'cvapc'
root_dir = Path.cwd() # start from working directory
# Suffix → MTL map type
suffix_to_map = {
"_d": "map_Kd", # diffuse
"_e": "map_Ke", # emissive
"_n": "map_Bump", # normal
"_s": "map_Ks", # specular
}
exts = [".tga", ".png", ".jpg", ".jpeg", ".dds", ".tif", ".tiff", ".bmp"]
# Recursive search for textures
def find_tex(suffix: str):
for ext in exts:
# Walk all subdirectories looking for matching filename
for path in root_dir.rglob(f"{base_name}{suffix}{ext}"):
return os.path.relpath(path, out_path.parent)
return None
found_textures = {suffix: find_tex(suffix) for suffix in suffix_to_map.keys()}
with open(output_file, "w", encoding="utf-8") as f:
f.write("# Material library for Ogre mesh\n\n")
for submesh in self.submeshes:
mat_name = submesh["material"]
f.write(f"newmtl {mat_name}\n")
f.write("Ka 1.0 1.0 1.0\n")
f.write("Kd 0.8 0.8 0.8\n")
f.write("Ks 0.5 0.5 0.5\n")
f.write("Ns 32.0\n")
f.write("d 1.0\n")
f.write("illum 2\n")
# Attach textures we found
for suffix, map_key in suffix_to_map.items():
tex = found_textures.get(suffix)
if tex:
f.write(f"{map_key} {tex}\n")
f.write("\n")
def convert(self, xml_file, obj_file, create_mtl=True):
"""Main conversion method"""
self.parse_mesh_xml(xml_file)
mtl_file = None
if create_mtl and self.submeshes:
mtl_file = Path(obj_file).with_suffix('.mtl')
self.write_mtl(mtl_file)
self.write_obj(obj_file, mtl_file)
print(f"✓ Converted {Path(xml_file).name} to {Path(obj_file).name}")
def main():
parser = argparse.ArgumentParser(
description='Convert Ogre mesh/skeleton files to OBJ format',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Convert single mesh file
python MeshToObj.py mesh.mesh -o output.obj
# Batch convert all meshes in directory
python MeshToObj.py --batch input_dir/ -o output_dir/
# Specify Ogre tools location
python MeshToObj.py mesh.mesh -o output.obj --ogre-tools /path/to/ogre/tools/
"""
)
parser.add_argument('input', help='Input .mesh file or directory (with --batch)')
parser.add_argument('-o', '--output', required=True, help='Output .obj file or directory')
parser.add_argument('--batch', action='store_true', help='Batch process directory')
parser.add_argument('--ogre-tools', help='Path to Ogre command line tools')
parser.add_argument('--keep-xml', action='store_true', help='Keep intermediate XML files')
parser.add_argument('--no-mtl', action='store_true', help='Do not create MTL file')
args = parser.parse_args()
# Initialize converter
xml_converter = OgreXMLConverter(args.ogre_tools)
if args.batch:
# Batch mode
output_dir = Path(args.output)
output_dir.mkdir(parents=True, exist_ok=True)
xml_dir = output_dir / 'xml_temp'
xml_dir.mkdir(exist_ok=True)
print(f"\n=== Converting Ogre files to XML ===")
xml_files = xml_converter.batch_convert(args.input, xml_dir)
print(f"XML conversion returned {len(xml_files)} files")
# Also scan the directory to see what's actually there
actual_xml_files = list(xml_dir.glob('*.xml'))
print(f"Actually found {len(actual_xml_files)} XML files in {xml_dir}")
for xf in actual_xml_files:
print(f" - {xf.name}")
# Use the files we actually found
if not xml_files and actual_xml_files:
print("Using files found by scanning directory...")
xml_files = [str(f) for f in actual_xml_files]
print(f"\n=== Converting XML to OBJ ===")
print(f"Processing {len(xml_files)} XML files")
for xml_file in xml_files:
xml_path = Path(xml_file)
print(f"Processing: {xml_path.name}")
# Handle both .mesh.xml and .xml naming
if xml_path.suffix == '.xml':
obj_name = xml_path.name.replace('.mesh.xml', '.obj').replace('.xml', '.obj')
obj_file = output_dir / obj_name
try:
converter = OgreXMLToOBJ()
converter.convert(xml_file, obj_file, create_mtl=not args.no_mtl)
except Exception as e:
print(f"✗ Error converting {xml_path.name}: {e}")
if not args.keep_xml:
import shutil
shutil.rmtree(xml_dir)
print(f"\n✓ Cleaned up temporary XML files")
else:
# Single file mode
print(f"\n=== Converting to XML ===")
xml_file = xml_converter.convert_to_xml(args.input)
if xml_file:
print(f"\n=== Converting XML to OBJ ===")
print(f"XML file: {xml_file}")
try:
converter = OgreXMLToOBJ()
converter.convert(xml_file, args.output, create_mtl=not args.no_mtl)
except Exception as e:
print(f"✗ Error during conversion: {e}")
import traceback
traceback.print_exc()
if not args.keep_xml:
os.remove(xml_file)
print(f"✓ Cleaned up temporary XML file")
else:
print("✗ Failed to create XML file")
print("\n✓ Conversion complete!")
if __name__ == '__main__':
main()