From 0ac921d8555436013ecc1e79a416b79e9e7cdeab Mon Sep 17 00:00:00 2001 From: LeovanGit Date: Fri, 27 Oct 2023 23:05:05 +0300 Subject: [PATCH 1/2] Add vertex colors to blender + fix unpack_color() --- addons/io_scene_swbf_msh/msh_mesh_to_blend.py | 10 +++++++++- addons/io_scene_swbf_msh/msh_utilities.py | 11 ++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/addons/io_scene_swbf_msh/msh_mesh_to_blend.py b/addons/io_scene_swbf_msh/msh_mesh_to_blend.py index 8f04253..8610b5e 100644 --- a/addons/io_scene_swbf_msh/msh_mesh_to_blend.py +++ b/addons/io_scene_swbf_msh/msh_mesh_to_blend.py @@ -40,6 +40,7 @@ def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str, vertex_positions = [] vertex_uvs = [] vertex_normals = [] + vertex_colors = [] # Keeps track of which vertices each group of weights affects # i.e. maps offset of vertices -> weights that affect them @@ -76,6 +77,11 @@ def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str, if segment.normals: vertex_normals += [tuple(convert_vector_space(n)) for n in segment.normals] + if segment.colors: + vertex_colors.extend(segment.colors) + else: + [vertex_colors.extend([0.0, 0.0, 0.0, 1.0]) for _ in range(len(segment.positions))] + if segment.weights: vertex_weights_offsets[polygon_index_offset] = segment.weights @@ -111,7 +117,6 @@ def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str, 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]) - # LOOPS flat_indices = [index for polygon in polygons for index in polygon] @@ -129,6 +134,9 @@ def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str, 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]]) + # Colors + blender_mesh.color_attributes.new("COLOR0", "FLOAT_COLOR", "POINT") + blender_mesh.color_attributes[0].data.foreach_set("color", vertex_colors) # POLYGONS/FACES diff --git a/addons/io_scene_swbf_msh/msh_utilities.py b/addons/io_scene_swbf_msh/msh_utilities.py index 6a35578..5dd00f0 100644 --- a/addons/io_scene_swbf_msh/msh_utilities.py +++ b/addons/io_scene_swbf_msh/msh_utilities.py @@ -39,12 +39,9 @@ def pack_color(color) -> int: return packed def unpack_color(color: int) -> List[float]: - - mask = int(0x000000ff) - - r = (color & (mask << 16)) / 255.0 - g = (color & (mask << 8)) / 255.0 - b = (color & mask) / 255.0 - a = (color & (mask << 24)) / 255.0 + r = (color >> 16 & 0xFF) / 255.0 + g = (color >> 8 & 0xFF) / 255.0 + b = (color >> 0 & 0xFF) / 255.0 + a = (color >> 24 & 0xFF) / 255.0 return [r,g,b,a] From ae42cda6aba997b4d1d7a4295dcdbfbe07872950 Mon Sep 17 00:00:00 2001 From: PrismaticFlower <5582558+PrismaticFlower@users.noreply.github.com> Date: Sun, 29 Oct 2023 11:13:12 +1300 Subject: [PATCH 2/2] 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. --- addons/io_scene_swbf_msh/msh_mesh_to_blend.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/addons/io_scene_swbf_msh/msh_mesh_to_blend.py b/addons/io_scene_swbf_msh/msh_mesh_to_blend.py index 8610b5e..08e2bfc 100644 --- a/addons/io_scene_swbf_msh/msh_mesh_to_blend.py +++ b/addons/io_scene_swbf_msh/msh_mesh_to_blend.py @@ -59,6 +59,7 @@ def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str, if model.geometry: + geometry_has_colors = any(segment.colors for segment in model.geometry) for segment in model.geometry: @@ -79,7 +80,7 @@ def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str, if segment.colors: vertex_colors.extend(segment.colors) - else: + elif geometry_has_colors: [vertex_colors.extend([0.0, 0.0, 0.0, 1.0]) for _ in range(len(segment.positions))] if segment.weights: @@ -135,8 +136,9 @@ def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str, blender_mesh.uv_layers[0].data.foreach_set("uv", [component for i in flat_indices for component in vertex_uvs[i]]) # Colors - blender_mesh.color_attributes.new("COLOR0", "FLOAT_COLOR", "POINT") - blender_mesh.color_attributes[0].data.foreach_set("color", vertex_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