19 Commits

Author SHA1 Message Date
3e066bbe64 Updated to use Blender 4.3.2 2025-02-25 17:42:09 -06:00
PrismaticFlower
0cc8beb830 Merge pull request #19 from styinx/#17_animation_frame_export
#17: Convert frame time from float to int
2025-02-23 06:34:34 +00:00
Christoph Zorn
336870ecb5 #17: Convert frame time from float to int 2024-05-04 10:09:17 +02:00
PrismaticFlower
daacdf1a5d handle Face Corner color attributes
fixes #15
2023-12-05 09:49:59 +13:00
PrismaticFlower
48c05380c6 update version number 2023-12-03 12:04:33 +13:00
PrismaticFlower
9f4a205f3d Merge pull request #14 from styinx/#13_bsdf_inputs_specular_key
#13: Add check for blender version on specular key
2023-12-03 12:03:14 +13:00
PrismaticFlower
216ea5d7a4 Merge branch 'master' of https://github.com/PrismaticFlower/SWBF-msh-Blender-IO 2023-12-03 11:58:19 +13:00
Christoph Zorn
d2c7dbd79f #13: Add check for blender version on specular key 2023-12-02 11:58:03 +01:00
PrismaticFlower
6247a289e6 update version number 2023-10-31 08:47:35 +13:00
PrismaticFlower
2b60f0d2a6 Merge pull request #12 from LeovanGit/vertex-color
Fix vertex colors export
2023-10-31 08:45:19 +13:00
LeovanGit
8d5c701b86 fix vertex colors export 2023-10-30 12:46:07 +03:00
PrismaticFlower
c072c9e56d update version number 2023-10-29 11:22:51 +13:00
PrismaticFlower
2421ba70c2 Merge pull request #10 from LeovanGit/vertex-colors
Vertex Colors
2023-10-29 11:16:18 +13:00
PrismaticFlower
ae42cda6ab Skip adding color attributes when unneeded
This is a very small change to skip adding the vertex colours to the Blender mesh if no segment of the geometry being loaded has vertex colours.
2023-10-29 11:13:12 +13:00
LeovanGit
0ac921d855 Add vertex colors to blender + fix unpack_color() 2023-10-27 23:05:05 +03:00
Will
806a7cc060 Merge pull request #8 from maximstewart/master
Add Animation Import(s) to NLA Track List
2023-10-01 08:43:52 -07:00
d1d83d39af Partial Revert: Animation Track patch cleanup 2023-06-02 22:27:58 -05:00
1ec4332576 Animation Track patch cleanup 2023-06-02 21:49:24 -05:00
125ad2792c Fixed import of animations to allow for bulk import 2023-05-28 17:18:50 -05:00
10 changed files with 27 additions and 180 deletions

2
.gitignore vendored
View File

@@ -1,8 +1,6 @@
.DS_Store .DS_Store
*.msh *.msh
*.swp
# Created by https://www.gitignore.io/api/python,visualstudiocode # Created by https://www.gitignore.io/api/python,visualstudiocode
# Edit at https://www.gitignore.io/?templates=python,visualstudiocode # Edit at https://www.gitignore.io/?templates=python,visualstudiocode

View File

@@ -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, PrismaticFlower',
"version": (1, 3, 0), "version": (1, 3, 3),
'blender': (2, 80, 0), 'blender': (4, 3, 2),
'location': 'File > Import-Export', 'location': 'File > Import-Export',
'description': 'Export as SWBF .msh file', 'description': 'Export as SWBF .msh file',
'warning': '', 'warning': '',

View File

@@ -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 = framerange.x + frame * increment frame_time = int(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:

View File

@@ -107,4 +107,4 @@ def extract_and_apply_anim(filename : str, scene : Scene):
arma.animation_data.action = action arma.animation_data.action = action
track = arma.animation_data.nla_tracks.new() track = arma.animation_data.nla_tracks.new()
track.strips.new(action.name, action.frame_range[0], action) track.strips.new(action.name, int(action.frame_range[0]), action)

View File

@@ -180,8 +180,10 @@ 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"].default_value = 0.0 bsdf.inputs[specular_key].default_value = 0.0
material.use_backface_culling = not bool(mat_props.doublesided) material.use_backface_culling = not bool(mat_props.doublesided)

View File

@@ -32,111 +32,7 @@ def validate_segment_geometry(segment : GeometrySegment):
return True return True
def get_shadow_geometry(model: Model): def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str, bpy.types.Material]) -> bpy.types.Object:
for segment in model.geometry:
if segment.shadow_geometry is not None:
return segment.shadow_geometry
return None
# SHDW mesh info is of a different form from
# normal segment geometry
def model_to_shadow_mesh(model: Model, shadow_geometry : ShadowGeometry):
blender_mesh = bpy.data.meshes.new(model.name)
# As is the case with normal geometry processing,
# these will contain flattened lists
vertex_positions = [convert_vector_space(position) for position in shadow_geometry.positions]
# Vertices
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])
def faces_from_half_edges(half_edges : List[Tuple[int,int,int,int]]) -> List[List[int]]:
faces = []
visited_edges = [False] * len(half_edges)
for i in range(len(half_edges)):
if visited_edges[i]:
continue
curr_edge = half_edges[i]
curr_index = curr_edge[0]
starting_index = curr_index
face_length = 0
face_temp = [0] * 5
while True:
if face_length + 1> len(face_temp):
face_temp.append(curr_index)
else:
face_temp[face_length] = curr_index
face_length += 1
curr_edge = half_edges[curr_edge[1]]
curr_index = curr_edge[0]
if (curr_index == starting_index):
break
#print(f"Added a face of length: {face_length}")
faces.append(face_temp[0:face_length])
return faces
polygons = faces_from_half_edges(shadow_geometry.edges)
# LOOPS
flat_indices = [index for polygon in polygons for index in polygon]
blender_mesh.loops.add(len(flat_indices))
# Position indices
blender_mesh.loops.foreach_set("vertex_index", flat_indices)
# POLYGONS/FACES
blender_mesh.polygons.add(len(polygons))
# Indices of starting loop for each polygon
polygon_loop_start_indices = [0] * len(polygons)
current_polygon_start_index = 0
# Number of loops in this polygon. Polygon i will use
# loops from polygon_loop_start_indices[i] to
# polygon_loop_start_indices[i] + polygon_loop_totals[i]
polygon_loop_totals = [0] * len(polygons)
for i,polygon in enumerate(polygons):
polygon_loop_start_indices[i] = current_polygon_start_index
current_polygon_length = len(polygon)
current_polygon_start_index += current_polygon_length
polygon_loop_totals[i] = current_polygon_length
blender_mesh.polygons.foreach_set("loop_start", polygon_loop_start_indices)
blender_mesh.polygons.foreach_set("loop_total", polygon_loop_totals)
blender_mesh.validate(clean_customdata=False)
blender_mesh.update()
#sv_name = model.name if model.name.startswith("sv_") else "sv_" + model.name
blender_mesh_object = bpy.data.objects.new(model.name, blender_mesh)
return blender_mesh_object
def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.types.Material]) -> bpy.types.Object:
blender_mesh = bpy.data.meshes.new(model.name) blender_mesh = bpy.data.meshes.new(model.name)
@@ -232,7 +128,6 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
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
@@ -246,24 +141,25 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
# POLYGONS/FACES # POLYGONS/FACES
blender_mesh.polygons.add(len(polygons)) blender_mesh.polygons.add(len(polygons))
# Indices of starting loop for each polygon # Indices of starting loop for each polygon
polygon_loop_start_indices = [0] * len(polygons) polygon_loop_start_indices = []
current_polygon_start_index = 0 current_polygon_start_index = 0
# Number of loops in this polygon. Polygon i will use # Number of loops in this polygon. Polygon i will use
# loops from polygon_loop_start_indices[i] to # loops from polygon_loop_start_indices[i] to
# polygon_loop_start_indices[i] + polygon_loop_totals[i] # polygon_loop_start_indices[i] + polygon_loop_totals[i]
polygon_loop_totals = [0] * len(polygons) polygon_loop_totals = []
for i,polygon in enumerate(polygons): for polygon in polygons:
polygon_loop_start_indices[i] = current_polygon_start_index polygon_loop_start_indices.append(current_polygon_start_index)
current_polygon_length = len(polygon) current_polygon_length = len(polygon)
current_polygon_start_index += current_polygon_length current_polygon_start_index += current_polygon_length
polygon_loop_totals[i] = current_polygon_length polygon_loop_totals.append(current_polygon_length)
blender_mesh.polygons.foreach_set("loop_start", polygon_loop_start_indices) blender_mesh.polygons.foreach_set("loop_start", polygon_loop_start_indices)
blender_mesh.polygons.foreach_set("loop_total", polygon_loop_totals) blender_mesh.polygons.foreach_set("loop_total", polygon_loop_totals)
@@ -278,7 +174,6 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
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)
@@ -301,17 +196,3 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
return blender_mesh_object return blender_mesh_object
def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str, bpy.types.Material]) -> bpy.types.Object:
shadow_geometry = get_shadow_geometry(model)
if shadow_geometry is not None:
return model_to_shadow_mesh(model, shadow_geometry)
else:
return model_to_mesh(model, scene, materials_map)

View File

@@ -39,20 +39,6 @@ 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. """
@@ -70,7 +56,6 @@ 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:

View File

@@ -215,10 +215,11 @@ 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:
data_type = mesh.color_attributes.active_color.data_type active_color = mesh.color_attributes.active_color
if data_type == "FLOAT_COLOR" or data_type == "BYTE_COLOR": data_index = loop_index if active_color.domain == "CORNER" else vertex_index
for v in mesh.color_attributes.active_color.data[vertex_index].color:
yield v for v in mesh.color_attributes.active_color.data[data_index].color:
yield v
if segment.weights is not None: if segment.weights is not None:
for v in mesh.vertices[vertex_index].groups: for v in mesh.vertices[vertex_index].groups:
@@ -247,9 +248,10 @@ 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:
data_type = mesh.color_attributes.active_color.data_type active_color = mesh.color_attributes.active_color
if data_type == "FLOAT_COLOR" or data_type == "BYTE_COLOR": data_index = loop_index if active_color.domain == "CORNER" else vertex_index
segment.colors.append(list(mesh.color_attributes.active_color.data[vertex_index].color))
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

View File

@@ -388,28 +388,6 @@ 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:

View File

@@ -44,9 +44,10 @@ def extract_models(scene: Scene, materials_map : Dict[str, bpy.types.Material])
new_obj = bpy.data.objects.new(model.name, None) new_obj = bpy.data.objects.new(model.name, None)
new_obj.empty_display_size = 1 new_obj.empty_display_size = 1
new_obj.empty_display_type = 'PLAIN_AXES' new_obj.empty_display_type = 'PLAIN_AXES'
new_obj.name = model.name
model_map[model.name] = new_obj model_map[model.name] = new_obj
new_obj.name = model.name
if model.parent: if model.parent:
new_obj.parent = model_map[model.parent] new_obj.parent = model_map[model.parent]