Compare commits
1 Commits
master
...
63f9e43e17
Author | SHA1 | Date | |
---|---|---|---|
![]() |
63f9e43e17 |
@@ -1,8 +1,8 @@
|
|||||||
bl_info = {
|
bl_info = {
|
||||||
'name': 'SWBF .msh Import-Export',
|
'name': 'SWBF .msh Import-Export',
|
||||||
'author': 'Will Snyder, PrismaticFlower',
|
'author': 'Will Snyder, SleepKiller',
|
||||||
"version": (1, 3, 3),
|
"version": (1, 2, 1),
|
||||||
'blender': (4, 3, 2),
|
'blender': (2, 80, 0),
|
||||||
'location': 'File > Import-Export',
|
'location': 'File > Import-Export',
|
||||||
'description': 'Export as SWBF .msh file',
|
'description': 'Export as SWBF .msh file',
|
||||||
'warning': '',
|
'warning': '',
|
||||||
@@ -162,14 +162,14 @@ def menu_func_export(self, context):
|
|||||||
|
|
||||||
|
|
||||||
class ImportMSH(Operator, ImportHelper):
|
class ImportMSH(Operator, ImportHelper):
|
||||||
""" Import SWBF .msh file(s). """
|
""" Import an SWBF .msh file. """
|
||||||
|
|
||||||
bl_idname = "swbf_msh.import"
|
bl_idname = "swbf_msh.import"
|
||||||
bl_label = "Import SWBF .msh File(s)"
|
bl_label = "Import SWBF .msh File(s)"
|
||||||
filename_ext = ".msh"
|
filename_ext = ".msh"
|
||||||
|
|
||||||
files: CollectionProperty(
|
files: CollectionProperty(
|
||||||
name="File Path(s)",
|
name="File Path",
|
||||||
type=bpy.types.OperatorFileListElement,
|
type=bpy.types.OperatorFileListElement,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -181,7 +181,7 @@ class ImportMSH(Operator, ImportHelper):
|
|||||||
|
|
||||||
animation_only: BoolProperty(
|
animation_only: BoolProperty(
|
||||||
name="Import Animation(s)",
|
name="Import Animation(s)",
|
||||||
description="Import one or more animations from the selected files and append each as a new Action to currently selected Armature.",
|
description="Import on or more animations from the selected files and append each as a new Action to currently selected Armature.",
|
||||||
default=False
|
default=False
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@@ -67,7 +67,7 @@ def extract_anim(armature: bpy.types.Armature, root_name: str) -> Animation:
|
|||||||
|
|
||||||
for frame in range(num_frames):
|
for frame in range(num_frames):
|
||||||
|
|
||||||
frame_time = int(framerange.x + frame * increment)
|
frame_time = framerange.x + frame * increment
|
||||||
bpy.context.scene.frame_set(frame_time)
|
bpy.context.scene.frame_set(frame_time)
|
||||||
|
|
||||||
for keyable_bone in keyable_bones:
|
for keyable_bone in keyable_bones:
|
||||||
|
@@ -32,6 +32,7 @@ def extract_and_apply_anim(filename : str, scene : Scene):
|
|||||||
|
|
||||||
if scene.animation is None:
|
if scene.animation is None:
|
||||||
raise Exception("No animation found in msh file!")
|
raise Exception("No animation found in msh file!")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
head, tail = os.path.split(filename)
|
head, tail = os.path.split(filename)
|
||||||
anim_name = tail.split(".")[0]
|
anim_name = tail.split(".")[0]
|
||||||
@@ -39,10 +40,6 @@ def extract_and_apply_anim(filename : str, scene : Scene):
|
|||||||
if anim_name in bpy.data.actions:
|
if anim_name in bpy.data.actions:
|
||||||
bpy.data.actions.remove(bpy.data.actions[anim_name], do_unlink=True)
|
bpy.data.actions.remove(bpy.data.actions[anim_name], do_unlink=True)
|
||||||
|
|
||||||
for nt in arma.animation_data.nla_tracks:
|
|
||||||
if anim_name == nt.strips[0].name:
|
|
||||||
arma.animation_data.nla_tracks.remove(nt)
|
|
||||||
|
|
||||||
action = bpy.data.actions.new(anim_name)
|
action = bpy.data.actions.new(anim_name)
|
||||||
action.use_fake_user = True
|
action.use_fake_user = True
|
||||||
|
|
||||||
@@ -106,5 +103,4 @@ def extract_and_apply_anim(filename : str, scene : Scene):
|
|||||||
fcurve_loc_z.keyframe_points.insert(i,t.z)
|
fcurve_loc_z.keyframe_points.insert(i,t.z)
|
||||||
|
|
||||||
arma.animation_data.action = action
|
arma.animation_data.action = action
|
||||||
track = arma.animation_data.nla_tracks.new()
|
|
||||||
track.strips.new(action.name, int(action.frame_range[0]), action)
|
|
||||||
|
@@ -180,10 +180,8 @@ to provide an exact emulation"""
|
|||||||
|
|
||||||
texture_input_nodes.append(texImage)
|
texture_input_nodes.append(texImage)
|
||||||
|
|
||||||
specular_key = "Specular" if bpy.app.version < (4, 0, 0) else "Specular IOR Level"
|
|
||||||
|
|
||||||
bsdf.inputs["Roughness"].default_value = 1.0
|
bsdf.inputs["Roughness"].default_value = 1.0
|
||||||
bsdf.inputs[specular_key].default_value = 0.0
|
bsdf.inputs["Specular"].default_value = 0.0
|
||||||
|
|
||||||
material.use_backface_culling = not bool(mat_props.doublesided)
|
material.use_backface_culling = not bool(mat_props.doublesided)
|
||||||
|
|
||||||
|
@@ -40,7 +40,6 @@ def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str,
|
|||||||
vertex_positions = []
|
vertex_positions = []
|
||||||
vertex_uvs = []
|
vertex_uvs = []
|
||||||
vertex_normals = []
|
vertex_normals = []
|
||||||
vertex_colors = []
|
|
||||||
|
|
||||||
# Keeps track of which vertices each group of weights affects
|
# Keeps track of which vertices each group of weights affects
|
||||||
# i.e. maps offset of vertices -> weights that affect them
|
# i.e. maps offset of vertices -> weights that affect them
|
||||||
@@ -59,7 +58,6 @@ def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str,
|
|||||||
|
|
||||||
|
|
||||||
if model.geometry:
|
if model.geometry:
|
||||||
geometry_has_colors = any(segment.colors for segment in model.geometry)
|
|
||||||
|
|
||||||
for segment in model.geometry:
|
for segment in model.geometry:
|
||||||
|
|
||||||
@@ -78,11 +76,6 @@ def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str,
|
|||||||
if segment.normals:
|
if segment.normals:
|
||||||
vertex_normals += [tuple(convert_vector_space(n)) for n in segment.normals]
|
vertex_normals += [tuple(convert_vector_space(n)) for n in segment.normals]
|
||||||
|
|
||||||
if segment.colors:
|
|
||||||
vertex_colors.extend(segment.colors)
|
|
||||||
elif geometry_has_colors:
|
|
||||||
[vertex_colors.extend([0.0, 0.0, 0.0, 1.0]) for _ in range(len(segment.positions))]
|
|
||||||
|
|
||||||
if segment.weights:
|
if segment.weights:
|
||||||
vertex_weights_offsets[polygon_index_offset] = segment.weights
|
vertex_weights_offsets[polygon_index_offset] = segment.weights
|
||||||
|
|
||||||
@@ -118,6 +111,7 @@ def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str,
|
|||||||
blender_mesh.vertices.add(len(vertex_positions))
|
blender_mesh.vertices.add(len(vertex_positions))
|
||||||
blender_mesh.vertices.foreach_set("co", [component for vertex_position in vertex_positions for component in vertex_position])
|
blender_mesh.vertices.foreach_set("co", [component for vertex_position in vertex_positions for component in vertex_position])
|
||||||
|
|
||||||
|
|
||||||
# LOOPS
|
# LOOPS
|
||||||
|
|
||||||
flat_indices = [index for polygon in polygons for index in polygon]
|
flat_indices = [index for polygon in polygons for index in polygon]
|
||||||
@@ -128,16 +122,13 @@ def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str,
|
|||||||
blender_mesh.loops.foreach_set("vertex_index", flat_indices)
|
blender_mesh.loops.foreach_set("vertex_index", flat_indices)
|
||||||
|
|
||||||
# Normals
|
# Normals
|
||||||
|
blender_mesh.create_normals_split()
|
||||||
blender_mesh.loops.foreach_set("normal", [component for i in flat_indices for component in vertex_normals[i]])
|
blender_mesh.loops.foreach_set("normal", [component for i in flat_indices for component in vertex_normals[i]])
|
||||||
|
|
||||||
# UVs
|
# UVs
|
||||||
blender_mesh.uv_layers.new(do_init=False)
|
blender_mesh.uv_layers.new(do_init=False)
|
||||||
blender_mesh.uv_layers[0].data.foreach_set("uv", [component for i in flat_indices for component in vertex_uvs[i]])
|
blender_mesh.uv_layers[0].data.foreach_set("uv", [component for i in flat_indices for component in vertex_uvs[i]])
|
||||||
|
|
||||||
# Colors
|
|
||||||
if geometry_has_colors:
|
|
||||||
blender_mesh.color_attributes.new("COLOR0", "FLOAT_COLOR", "POINT")
|
|
||||||
blender_mesh.color_attributes[0].data.foreach_set("color", vertex_colors)
|
|
||||||
|
|
||||||
|
|
||||||
# POLYGONS/FACES
|
# POLYGONS/FACES
|
||||||
@@ -174,6 +165,7 @@ def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str,
|
|||||||
reset_normals = [0.0] * (len(blender_mesh.loops) * 3)
|
reset_normals = [0.0] * (len(blender_mesh.loops) * 3)
|
||||||
blender_mesh.loops.foreach_get("normal", reset_normals)
|
blender_mesh.loops.foreach_get("normal", reset_normals)
|
||||||
blender_mesh.normals_split_custom_set(tuple(zip(*(iter(reset_normals),) * 3)))
|
blender_mesh.normals_split_custom_set(tuple(zip(*(iter(reset_normals),) * 3)))
|
||||||
|
blender_mesh.use_auto_smooth = True
|
||||||
|
|
||||||
|
|
||||||
blender_mesh_object = bpy.data.objects.new(model.name, blender_mesh)
|
blender_mesh_object = bpy.data.objects.new(model.name, blender_mesh)
|
||||||
@@ -196,3 +188,4 @@ def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str,
|
|||||||
|
|
||||||
|
|
||||||
return blender_mesh_object
|
return blender_mesh_object
|
||||||
|
|
||||||
|
@@ -39,6 +39,20 @@ class VertexWeight:
|
|||||||
weight: float = 1.0
|
weight: float = 1.0
|
||||||
bone: int = 0
|
bone: int = 0
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ShadowGeometry:
|
||||||
|
""" Class representing 'SHDW' chunks. """
|
||||||
|
|
||||||
|
# Perhaps I could just use the positions list in the segment
|
||||||
|
# class, but I don't know if SHDW info can coexist with
|
||||||
|
# a normal geometry segment...
|
||||||
|
positions: List[Vector] = field(default_factory=list)
|
||||||
|
|
||||||
|
# The second two entries may not be necessary...
|
||||||
|
edges: List[Tuple[int,int,int,int]] = field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class GeometrySegment:
|
class GeometrySegment:
|
||||||
""" Class representing a 'SEGM' section in a .msh file. """
|
""" Class representing a 'SEGM' section in a .msh file. """
|
||||||
@@ -56,6 +70,7 @@ class GeometrySegment:
|
|||||||
triangles: List[List[int]] = field(default_factory=list)
|
triangles: List[List[int]] = field(default_factory=list)
|
||||||
triangle_strips: List[List[int]] = None
|
triangle_strips: List[List[int]] = None
|
||||||
|
|
||||||
|
shadow_geometry: ShadowGeometry = None
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class CollisionPrimitive:
|
class CollisionPrimitive:
|
||||||
|
@@ -179,7 +179,7 @@ def create_mesh_geometry(mesh: bpy.types.Mesh, valid_vgroup_indices: Set[int]) -
|
|||||||
vertex_remap: List[Dict[Tuple[int, int], int]] = [dict() for i in range(material_count)]
|
vertex_remap: List[Dict[Tuple[int, int], int]] = [dict() for i in range(material_count)]
|
||||||
polygons: List[Set[int]] = [set() for i in range(material_count)]
|
polygons: List[Set[int]] = [set() for i in range(material_count)]
|
||||||
|
|
||||||
if mesh.color_attributes.active_color is not None:
|
if mesh.vertex_colors.active is not None:
|
||||||
for segment in segments:
|
for segment in segments:
|
||||||
segment.colors = []
|
segment.colors = []
|
||||||
|
|
||||||
@@ -215,10 +215,7 @@ def create_mesh_geometry(mesh: bpy.types.Mesh, valid_vgroup_indices: Set[int]) -
|
|||||||
yield mesh.uv_layers.active.data[loop_index].uv.y
|
yield mesh.uv_layers.active.data[loop_index].uv.y
|
||||||
|
|
||||||
if segment.colors is not None:
|
if segment.colors is not None:
|
||||||
active_color = mesh.color_attributes.active_color
|
for v in mesh.vertex_colors.active.data[loop_index].color:
|
||||||
data_index = loop_index if active_color.domain == "CORNER" else vertex_index
|
|
||||||
|
|
||||||
for v in mesh.color_attributes.active_color.data[data_index].color:
|
|
||||||
yield v
|
yield v
|
||||||
|
|
||||||
if segment.weights is not None:
|
if segment.weights is not None:
|
||||||
@@ -248,10 +245,7 @@ def create_mesh_geometry(mesh: bpy.types.Mesh, valid_vgroup_indices: Set[int]) -
|
|||||||
segment.texcoords.append(mesh.uv_layers.active.data[loop_index].uv.copy())
|
segment.texcoords.append(mesh.uv_layers.active.data[loop_index].uv.copy())
|
||||||
|
|
||||||
if segment.colors is not None:
|
if segment.colors is not None:
|
||||||
active_color = mesh.color_attributes.active_color
|
segment.colors.append(list(mesh.vertex_colors.active.data[loop_index].color))
|
||||||
data_index = loop_index if active_color.domain == "CORNER" else vertex_index
|
|
||||||
|
|
||||||
segment.colors.append(list(active_color.data[data_index].color))
|
|
||||||
|
|
||||||
if segment.weights is not None:
|
if segment.weights is not None:
|
||||||
groups = mesh.vertices[vertex_index].groups
|
groups = mesh.vertices[vertex_index].groups
|
||||||
|
@@ -388,6 +388,28 @@ def _read_segm(segm: Reader, materials_list: List[Material]) -> GeometrySegment:
|
|||||||
#if segm.read_u16 != 0:
|
#if segm.read_u16 != 0:
|
||||||
# segm.skip_bytes(-2)
|
# segm.skip_bytes(-2)
|
||||||
|
|
||||||
|
elif next_header == "SHDW":
|
||||||
|
|
||||||
|
shadow_geometry = ShadowGeometry()
|
||||||
|
|
||||||
|
with segm.read_child() as shdw:
|
||||||
|
#print("Found shadow chunk")
|
||||||
|
|
||||||
|
num_positions = shdw.read_u32()
|
||||||
|
#print(f" Num verts in shadow mesh: {num_positions}")
|
||||||
|
shadow_geometry.positions = [shdw.read_vec() for _ in range(num_positions)]
|
||||||
|
|
||||||
|
num_edges = shdw.read_u32()
|
||||||
|
#print(f" Num edges in shadow mesh: {num_edges}")
|
||||||
|
edges = []
|
||||||
|
for i in range(num_edges):
|
||||||
|
edges.append(tuple(shdw.read_u16(4)))
|
||||||
|
#print(" " + str(edges[-1]))
|
||||||
|
shadow_geometry.edges = edges
|
||||||
|
|
||||||
|
geometry_seg.shadow_geometry = shadow_geometry
|
||||||
|
|
||||||
|
|
||||||
elif next_header == "WGHT":
|
elif next_header == "WGHT":
|
||||||
with segm.read_child() as wght:
|
with segm.read_child() as wght:
|
||||||
|
|
||||||
|
@@ -39,9 +39,12 @@ def pack_color(color) -> int:
|
|||||||
return packed
|
return packed
|
||||||
|
|
||||||
def unpack_color(color: int) -> List[float]:
|
def unpack_color(color: int) -> List[float]:
|
||||||
r = (color >> 16 & 0xFF) / 255.0
|
|
||||||
g = (color >> 8 & 0xFF) / 255.0
|
mask = int(0x000000ff)
|
||||||
b = (color >> 0 & 0xFF) / 255.0
|
|
||||||
a = (color >> 24 & 0xFF) / 255.0
|
r = (color & (mask << 16)) / 255.0
|
||||||
|
g = (color & (mask << 8)) / 255.0
|
||||||
|
b = (color & mask) / 255.0
|
||||||
|
a = (color & (mask << 24)) / 255.0
|
||||||
|
|
||||||
return [r,g,b,a]
|
return [r,g,b,a]
|
||||||
|
Reference in New Issue
Block a user