Created
December 30, 2022 19:16
-
-
Save semagnum/e0983929c2ebf093b4b7193035f23421 to your computer and use it in GitHub Desktop.
From painted vertex color attributes, create an RGB flowmap that shows the "flow" of colors
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 | |
import bmesh | |
from mathutils import Vector | |
obj = bpy.context.active_object | |
me = obj.data | |
# cannot access color attributes within bmesh, | |
# so we will create a dict of indices to access the linked edges in object mode | |
bpy.ops.object.mode_set(mode='EDIT') | |
bm = bmesh.from_edit_mesh(me) | |
index_to_linked_verts = {v.index: [e.other_vert(v).index for e in v.link_edges] for v in bm.verts} | |
bm.free() | |
bpy.ops.object.mode_set(mode='OBJECT') | |
color_attr = me.color_attributes[''] | |
target_attr = me.color_attributes['Flow'] | |
for v in me.vertices: | |
v_col = Vector(color_attr.data[v.index].color) | |
v_pos = v.co | |
flow_vec = Vector() | |
for adj_ind in index_to_linked_verts[v.index]: | |
adj = me.vertices[adj_ind] | |
adj_col = Vector(color_attr.data[adj_ind].color) | |
adj_pos = adj.co | |
col_len = (adj_col - v_col).length | |
if col_len <= 0.001: | |
flow_vec += (adj_pos - v_pos) | |
else: | |
flow_vec += ((adj_pos - v_pos) / (adj_col - v_col).length) | |
flow_vec.normalize() | |
target_attr.data[v.index].color = (flow_vec[0], flow_vec[1], flow_vec[2], 1.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment