-
-
Save pgtwitter/a6799524f467a17cbe3a7f3e16c99efc to your computer and use it in GitHub Desktop.
Grayscale Using Fieldler Vector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %% | |
import bpy | |
from sklearn.cluster import KMeans # /path/to/blender's/python -m pip install scikit-learn | |
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)] | |
G = S[0] | |
coordinates = [mesh.vertices[i].co for i in G.nodes] | |
fiedler_vector = nx.fiedler_vector(G) | |
pred = KMeans(n_clusters=11).fit_predict([(v, x, y, z) for v, (x, y, z) in zip(fiedler_vector, coordinates)]) | |
colors = dict(zip(G.nodes, pred)) | |
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 G.nodes: | |
ratio = colors[v] * 0.1 | |
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