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 20, 2024
1 parent a25db12 commit 3e3d9ee
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 39 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
27 changes: 10 additions & 17 deletions io_ogre/ogre/ogre_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 3e3d9ee

Please sign in to comment.