Skip to content

Commit

Permalink
Refactor Vertex Color export and import
Browse files Browse the repository at this point in the history
  • Loading branch information
sercero committed Mar 21, 2024
1 parent a25db12 commit 511ef96
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
3 changes: 0 additions & 3 deletions io_ogre/ogre/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,3 @@ def gather_metallic_roughness_texture(mat_wrapper):
return None

return ShaderImageTextureWrapper(node_image)



43 changes: 24 additions & 19 deletions io_ogre/ogre/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 8 additions & 12 deletions io_ogre/ogre/ogre_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,24 +1112,20 @@ 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='CORNER', 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

# Bone assignments:
if 'boneIDs' in meshData:
Expand Down

0 comments on commit 511ef96

Please sign in to comment.