0
$\begingroup$

my goal is to create an operator script that will add the specified color to the vertex color of the selected object based on distance (and lambert using the normals facing). this will essentially paint with a simulated point light. ideally id like to do this in vertex paint mode and just grab the current paint color. i have to figure out the falloff logic. its acting kinda wonky.

here is the script (blender 3.5):

import bpy
import mathutils

newcol = mathutils.Color([0.0, 1.0, 0.2])

# Get the active object and ensure it's a mesh
obj = bpy.context.active_object
if obj.type != 'MESH':
    raise ValueError("The active object is not a mesh")

# Get the mesh data
mesh = obj.data

# Get the 3D cursor location
cursor_location = bpy.context.scene.cursor.location

# Get the vertex color data
colattr = obj.data.color_attributes[0]

for v_index in range(len(mesh.vertices)):
    # Calculate the distance between the vertex and the cursor
    distance = (mesh.vertices[v_index].co - cursor_location).length
    
    # Apply quadratic falloff
    falloff = 1 - (distance / 2) ** 2  # Adjust the divisor for desired falloff range
    
    # Multiply the float color values by the falloff and add it to the current color
    colattr.data[v_index].color[0] += newcol.r
    colattr.data[v_index].color[1] += newcol.g
    colattr.data[v_index].color[2] += newcol.b

# Update the mesh
mesh.update()

right now its assigning color, just not correctly falling off

$\endgroup$
5
  • $\begingroup$ Ok so it turns out my vertex color channel is in the face corner domain. I just need to figure out how to iterate through faces to do this properly $\endgroup$ Commented May 10, 2023 at 22:34
  • $\begingroup$ You can either store it into the vertex domain or face corner domain. $\endgroup$
    – lemon
    Commented May 11, 2023 at 7:17
  • $\begingroup$ Yes, turns out it this code only works with Vertex Domain color channels. $\endgroup$ Commented May 11, 2023 at 17:02
  • $\begingroup$ You can change the domain in the UI. $\endgroup$
    – lemon
    Commented May 12, 2023 at 6:07
  • $\begingroup$ again, the original code works. it is the final code. it was giving me issues due to the domain of the vertex color channel. if you use face corner domain, you would need different code. that i do not know. $\endgroup$ Commented May 12, 2023 at 23:05

1 Answer 1

0
$\begingroup$

@robin-betts marked my previous answer as a comment. this is incorrect. the answer to my issue was indeed that the vertex color channel needs to be in the vertex domain. that is why the falloff logic was not working. Once you add a vertex domain channel, this code works.

$\endgroup$
2
  • 2
    $\begingroup$ To be useful to others, you may post your final code so other people may benefit from it $\endgroup$
    – Gorgious
    Commented May 12, 2023 at 20:57
  • $\begingroup$ where's the code $\endgroup$
    – Unknow0059
    Commented Jan 12 at 8:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .