Skip to content

Instantly share code, notes, and snippets.

@aobond2
Last active September 12, 2023 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aobond2/84653516d9756beec246dfda864bc276 to your computer and use it in GitHub Desktop.
Save aobond2/84653516d9756beec246dfda864bc276 to your computer and use it in GitHub Desktop.
Vertex colour aware mesh optimizing
import bpy
BUpperLimit = 0.7
BLowerLimit = 0.3
GLimit = 0.3
vertexGroupName = "vcolorVG"
decimateRatio = 0.8
def objectSelection():
# TODO: Change this based on needs. Get all meshes in hierarchy
obj = bpy.context.active_object
return obj
def loopingVertices():
vertexArray = []
obj = objectSelection()
meshData = obj.data
deselectAllVertices()
haveVC = False
deleteExistingVertexGroup(obj)
makeVertexGroup(obj)
# Get G and B color between defined bounds, AO and curvature
for i, polygon in enumerate(meshData.polygons):
for i1, loopIndex in enumerate(polygon.loop_indices):
try:
colorData = meshData.vertex_colors.active.data[loopIndex]
if colorData.color[2] < BLowerLimit \
or colorData.color[2] > BUpperLimit \
or colorData.color[1] < GLimit:
meshData.vertices[meshData.polygons[i].vertices[i1]].select = True
vertexArray.append(meshData.vertices[meshData.polygons[i].vertices[i1]])
haveVC = True
except:
haveVC = False
if haveVC:
addToVertexGroup(vertexArray, obj)
decimateMesh(obj)
def decimateMesh(inputObj):
decimateExist = False
for modifier in inputObj.modifiers:
if modifier and modifier.type == 'DECIMATE':
decimateExist = True
else:
decimateExist = False
if (decimateExist == False):
bpy.ops.object.modifier_add(type='DECIMATE')
bpy.context.object.modifiers["Decimate"].ratio = decimateRatio
bpy.context.object.modifiers["Decimate"].vertex_group = vertexGroupName
else:
bpy.context.object.modifiers["Decimate"].ratio = decimateRatio
bpy.context.object.modifiers["Decimate"].vertex_group = vertexGroupName
def expandSelection(inputObj):
# TODO: THere could be better way to handle this, maybe soft selection
bpy.ops.mesh.select_less()
def makeVertexGroup(inputObj):
if vertexGroupName not in inputObj.vertex_groups:
bpy.ops.object.vertex_group_add()
vertexGroup = inputObj.vertex_groups.active
vertexGroup.name = vertexGroupName
def addToVertexGroup(vertexArray, inputObj):
invertSelection(inputObj)
vg = inputObj.vertex_groups[vertexGroupName]
selectedVerts = [v.index for v in inputObj.data.vertices if v.select]
for vertex in selectedVerts:
vg.add([vertex], 1.0, 'ADD')
def deleteExistingVertexGroup(inputObj):
if inputObj.vertex_groups:
for vg in inputObj.vertex_groups:
inputObj.vertex_groups.remove(vg)
def invertSelection(inputObj):
bpy.context.view_layer.objects.active = inputObj
bpy.ops.object.mode_set(mode='OBJECT')
for vertex in inputObj.data.vertices:
vertex.select = not vertex.select
bpy.ops.object.mode_set(mode='EDIT')
expandSelection(inputObj)
bpy.ops.object.mode_set(mode='OBJECT')
def deselectAllVertices():
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='OBJECT')
loopingVertices()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment