forked from RandomTBush/RTB-3DSMax-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path__init__.py
More file actions
286 lines (231 loc) · 10.6 KB
/
__init__.py
File metadata and controls
286 lines (231 loc) · 10.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
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import bpy
import os
from .import_d3dmesh import import_d3dmesh
from .import_skl import import_skl
from bpy.props import StringProperty
from bpy_extras.io_utils import ImportHelper
from math import pi
import time
class D3DMesh_ImportOperator(bpy.types.Operator, ImportHelper):
bl_idname = "import_scene.d3dmesh"
bl_label = "Import D3DMesh"
bl_options = {'REGISTER', 'PRESET'}
# WOAS: I hate how blender api online docs don't have ImportHelper templates or any explanation
# so you have to dig through templates built into blender's text editor
directory: bpy.props.StringProperty(subtype='FILE_PATH', options={'SKIP_SAVE', 'HIDDEN'})
files: bpy.props.CollectionProperty(type=bpy.types.OperatorFileListElement, options={'SKIP_SAVE', 'HIDDEN'})
rotation : bpy.props.FloatVectorProperty(
name="Rotation",
subtype='EULER',
unit='ROTATION',
default=(pi/2,0,0)
)
scale : bpy.props.FloatVectorProperty(
name="Scale",
default=(1,1,1)
)
parse_skeleton : bpy.props.BoolProperty(
name="Import Skeleton Files (WIP)",
default=False,
description="Parse Skeleton Files (*.skl)\nNot Yet Implemented"
)
parse_materials : bpy.props.BoolProperty(
name="Parse Materials",
default=False,
description=""
)
parse_textures : bpy.props.BoolProperty(
name="Parse Textures",
default=False,
description=""
)
parse_uv_layers : bpy.props.BoolProperty(
name="Parse UV Layers",
default=True,
)
parse_lods: bpy.props.BoolProperty(
name="Parse LODs",
description="If unchecked only first LOD is going to be imported",
default=False,
)
join_submeshes: bpy.props.BoolProperty(
name="Join Submeshes",
description="",
default=False,
)
early_game_fix : bpy.props.EnumProperty(
name="Early Game Fix",
items=[
("OLD", "Texas Hold'em / Bone / CSI 3/4 / Sam and Max S1/S2 (Ep. 1/2)",""),
("SM2-34", "Sam and Max Season 2 (Ep. 3/4)",""),
("SM2-5", "Sam and Max Season 2 (Ep. 5 - What's New, Beelzebub?)",""),
("SBCG4AP-1", "Strong Bad's CG4AP (Ep. 1 - Homestar Ruiner)",""),
("SBCG4AP-2", "Strong Bad's CG4AP (Ep. 2 - Strong Badia the Free)",""),
("SBCG4AP-3", "Strong Bad's CG4AP (Ep. 3 - Baddest of the Bands)",""),
("SBCG4AP-4", "Strong Bad's CG4AP (Ep. 4 - Dangeresque 3)",""),
("SBCG4AP-5", "Strong Bad's CG4AP (Ep. 5 - 8-Bit is Enough)",""),
("WG", "Wallace and Gromit (Ep. 1-3)",""),
],
description="Early Game Fix\nNot Yet Implemented"
)
verbose: bpy.props.BoolProperty(
name="Verbose Console Output",
description="Output extra info to the console\nMay slow down operation",
default=True,
)
filter_glob: StringProperty(
default="*.d3dmesh;*.skl",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
def execute(self, context):
if not self.directory:
return {'CANCELLED'}
prefs = AddonPreferences(context.preferences.addons[__name__].preferences)
if self.parse_skeleton and prefs.bone_names_cached_amt == 0:
prefs.load_databases(force = False)
if (self.parse_textures or self.parse_materials) and prefs.tex_names_cached_amt == 0:
prefs.load_databases(force = False)
start_time = time.time()
for f in self.files:
fpath = os.path.join(self.directory, f.name)
print(f"Processing {fpath}...")
new_objs = []
match os.path.splitext(f.name)[1]:
case ".d3dmesh":
new_objs = import_d3dmesh(
filepath=fpath,
verbose=self.verbose,
parse_uv_layers=self.parse_uv_layers,
early_game_fix=self.early_game_fix,
parse_lods=self.parse_lods,
join_submeshes=self.join_submeshes,
bone_db=prefs.get_bone_database(),
tex_db=prefs.get_tex_database(),
)
case ".skl":
self.report({'WARNING'},f".skl files not supported yet")
import_skl(
#params would go here
)
for new_obj in new_objs:
match type(new_obj):
case bpy.types.Object:
context.scene.collection.objects.link(new_obj)
new_obj.rotation_euler = self.rotation
new_obj.scale = self.scale
case _:
print(new_obj)
if self.verbose: print(f"Finished Importing in {time.time()-start_time:.2f}s")
self.report({'INFO'}, f"Finished Importing ({time.time()-start_time:.2f}s)")
return {"FINISHED"}
def draw(self, context):
layout = self.layout
box = layout.box()
box.label(text="Supports selecting multiple files", icon='DOCUMENTS')
layout.prop(self, "rotation")
layout.prop(self, "scale")
layout.prop(self, "parse_materials", icon='MATERIAL_DATA')
layout.prop(self, "parse_textures", icon='TEXTURE')
layout.prop(self, "parse_uv_layers", icon='UV_DATA')
layout.prop(self, "join_submeshes", icon='STICKY_UVS_LOC')
layout.prop(self, "parse_lods", icon='MOD_MULTIRES')
r = layout.row()
r.prop(self, "parse_skeleton", icon='ARMATURE_DATA')
r.enabled = False
r = layout.row()
r.label(text="Early Game Fix:", icon='GHOST_DISABLED')
r.prop(self, "early_game_fix", text="")
r.enabled = False
layout.prop(self, "verbose", icon='CONSOLE')
cache_box = layout.box()
cache_box = cache_box.column()
bone_names_cached_amt = context.preferences.addons[__name__].preferences.bone_names_cached_amt
tex_names_cached_amt = context.preferences.addons[__name__].preferences.tex_names_cached_amt
if bone_names_cached_amt == 0:
cache_box.label(text="Bone Names DB not loaded",icon='CHECKBOX_DEHLT')
else:
cache_box.label(text=f"Bone Names DB loaded ({bone_names_cached_amt}) items)",icon='CHECKBOX_HLT')
if tex_names_cached_amt == 0:
cache_box.label(text="Texture Names DB not loaded",icon='CHECKBOX_DEHLT')
else:
cache_box.label(text=f"Texture Names DB loaded ({tex_names_cached_amt} items)",icon='CHECKBOX_HLT')
cache_box.operator("import.ttg_hashdb", icon='IMPORT')
class Manual_db_import(bpy.types.Operator):
bl_idname = "import.ttg_hashdb"
bl_label = "Manually Load Databases"
bl_description = "Loading entire database can be slow, so it's cached internally\n\
Hash databases are loaded on first import automatically\n\
This operator is made to force reload the databases (in case RTB updates them)\n\
Keep in mind loading the databases takes some time. Disabling the addon will unload the cache"
def execute(self, context):
context.preferences.addons[__name__].preferences.load_databases(force = True)
return {"FINISHED"}
class AddonPreferences(bpy.types.AddonPreferences):
bl_idname = __name__
bone_names_cache_pickled : bpy.props.StringProperty(
name="Bone Names DB pickled"
)
texture_names_cache_pickled : bpy.props.StringProperty(
name="Texture Names DB pickled"
)
bone_names_cached_amt : bpy.props.IntProperty(default=0)
tex_names_cached_amt : bpy.props.IntProperty(default=0)
def load_databases(self, force = False):
import pickle
from .import_d3dmesh import load_bones_db, load_tex_db
if self.bone_names_cache_pickled == "" or force:
bones_db = load_bones_db(verbose=True)
self.bone_names_cached_amt = len(bones_db)
self.bone_names_cache_pickled = pickle.dumps(bones_db).hex()
if self.texture_names_cache_pickled == "" or force:
tex_db = load_tex_db(verbose=True)
self.tex_names_cached_amt = len(tex_db)
self.texture_names_cache_pickled = pickle.dumps(tex_db).hex()
def get_bone_database(self):
if self.bone_names_cache_pickled == "":
return {}
import pickle
return pickle.loads(bytes.fromhex(self.bone_names_cache_pickled))
def get_tex_database(self):
if self.texture_names_cache_pickled == "":
return {}
import pickle
return pickle.loads(bytes.fromhex(self.texture_names_cache_pickled))
def draw(self, context):
layout = self.layout
cache_box = layout.column()
bone_names_cached_amt = self.bone_names_cached_amt
tex_names_cached_amt = self.tex_names_cached_amt
if bone_names_cached_amt == 0:
cache_box.label(text="Bone Names DB not loaded",icon='CHECKBOX_DEHLT')
else:
cache_box.label(text=f"Bone Names DB loaded ({bone_names_cached_amt}) items)",icon='CHECKBOX_HLT')
if tex_names_cached_amt == 0:
cache_box.label(text="Texture Names DB not loaded",icon='CHECKBOX_DEHLT')
else:
cache_box.label(text=f"Texture Names DB loaded ({tex_names_cached_amt} items)",icon='CHECKBOX_HLT')
cache_box.operator("import.ttg_hashdb", icon='IMPORT')
classes_to_register = [D3DMesh_ImportOperator, Manual_db_import, AddonPreferences]
def menu_func_import(self, context):
self.layout.operator(D3DMesh_ImportOperator.bl_idname, text="D3DMesh (.d3dmesh)")
def register():
for cls in classes_to_register:
bpy.utils.register_class(cls)
bpy.types.TOPBAR_MT_file_import.append(menu_func_import)
def unregister():
for cls in classes_to_register:
bpy.utils.unregister_class(cls)
bpy.types.TOPBAR_MT_file_import.remove(menu_func_import)