-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3ds2gbxml.py
165 lines (145 loc) · 6.35 KB
/
3ds2gbxml.py
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
import logging
import sys
import argparse
import os
from pathlib import Path
import CPlugSurface
import CPlugVisualIndexedTriangles
from CPlugErrors import NoTrimeshError, NoVerticesError, NoFacesError
from modules.threedees import read_3ds, IncorrectFormatError, DataError, EditorChunk, ObjectBlock
VERSION = '1.0.8'
if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog='3ds2gbxml'
)
parser.add_argument('file')
parser.add_argument('-lf', '--logfile',
dest='logfile')
parser.add_argument('-i', '--info',
dest='verbose', action='store_true')
parser.add_argument('-v', '--visual',
dest='visual', action='store_true')
parser.add_argument('-a', '--animate',
dest='animate', action='store_true')
parser.add_argument('-s', '--surface',
dest='surface', action='store_true')
parser.add_argument('--tmf',
dest='tmf', action='store_true')
args = parser.parse_args()
# Set up logger
loglevel = logging.WARNING
if args.verbose:
loglevel = logging.INFO
logfile = None
if args.logfile:
logfile = args.logfile
logging.basicConfig(
level=loglevel,
format='%(asctime)s-[%(levelname)s]: %(message)s',
filename=logfile
)
logging.info(f'3ds2gbxml version {VERSION}')
if not args.visual and not args.surface and not args.animate:
logging.error('Conversion Error: no mode selected')
sys.exit(1)
try:
logging.info('===============================')
chunk = read_3ds(args.file)
objects: list = []
# Find model objects in the file
for editor_chunk in chunk.children:
if isinstance(editor_chunk, EditorChunk):
for obj in editor_chunk.children:
if isinstance(obj, ObjectBlock):
if len(obj.children) > 0:
objects.append(obj)
if len(objects) == 0:
logging.error('Conversion Error: no objects to convert')
sys.exit(1)
saved_to: list = []
if args.animate:
# Add SubVisuals
try:
logging.info('===============================')
gbx_tree = CPlugVisualIndexedTriangles.create_anim_xml(objects)
print(gbx_tree)
save_path_file = f"{Path(args.file).stem}.CPlugVisualIndexedTriangles.xml"
with open(save_path_file, "wb") as f:
gbx_tree.write(f)
logging.info(f'Saved to "{save_path_file}')
saved_to.append(save_path_file)
except OSError as e:
logging.error(f'Failed to open "{e.filename}" for writing, message: {e.args[1]}')
sys.exit(e.errno)
except NoTrimeshError:
logging.error('Conversion Error: No mesh present')
sys.exit(1)
except NoVerticesError:
logging.error('Conversion Error: No vertices present')
sys.exit(1)
except NoFacesError:
logging.error('Conversion Error: No faces present')
sys.exit(1)
else:
for model_obj in objects:
name = model_obj.name.split('$')[0]
# To Visual Mesh
if args.visual:
try:
logging.info('===============================')
gbx_tree = CPlugVisualIndexedTriangles.create_xml(model_obj)
save_path_file = f"{name}.CPlugVisualIndexedTriangles.xml"
with open(save_path_file, "wb") as f:
gbx_tree.write(f)
logging.info(f'Saved to "{save_path_file}')
saved_to.append(save_path_file)
except OSError as e:
logging.error(f'Failed to open "{e.filename}" for writing, message: {e.args[1]}')
sys.exit(e.errno)
except NoTrimeshError:
logging.error('Conversion Error: No mesh present')
sys.exit(1)
except NoVerticesError:
logging.error('Conversion Error: No vertices present')
sys.exit(1)
except NoFacesError:
logging.error('Conversion Error: No faces present')
sys.exit(1)
# To Collision Surface
if args.surface:
try:
logging.info('===============================')
gbx_tree = CPlugSurface.create_xml(objects, args.tmf)
if args.tmf:
save_path_file = f"{Path(args.file).stem}.CPlugSurfaceGeom.xml"
else:
save_path_file = f"{Path(args.file).stem}.CPlugSurfaceCrystal.xml"
with open(save_path_file, "wb") as f:
gbx_tree.write(f)
logging.info(f'Saved to "{save_path_file}')
saved_to.append(save_path_file)
except OSError as e:
logging.error(f'Failed to open "{e.filename}" for writing, message: {e.args[1]}')
sys.exit(e.errno)
except NoTrimeshError:
logging.error('Conversion Error: No mesh present')
sys.exit(1)
except NoVerticesError:
logging.error('Conversion Error: No vertices present')
sys.exit(1)
except NoFacesError:
logging.error('Conversion Error: No faces present')
sys.exit(1)
except OSError as e:
logging.error(f'Failed to open "{e.filename}" for reading, message: {e.args[1]}')
sys.exit(e.errno)
except IncorrectFormatError:
logging.error('Parser Error: incorrect 3ds file')
sys.exit(1)
except DataError as e:
logging.error(f'Parser Error: incorrect data at offset: {hex(e.position)}, message: "{e.args[0]}"')
sys.exit(1)
logging.info('Done!')
print(f'Successfully saved the following {len(saved_to)} files:')
for file_path in saved_to:
print(file_path)