Skip to content

Instantly share code, notes, and snippets.

@pgtwitter
Created June 2, 2024 04:43
Show Gist options
  • Save pgtwitter/0e6d3d8483b842c7360c425bfadba6bb to your computer and use it in GitHub Desktop.
Save pgtwitter/0e6d3d8483b842c7360c425bfadba6bb to your computer and use it in GitHub Desktop.
Grayscale Using Fieldler Vector
# %%
import bpy
import networkx as nx # /path/to/blender's/python -m pip install scipy networkx
obj = bpy.data.objects['Armadillo']
mesh = obj.data
edges = [(e.vertices[0], e.vertices[1]) for e in mesh.edges]
G = nx.Graph(edges)
S = [G.subgraph(c).copy() for c in nx.connected_components(G)]
fiedler_vector = nx.fiedler_vector(S[0])
minV = min(fiedler_vector)
maxV = max(fiedler_vector)
node2fiedler_value = dict(zip(S[0].nodes, fiedler_vector))
name = 'GrayscaleUsingFieldlerVector'
if name not in mesh.vertex_colors:
mesh.vertex_colors.new(name=name)
vc = mesh.vertex_colors[name].data
for polygon in mesh.polygons:
for i, v in zip(polygon.loop_indices, polygon.vertices):
if v in node2fiedler_value:
ratio = (node2fiedler_value[v]-minV) / (maxV-minV)
vc[i].color = [ratio, ratio, ratio, 1]
else:
vc[i].color = [0, 0, 0, 0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment