forked from djalex88/io_ts2_gmdc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
324 lines (274 loc) · 10.1 KB
/
__init__.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
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
#-------------------------------------------------------------------------------
# Original work: Copyright (C) 2021 DjAlex88 (https://github.com/djalex88/)
# Modified work: Copyright (C) 2024 Cosmatevs (https://github.com/Cosmatevs/)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#-------------------------------------------------------------------------------
bl_info = {
"name": "TS2 GMDC Importer/Exporter",
"description": "Import-Export TS2 GMDC, Import skeleton from CRES.",
"author": "DjAlex88 (https://github.com/djalex88/), Cosmatevs",
"version": (0, 91, 3),
"blender": (2, 80, 0),
"location": "File > Import > Import TS2 GMDC (.5gd, .gmdc)",
"category": "Import-Export",
}
import bpy
from bpy.props import BoolProperty, StringProperty, FloatProperty, EnumProperty, PointerProperty
from bpy_extras.io_utils import ImportHelper, ExportHelper
class Import_GMDC(bpy.types.Operator, ImportHelper):
"""Import TS2 GMDC"""
bl_idname = "import_scene.ts2_gmdc"
bl_label = "Import TS2 GMDC"
bl_options = {'REGISTER', 'UNDO'}
filename_ext = ".gmdc"
filepath : StringProperty(subtype='FILE_PATH')
import_mode : EnumProperty(
items = [
('GEOMETRY', "Geometry", "Import data from GMDC file", 'MESH_DATA', 0),
('SKELETON', "Skeleton", "Import data from CRES file", 'ARMATURE_DATA', 1),
],
default = 'GEOMETRY',
name = "Mode",
description = "Import mode" )
# geometry (GMDC file)
#
remove_doubles : BoolProperty(
name = "Remove doubles",
description = "If some vertices differ only in texture coordinates, then they are fused together (removes seams)",
default = True )
import_bmesh : BoolProperty(
name = "Bounding geometry",
description = "Import bounding geometry",
default = False )
replace_inv_t : BoolProperty(
name = "Replace inverse transforms",
description = "If the scene already has inverse transforms set, they will be replaced",
default = False )
# skeleton (CRES file)
#
selected_only : BoolProperty(
name = "Only for selected objects",
description = "Import skeleton and create armature modifier only for selected objects",
default = True )
all_bones : BoolProperty(
name = "All bones / transforms",
description = "Import all bones/transforms; otherwise, used bones only",
default = False )
# other
#
save_log : BoolProperty(
name = "Save log",
description = "Save log info into file *.import_log.txt",
default = False )
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
from .gmdc_import import begin_import
begin_import(
self.filepath,
context.scene,
settings={
'import_mode' : self.import_mode,
'remove_doubles' : self.remove_doubles,
'import_bmesh' : self.import_bmesh,
'replace_inv_t' : self.replace_inv_t,
'selected_only' : self.selected_only,
'all_bones' : self.all_bones,
'save_log' : self.save_log,
})
return {'FINISHED'}
def draw(self, context):
box = self.layout.box()
box.prop(self, 'import_mode')
if self.import_mode == 'GEOMETRY':
box.prop(self, 'remove_doubles')
box.prop(self, 'import_bmesh')
box.prop(self, 'replace_inv_t')
if self.import_mode == 'SKELETON':
box.prop(self, 'selected_only')
box.prop(self, 'all_bones')
box = self.layout.box()
box.label(text="Other", icon='MODIFIER')
box.prop(self, 'save_log')
#---------------------------------------
# scans ./target dir and returns list of items
def load_target_file_items():
import os
target_dir = os.path.join(os.path.dirname(__file__), 'target')
items = []
try:
file_names = os.listdir(target_dir)
except:
pass
else:
for name in file_names:
path = os.path.join(target_dir, name)
if os.path.isfile(path):
items.append((path, name, ""))
return items
class Export_GMDC(bpy.types.Operator, ExportHelper):
"""Export TS2 GMDC"""
bl_idname = "export_scene.ts2_gmdc"
bl_label = "Export TS2 GMDC"
bl_options = {'REGISTER'}
filename_ext = ".gmdc"
filepath : StringProperty(subtype='FILE_PATH')
# main properties
#
selected_only : BoolProperty(
name = "Only selected objects",
description = "Export only selected objects",
default = False )
apply_transforms : BoolProperty(
name = "Apply rotation & scale",
description = "Apply rotation and scaling to mesh objects",
default = True )
export_rigging : BoolProperty(
name = "Rigging data",
description = "Export rigging data (bone indices, weights)",
default = False )
export_tangents : BoolProperty(
name = "Tangents",
description = "Calculate and export tangent vectors (required for bump mapping)",
default = False )
export_bmesh : BoolProperty(
name = "Bounding geometry",
description = "Create mesh object for bounding geometry (if any)",
default = False )
bmesh_name : StringProperty(
name = "Obj. name",
description = "Name of mesh object of bounding geometry",
default = "b_mesh" )
bmesh_threshold : FloatProperty(
name = "Bone weight threshold",
description = "Minimum bone weight required to include a triangle into bounding mesh",
default = 0.5,
min = 0.05,
max = 1,
step = 0.05,
precision = 2 )
export_morphs : EnumProperty(
items = [
('0', "Do not export morphs", "Ignore shape keys (if exist); no morph data created"),
('1', "Diff. in v.coords only", "Use only vertex coordinates to calculate morph data"),
('2', "Diff. in v.coords and normals", "Calculate morph data from vertex coordinates and normals"),
],
default = '0',
name = "Morphs",
description = "Calculate morph data from shape keys" )
align_normals : BoolProperty(
name = "Align normals",
description = "Align normals of vertices connecting to head mesh",
default = False )
align_target : EnumProperty(
items = load_target_file_items(),
default = None,
name = "Target",
description = "Reference mesh to align normals to" )
resource_name : StringProperty(
name = "SGResource",
description = "SGResource name of this geometry",
default = "" )
# other
#
name_suffix : BoolProperty(
name = "_tslocator_gmdc",
description = "Add default suffix",
default = True )
use_obj_props : BoolProperty(
name = "Use object properties",
description = "Custom properties for objects (e.g. flags, name). See object properties panel",
default = False )
save_log : BoolProperty(
name = "Save log",
description = "Save log info into file *.export_log.txt",
default = False )
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
from .gmdc_export import begin_export
begin_export(
bpy.path.ensure_ext(self.filepath, self.filename_ext),
context.scene,
settings={
'selected_only' : self.selected_only,
'apply_transforms' : self.apply_transforms,
'export_rigging' : self.export_rigging,
'export_tangents' : self.export_tangents,
'export_bmesh' : self.export_bmesh,
'bmesh_name' : self.bmesh_name.strip(),
'bmesh_threshold' : self.bmesh_threshold,
'export_morphs' : int(self.export_morphs),
'align_normals' : self.align_normals,
'align_target' : self.align_target,
'resource_name' : self.resource_name.strip(),
'name_suffix' : self.name_suffix,
'use_obj_props' : self.use_obj_props,
'save_log' : self.save_log,
})
return {'FINISHED'}
def draw(self, context):
box = self.layout.box()
box.label(text="Geometry", icon='MESH_DATA')
box.prop(self, 'selected_only')
box.prop(self, 'apply_transforms')
box.prop(self, 'export_rigging')
box.prop(self, 'export_tangents')
box.prop(self, 'export_morphs')
box.prop(self, 'align_normals')
if self.align_normals:
box.prop(self, 'align_target')
box.prop(self, 'export_bmesh')
if self.export_bmesh:
box.prop(self, 'bmesh_name')
box.prop(self, 'bmesh_threshold')
box = self.layout.box()
box.label(text="Other", icon='MODIFIER')
box.label(text="SGResource:")
row = box.split(factor=0.5, align=True)
row.prop(self, 'resource_name', text="")
row.prop(self, 'name_suffix', toggle=True)
box.prop(self, 'use_obj_props')
box.prop(self, 'save_log')
#---------------------------------------
def menu_import(self, context):
self.layout.operator(Import_GMDC.bl_idname, text="GMDC (.gmdc, .5gd)")
def menu_export(self, context):
self.layout.operator(Export_GMDC.bl_idname, text="GMDC (.gmdc)")
classes = (
Import_GMDC,
Export_GMDC,
)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
bpy.types.TOPBAR_MT_file_import.append(menu_import)
bpy.types.TOPBAR_MT_file_export.append(menu_export)
def unregister():
from bpy.utils import unregister_class
for cls in classes:
unregister_class(cls)
bpy.types.TOPBAR_MT_file_import.remove(menu_import)
bpy.types.TOPBAR_MT_file_export.remove(menu_export)
if __name__ == "__main__":
register()