From 3e3d9eeb79ce9ec5d20dffd33504a513b79bb841 Mon Sep 17 00:00:00 2001 From: sercero Date: Wed, 20 Mar 2024 00:14:13 -0300 Subject: [PATCH] Refactor Vertex Color export and import --- io_ogre/ogre/material.py | 3 --- io_ogre/ogre/mesh.py | 43 +++++++++++++++++++++---------------- io_ogre/ogre/ogre_import.py | 27 +++++++++-------------- 3 files changed, 34 insertions(+), 39 deletions(-) diff --git a/io_ogre/ogre/material.py b/io_ogre/ogre/material.py index 5d52b7e..b3d5587 100644 --- a/io_ogre/ogre/material.py +++ b/io_ogre/ogre/material.py @@ -840,6 +840,3 @@ def gather_metallic_roughness_texture(mat_wrapper): return None return ShaderImageTextureWrapper(node_image) - - - diff --git a/io_ogre/ogre/mesh.py b/io_ogre/ogre/mesh.py index 6ee61f3..0f53472 100644 --- a/io_ogre/ogre/mesh.py +++ b/io_ogre/ogre/mesh.py @@ -29,44 +29,49 @@ class VertexColorLookup: def __init__(self, mesh): - self.mesh = mesh - self.__colors = None - self.__alphas = None - color_names = ["col", "color"] - alpha_names = ["a", "alpha"] + color_names = ["col", "color", "attribute"] + + vertex_colors = None + + # In Blender version 3.2, vertex colors have been refactored into generic color attributes + # https://developer.blender.org/docs/release_notes/3.2/sculpt/ + if (bpy.app.version[0] >= 3 and bpy.app.version[1] >= 2) or bpy.app.version[0] > 3: + print("Blender version >= 3.2") + vertex_colors = mesh.color_attributes + else: + print("Blender version < 3.2") + vertex_colors = mesh.vertex_colors - if len(self.mesh.vertex_colors) > 0: - for key, colors in self.mesh.vertex_colors.items(): + if len(vertex_colors) > 0: + for key, colors in vertex_colors.items(): if (self.__colors is None) and (key.lower() in color_names): self.__colors = colors - if (self.__alphas is None) and (key.lower() in alpha_names): - self.__alphas = colors - if self.__colors is None and self.__alphas is None: - # No alpha and color found by name, assume that the only - # vertex color data is actual color data - self.__colors = colors + + if ((bpy.app.version[0] >= 3 and bpy.app.version[1] >= 2) or bpy.app.version[0] > 3): + if colors.domain != 'CORNER': + Report.warnings.append( 'Mesh "%s" with color attribute "%s" has wrong color domain: "%s" (should be: "CORNER")' % \ + (mesh.name, key, colors.domain) ) + if colors.data_type != 'BYTE_COLOR': + Report.warnings.append( 'Mesh "%s" with color attribute "%s" has wrong color data type: "%s" (should be: "BYTE_COLOR")' % \ + (mesh.name, key, colors.data_type) ) if self.__colors: self.__colors = [x.color for x in self.__colors.data] - if self.__alphas: - self.__alphas = [x.color for x in self.__alphas.data] + #self.__colors = [x.color_srgb for x in self.__colors.data] @property def has_color_data(self): - return self.__colors is not None or self.__alphas is not None + return self.__colors is not None def get(self, item): if self.__colors: color = self.__colors[item] else: color = [1.0] * 4 - if self.__alphas: - color[3] = mathutils.Vector(self.__alphas[item]).length return color - def dot_mesh(ob, path, force_name=None, ignore_shape_animation=False, normals=True, tangents=4, isLOD=False, **kwargs): """ export the vertices of an object into a .mesh file diff --git a/io_ogre/ogre/ogre_import.py b/io_ogre/ogre/ogre_import.py index 630786b..7b5a2a2 100644 --- a/io_ogre/ogre/ogre_import.py +++ b/io_ogre/ogre/ogre_import.py @@ -1112,24 +1112,17 @@ def bCreateSubMeshes(meshData, meshName): # Vertex colors if 'vertexcolors' in geometry: - colourData = me.vertex_colors.new(name='Colour'+str(j)).data + colourData = None + + if (bpy.app.version[0] >= 3 and bpy.app.version[1] >= 2) or bpy.app.version[0] > 3: + print("Blender version >= 3.2") + colourData = me.color_attributes.new(name='Colour', domain='POINT', type='BYTE_COLOR').data + else: + colourData = me.vertex_colors.new(name='Colour').data + vcolors = geometry['vertexcolors'] - loopIndex = 0 - for face in faces: - for v in face: - colourData[loopIndex].color = vcolors[v] - loopIndex += 1 - - # Vertex Alpha - for c in vcolors: - if c[3] != 1.0: - alphaData = me.vertex_colors.new(name='Alpha'+str(j)).data - loopIndex = 0 - for face in faces: - for v in face: - colourData[loopIndex].color[3] = vcolors[v][3] - loopIndex += 1 - break + for v in range(len(vcolors)): + colourData[v].color = vcolors[v] # Bone assignments: if 'boneIDs' in meshData: