-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
238 lines (196 loc) · 9.73 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
# Copyright (C) 2021-2024
# Created by LightningPirate
# This file is part of SWE1R Import/Export.
# SWE1R Import/Export 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
# MERCHANTABILITY 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 <https://www.gnu.org
# /licenses>.
bl_info = {
"name": "SWE1R Import/Export",
"blender": (2, 80, 0),
"category": "Object",
}
import os
import json
if "bpy" in locals(): #means Blender already started once
print('already loaded in blender')
import importlib
importlib.reload(swr_import)
importlib.reload(swr_export)
importlib.reload(block)
importlib.reload(modelblock)
importlib.reload(textureblock)
importlib.reload(splineblock)
importlib.reload(general)
importlib.reload(popup)
else: #start up
print('starting up for the first time')
from .swr_import import *
from .swr_export import *
from .popup import *
from .model_list import *
from .modelblock import *
from .block import *
from .textureblock import *
from .splineblock import *
from .general import *
import bpy
version = bpy.app.version_string
version = version.split(".")
if version[0] is not "4" and version[1] is not "0":
print("blender-swe1r: Unsupported Blender version")
SETTINGS_FILE = os.path.join(bpy.utils.user_resource('CONFIG'), "blender_swe1r_settings.json")
def save_settings(self, context):
keys = ['import_folder', 'import_type', 'import_model', 'export_folder', 'export_model', 'export_texture', 'export_spline']
settings = load_settings()
for key in [key for key in keys if context.scene.get(key) is not None]:
settings[key] = context.scene.get(key)
with open(SETTINGS_FILE, 'w') as f:
json.dump(settings, f)
def load_settings():
settings = {}
if os.path.exists(SETTINGS_FILE):
with open(SETTINGS_FILE, 'r') as f:
settings = json.load(f)
return settings
def set_setting(key, value):
settings = load_settings()
settings[key] = value
save_settings(settings)
def get_setting(key, default=None):
settings = load_settings()
val = settings.get(key, default)
return val if val is not None else default
model_types = [('0', 'All', 'View all models'),
('1', 'MAlt', 'High LOD pods'),
('2', 'Modl', 'Misc animated elements'),
('3', 'Part', 'Misc props'),
('4', 'Podd', 'Pod models'),
('5', 'Pupp', 'Animated racers'),
('6', 'Scen', 'Animated scenes'),
('7', 'Trak', 'Tracks'),
]
models = [(str(i), f"{model['extension']} {model['name']}", f"Import model {model['name']}") for i, model in enumerate(model_list)]
def update_model_dropdown(self, context):
model_type = model_types[int(context.scene.import_type)][1]
if model_type == 'All':
items_for_selected_category = [(str(i), f"{model['extension']} {model['name']}", f"Import model {model['name']}") for i, model in enumerate(model_list)]
else:
items_for_selected_category = [(str(i), f"{model['name']}", f"Import model {model['name']}") for i, model in enumerate(model_list) if model['extension'] == model_type]
items_for_selected_category.insert(0, ('-1', 'All', 'Import all models'))
bpy.types.Scene.import_model = bpy.props.EnumProperty(
items=items_for_selected_category,
name="Model Selection",
description="Select models to import",
)
save_settings(self, context)
class ImportExportExamplePanel(bpy.types.Panel):
bl_label = "SWE1R Import/Export"
bl_idname = "PT_ImportExportExamplePanel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'SWE1R Import/Export'
def draw(self, context):
layout = self.layout
# Section 1: Import
box = layout.box()
box.label(text="Import")
box.prop(context.scene, "import_folder", full_event=False)
box.prop(context.scene, "import_type", text="Type")
box.prop(context.scene, "import_model", text="Model")
box.operator("import.import_operator", text="Import")
# Section 2: Export
box = layout.box()
box.label(text="Export")
box.prop(context.scene, "export_folder", text="", full_event=False)
# Checkboxes on the same row
row = box.row()
row.prop(context.scene, "export_model", text="Model", icon='MESH_CUBE', toggle=True, icon_only=True)
row.prop(context.scene, "export_texture", text="Texture", icon='MATERIAL', toggle=True, icon_only=True)
row.prop(context.scene, "export_spline", text="Spline", icon='CURVE_BEZCURVE', toggle=True, icon_only=True)
box.operator("import.export_operator", text="Export")
class ImportOperator(bpy.types.Operator):
bl_label = "SWE1R Import/Export"
bl_idname = "import.import_operator"
def execute(self, context):
folder_path = context.scene.import_folder
if folder_path == "":
show_custom_popup(bpy.context, "No set import folder", "Select your folder containing the .bin files")
return {'CANCELLED'}
if folder_path[:2] == '//':
folder_path = os.path.join(os.path.dirname(bpy.data.filepath), folder_path[2:])
if not os.path.exists(folder_path + 'out_modelblock.bin'):
show_custom_popup(bpy.context, "Missing required files", "No out_modelblock.bin found in the selected folder.")
return {'CANCELLED'}
import_model(folder_path, [int(context.scene.import_model)])
return {'FINISHED'}
class ExportOperator(bpy.types.Operator):
bl_label = "SWE1R Import/Export"
bl_idname = "import.export_operator"
def execute(self, context):
selected_objects = context.selected_objects
selected_collection = context.view_layer.active_layer_collection.collection
if selected_objects:
selected_collection = selected_objects[0].users_collection[0]
if selected_collection is None:
show_custom_popup(bpy.context, "No collection", "Exported items must be part of a collection")
return {'CANCELLED'}
folder_path = context.scene.export_folder if context.scene.export_folder else context.scene.import_folder
if 'ind' not in selected_collection:
show_custom_popup(bpy.context, "Invalid collection selected", "Please select a model collection to export")
return {'CANCELLED'}
if folder_path == "":
show_custom_popup(bpy.context, "No set export folder", "Select your folder containing the .bin files")
return {'CANCELLED'}
if not os.path.exists(folder_path + 'out_modelblock.bin'):
show_custom_popup(bpy.context, "Missing required files", "No out_modelblock.bin found in the selected folder.")
return {'CANCELLED'}
export_model(selected_collection, folder_path, [context.scene.export_model, context.scene.export_texture, context.scene.export_spline])
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(ImportOperator.bl_idname)
self.layout.operator(ExportOperator.bl_idname)
def register():
bpy.types.Scene.import_folder = bpy.props.StringProperty(subtype='DIR_PATH', update=save_settings, default =get_setting('import_folder', ""), description="Select the lev01 folder (or any folder containing the .bin files)")
bpy.types.Scene.import_type = bpy.props.EnumProperty(
items=model_types,
name="Model Type",
description="Select model type",
default=get_setting('import_type', 0),
update=update_model_dropdown
)
bpy.types.Scene.import_model = bpy.props.EnumProperty(
items=models,
name="Model",
description="Select model",
default=get_setting('import_model', 0),
update=save_settings
)
bpy.types.Scene.export_folder = bpy.props.StringProperty(subtype='DIR_PATH', update=save_settings, default=get_setting('export_folder', ""), description="Select the lev01 folder (or any folder you wish to export to)")
bpy.types.Scene.export_model = bpy.props.BoolProperty(name="Model", update=save_settings, default=get_setting('export_model', True))
bpy.types.Scene.export_texture = bpy.props.BoolProperty(name="Texture", update=save_settings, default=get_setting('export_texture', True))
bpy.types.Scene.export_spline = bpy.props.BoolProperty(name="Spline", update=save_settings, default=get_setting('export_spline', True))
bpy.utils.register_class(ImportExportExamplePanel)
bpy.utils.register_class(ImportOperator)
bpy.utils.register_class(ExportOperator)
bpy.types.TOPBAR_MT_file.append(menu_func)
def unregister():
bpy.utils.unregister_class(ImportExportExamplePanel)
bpy.utils.unregister_class(ImportOperator)
bpy.utils.unregister_class(ExportOperator)
bpy.types.TOPBAR_MT_file.remove(menu_func)
del bpy.types.Scene.import_folder
del bpy.types.Scene.import_type
del bpy.types.Scene.import_model
del bpy.types.Scene.export_folder
del bpy.types.Scene.export_model
del bpy.types.Scene.export_texture
del bpy.types.Scene.export_spline