2019-11-11 10:03:52 +00:00
|
|
|
bl_info = {
|
2022-01-19 03:47:05 +00:00
|
|
|
'name': 'SWBF .msh Import-Export',
|
|
|
|
'author': 'Will Snyder, SleepKiller',
|
|
|
|
"version": (1, 0, 0),
|
2022-01-21 04:42:36 +00:00
|
|
|
'blender': (2, 80, 0),
|
2019-11-11 10:03:52 +00:00
|
|
|
'location': 'File > Import-Export',
|
|
|
|
'description': 'Export as SWBF .msh file',
|
|
|
|
'warning': '',
|
2019-11-21 13:10:21 +00:00
|
|
|
'wiki_url': "https://github.com/SleepKiller/SWBF-msh-Blender-Export/blob/master/docs/reference_manual.md",
|
|
|
|
'tracker_url': "https://github.com/SleepKiller/SWBF-msh-Blender-Export/issues",
|
2019-11-11 10:03:52 +00:00
|
|
|
'support': 'COMMUNITY',
|
|
|
|
'category': 'Import-Export'
|
|
|
|
}
|
|
|
|
|
|
|
|
# Taken from glTF-Blender-IO, because I do not understand Python that well
|
|
|
|
# (this is the first thing of substance I've created in it) and just wanted
|
|
|
|
# script reloading to work.
|
|
|
|
#
|
|
|
|
# https://github.com/KhronosGroup/glTF-Blender-IO
|
|
|
|
#
|
|
|
|
# Copyright 2018-2019 The glTF-Blender-IO authors.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
def reload_package(module_dict_main):
|
|
|
|
import importlib
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
def reload_package_recursive(current_dir, module_dict):
|
|
|
|
for path in current_dir.iterdir():
|
|
|
|
if "__init__" in str(path) or path.stem not in module_dict:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if path.is_file() and path.suffix == ".py":
|
|
|
|
importlib.reload(module_dict[path.stem])
|
|
|
|
elif path.is_dir():
|
|
|
|
reload_package_recursive(path, module_dict[path.stem].__dict__)
|
|
|
|
|
|
|
|
reload_package_recursive(Path(__file__).parent, module_dict_main)
|
|
|
|
|
|
|
|
|
|
|
|
if "bpy" in locals():
|
|
|
|
reload_package(locals())
|
|
|
|
# End of stuff taken from glTF
|
|
|
|
|
|
|
|
import bpy
|
2020-10-20 18:28:53 +00:00
|
|
|
from bpy_extras.io_utils import ExportHelper, ImportHelper
|
2020-12-11 16:50:03 +00:00
|
|
|
from bpy.props import BoolProperty, EnumProperty, CollectionProperty
|
2022-02-09 19:50:52 +00:00
|
|
|
from bpy.types import Operator, Menu
|
2022-01-28 22:28:12 +00:00
|
|
|
from .msh_scene_utilities import create_scene, set_scene_animation
|
2019-11-11 10:03:52 +00:00
|
|
|
from .msh_scene_save import save_scene
|
2020-10-20 18:28:53 +00:00
|
|
|
from .msh_scene_read import read_scene
|
2019-11-15 03:28:09 +00:00
|
|
|
from .msh_material_properties import *
|
2020-12-11 21:49:40 +00:00
|
|
|
from .msh_skeleton_properties import *
|
2022-01-18 20:16:49 +00:00
|
|
|
from .msh_collision_prim_properties import *
|
2020-10-30 20:59:54 +00:00
|
|
|
from .msh_to_blend import *
|
2021-01-07 05:26:19 +00:00
|
|
|
from .zaa_to_blend import *
|
2019-11-11 10:03:52 +00:00
|
|
|
|
|
|
|
|
2019-11-20 02:17:25 +00:00
|
|
|
class ExportMSH(Operator, ExportHelper):
|
|
|
|
""" Export the current scene as a SWBF .msh file. """
|
|
|
|
|
|
|
|
bl_idname = "swbf_msh.export"
|
|
|
|
bl_label = "Export SWBF .msh File"
|
2019-11-11 10:03:52 +00:00
|
|
|
filename_ext = ".msh"
|
|
|
|
|
|
|
|
filter_glob: StringProperty(
|
|
|
|
default="*.msh",
|
|
|
|
options={'HIDDEN'},
|
|
|
|
maxlen=255, # Max internal buffer length, longer would be clamped.
|
|
|
|
)
|
|
|
|
|
2019-11-20 02:17:25 +00:00
|
|
|
generate_triangle_strips: BoolProperty(
|
|
|
|
name="Generate Triangle Strips",
|
|
|
|
description="Triangle strip generation can be slow for meshes with thousands of faces "
|
|
|
|
"and is off by default to enable fast mesh iteration.\n\n"
|
|
|
|
"In order to improve runtime performance and reduce munged model size you are "
|
|
|
|
"**strongly** advised to turn it on for your 'final' export!",
|
|
|
|
default=False
|
2019-11-11 10:03:52 +00:00
|
|
|
)
|
|
|
|
|
2020-01-06 04:08:36 +00:00
|
|
|
export_target: EnumProperty(name="Export Target",
|
|
|
|
description="What to export.",
|
|
|
|
items=(
|
|
|
|
('SCENE', "Scene", "Export the current active scene."),
|
|
|
|
('SELECTED', "Selected", "Export the currently selected objects and their parents."),
|
|
|
|
('SELECTED_WITH_CHILDREN', "Selected with Children", "Export the currently selected objects with their children and parents.")
|
|
|
|
),
|
|
|
|
default='SCENE')
|
|
|
|
|
2019-11-20 06:18:49 +00:00
|
|
|
apply_modifiers: BoolProperty(
|
|
|
|
name="Apply Modifiers",
|
|
|
|
description="Whether to apply Modifiers during export or not.",
|
|
|
|
default=True
|
|
|
|
)
|
|
|
|
|
2020-10-20 03:18:08 +00:00
|
|
|
|
2022-01-28 22:28:12 +00:00
|
|
|
animation_export: EnumProperty(name="Export Animation(s)",
|
|
|
|
description="If/how animation data should be exported.",
|
|
|
|
items=(
|
|
|
|
('NONE', "None", "Do not include animation data in the export."),
|
|
|
|
('ACTIVE', "Active", "Export animation extracted from the scene's Armature's active Action."),
|
|
|
|
('BATCH', "Batch", "Export a separate animation file for each Action in the scene.")
|
|
|
|
),
|
|
|
|
default='NONE')
|
2020-10-16 18:56:03 +00:00
|
|
|
|
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
def execute(self, context):
|
2020-10-16 18:56:03 +00:00
|
|
|
|
2022-01-28 22:28:12 +00:00
|
|
|
scene, armature_obj = create_scene(
|
|
|
|
generate_triangle_strips=self.generate_triangle_strips,
|
|
|
|
apply_modifiers=self.apply_modifiers,
|
|
|
|
export_target=self.export_target,
|
|
|
|
skel_only=self.animation_export != 'NONE') # Exclude geometry data (except root stuff) if we're doing anims
|
|
|
|
|
|
|
|
if self.animation_export != 'NONE' and not armature_obj:
|
|
|
|
raise Exception("Could not find an armature object from which to export animations!")
|
|
|
|
|
|
|
|
|
|
|
|
def write_scene_to_file(filepath : str, scene_to_write : Scene):
|
|
|
|
with open(filepath, 'wb') as output_file:
|
|
|
|
save_scene(output_file=output_file, scene=scene_to_write)
|
|
|
|
|
|
|
|
if self.animation_export == 'ACTIVE':
|
|
|
|
set_scene_animation(scene, armature_obj)
|
|
|
|
write_scene_to_file(self.filepath, scene)
|
|
|
|
|
|
|
|
elif self.animation_export == 'BATCH':
|
|
|
|
export_dir = self.filepath if os.path.isdir(self.filepath) else os.path.dirname(self.filepath)
|
|
|
|
|
|
|
|
for action in bpy.data.actions:
|
|
|
|
anim_save_path = os.path.join(export_dir, action.name + ".msh")
|
|
|
|
armature_obj.animation_data.action = action
|
|
|
|
set_scene_animation(scene, armature_obj)
|
|
|
|
write_scene_to_file(anim_save_path, scene)
|
|
|
|
else:
|
|
|
|
write_scene_to_file(self.filepath, scene)
|
2019-11-11 10:03:52 +00:00
|
|
|
|
2019-11-15 03:28:09 +00:00
|
|
|
return {'FINISHED'}
|
2019-11-11 10:03:52 +00:00
|
|
|
|
2020-10-20 18:28:53 +00:00
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
# Only needed if you want to add into a dynamic menu
|
|
|
|
def menu_func_export(self, context):
|
2019-11-20 02:17:25 +00:00
|
|
|
self.layout.operator(ExportMSH.bl_idname, text="SWBF msh (.msh)")
|
2019-11-11 10:03:52 +00:00
|
|
|
|
2020-10-20 18:28:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ImportMSH(Operator, ImportHelper):
|
|
|
|
""" Import an SWBF .msh file. """
|
|
|
|
|
|
|
|
bl_idname = "swbf_msh.import"
|
2022-01-28 22:28:12 +00:00
|
|
|
bl_label = "Import SWBF .msh File(s)"
|
2020-10-20 18:28:53 +00:00
|
|
|
filename_ext = ".msh"
|
|
|
|
|
2020-12-11 16:50:03 +00:00
|
|
|
files: CollectionProperty(
|
|
|
|
name="File Path",
|
|
|
|
type=bpy.types.OperatorFileListElement,
|
|
|
|
)
|
|
|
|
|
2020-10-20 18:28:53 +00:00
|
|
|
filter_glob: StringProperty(
|
2021-01-07 05:26:19 +00:00
|
|
|
default="*.msh;*.zaa;*.zaabin",
|
2020-10-20 18:28:53 +00:00
|
|
|
options={'HIDDEN'},
|
|
|
|
maxlen=255, # Max internal buffer length, longer would be clamped.
|
|
|
|
)
|
|
|
|
|
2020-12-05 05:53:59 +00:00
|
|
|
animation_only: BoolProperty(
|
2022-01-28 22:28:12 +00:00
|
|
|
name="Import Animation(s)",
|
|
|
|
description="Import on or more animations from the selected files and append each as a new Action to currently selected Armature.",
|
2020-12-05 05:53:59 +00:00
|
|
|
default=False
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-10-20 18:28:53 +00:00
|
|
|
def execute(self, context):
|
2020-12-11 16:50:03 +00:00
|
|
|
dirname = os.path.dirname(self.filepath)
|
|
|
|
for file in self.files:
|
|
|
|
filepath = os.path.join(dirname, file.name)
|
2021-01-07 05:26:19 +00:00
|
|
|
if filepath.endswith(".zaabin") or filepath.endswith(".zaa"):
|
|
|
|
extract_and_apply_munged_anim(filepath)
|
|
|
|
else:
|
|
|
|
with open(filepath, 'rb') as input_file:
|
|
|
|
scene = read_scene(input_file, self.animation_only)
|
|
|
|
|
2022-01-28 22:28:12 +00:00
|
|
|
if not self.animation_only:
|
|
|
|
extract_scene(filepath, scene)
|
|
|
|
else:
|
|
|
|
extract_and_apply_anim(filepath, scene)
|
2020-12-05 05:53:59 +00:00
|
|
|
|
2020-10-20 18:28:53 +00:00
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
def menu_func_import(self, context):
|
|
|
|
self.layout.operator(ImportMSH.bl_idname, text="SWBF msh (.msh)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-02-09 19:50:52 +00:00
|
|
|
class FillSWBFMaterialProperties(bpy.types.Operator):
|
|
|
|
bl_idname = "swbf_msh.fill_mat_props"
|
|
|
|
bl_label = "Fill SWBF Material Properties"
|
|
|
|
bl_description = ("Fill in SWBF properties of all materials used by selected objects.\n"
|
|
|
|
"Only considers materials that use nodes.\n"
|
2022-02-09 20:16:12 +00:00
|
|
|
"Please see 'Materials > Materials Operators' in the docs for more details.")
|
2022-02-09 19:50:52 +00:00
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
|
|
|
|
slots = sum([list(ob.material_slots) for ob in bpy.context.selected_objects if ob.type == 'MESH'],[])
|
|
|
|
mats = [slot.material for slot in slots if (slot.material and slot.material.node_tree)]
|
|
|
|
|
|
|
|
for mat in mats:
|
|
|
|
try:
|
|
|
|
for BSDF_node in [n for n in mat.node_tree.nodes if n.type == 'BSDF_PRINCIPLED']:
|
|
|
|
base_col = BSDF_node.inputs['Base Color']
|
|
|
|
|
|
|
|
for link in base_col.links :
|
|
|
|
link_node = link.from_node
|
|
|
|
|
|
|
|
if link_node.type != 'TEX_IMAGE':
|
|
|
|
continue
|
|
|
|
|
|
|
|
tex_name = link_node.image.name
|
|
|
|
|
|
|
|
i = tex_name.find(".tga")
|
|
|
|
|
|
|
|
# Get rid of trailing number in case one is present
|
|
|
|
if i > 0:
|
|
|
|
tex_name = tex_name[0:i+4]
|
|
|
|
|
|
|
|
mat.swbf_msh_mat.rendertype = 'NORMAL_BF2'
|
|
|
|
mat.swbf_msh_mat.diffuse_map = tex_name
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
# Many chances for null ref exceptions. None if user reads doc section...
|
|
|
|
pass
|
|
|
|
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
|
|
class VIEW3D_MT_SWBF(bpy.types.Menu):
|
|
|
|
bl_label = "SWBF"
|
|
|
|
|
|
|
|
def draw(self, _context):
|
|
|
|
layout = self.layout
|
|
|
|
layout.operator("swbf_msh.fill_mat_props", text="Fill SWBF Material Properties")
|
|
|
|
|
|
|
|
|
|
|
|
def draw_matfill_menu(self, context):
|
|
|
|
layout = self.layout
|
|
|
|
layout.separator()
|
|
|
|
layout.menu("VIEW3D_MT_SWBF")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
def register():
|
2022-01-18 20:16:49 +00:00
|
|
|
bpy.utils.register_class(CollisionPrimitiveProperties)
|
|
|
|
|
2019-11-15 03:28:09 +00:00
|
|
|
bpy.utils.register_class(MaterialProperties)
|
|
|
|
bpy.utils.register_class(MaterialPropertiesPanel)
|
2020-12-11 21:49:40 +00:00
|
|
|
|
|
|
|
bpy.utils.register_class(SkeletonProperties)
|
|
|
|
bpy.utils.register_class(SkeletonPropertiesPanel)
|
|
|
|
|
2019-11-20 02:17:25 +00:00
|
|
|
bpy.utils.register_class(ExportMSH)
|
2020-10-20 18:28:53 +00:00
|
|
|
bpy.utils.register_class(ImportMSH)
|
2019-11-15 03:28:09 +00:00
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
|
2020-10-20 18:28:53 +00:00
|
|
|
bpy.types.TOPBAR_MT_file_import.append(menu_func_import)
|
2020-12-11 21:49:40 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
bpy.types.Object.swbf_msh_coll_prim = bpy.props.PointerProperty(type=CollisionPrimitiveProperties)
|
|
|
|
bpy.types.Material.swbf_msh_mat = bpy.props.PointerProperty(type=MaterialProperties)
|
2020-12-11 21:49:40 +00:00
|
|
|
bpy.types.Armature.swbf_msh_skel = bpy.props.CollectionProperty(type=SkeletonProperties)
|
2019-11-11 10:03:52 +00:00
|
|
|
|
2022-02-09 19:50:52 +00:00
|
|
|
bpy.utils.register_class(FillSWBFMaterialProperties)
|
|
|
|
bpy.utils.register_class(VIEW3D_MT_SWBF)
|
|
|
|
bpy.types.VIEW3D_MT_object_context_menu.append(draw_matfill_menu)
|
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
def unregister():
|
2022-01-18 20:16:49 +00:00
|
|
|
bpy.utils.unregister_class(CollisionPrimitiveProperties)
|
|
|
|
|
2019-11-15 03:28:09 +00:00
|
|
|
bpy.utils.unregister_class(MaterialProperties)
|
|
|
|
bpy.utils.unregister_class(MaterialPropertiesPanel)
|
2020-12-11 21:49:40 +00:00
|
|
|
|
|
|
|
bpy.utils.unregister_class(SkeletonProperties)
|
|
|
|
bpy.utils.unregister_class(SkeletonPropertiesPanel)
|
|
|
|
|
2019-11-20 02:17:25 +00:00
|
|
|
bpy.utils.unregister_class(ExportMSH)
|
2020-10-20 18:28:53 +00:00
|
|
|
bpy.utils.unregister_class(ImportMSH)
|
2020-12-11 21:49:40 +00:00
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)
|
2020-10-20 18:28:53 +00:00
|
|
|
bpy.types.TOPBAR_MT_file_import.remove(menu_func_import)
|
|
|
|
|
2022-02-09 19:50:52 +00:00
|
|
|
bpy.utils.unregister_class(FillSWBFMaterialProperties)
|
|
|
|
|
|
|
|
bpy.utils.unregister_class(VIEW3D_MT_SWBF)
|
|
|
|
bpy.types.VIEW3D_MT_object_context_menu.remove(draw_matfill_menu)
|
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
register()
|