0
$\begingroup$

I am trying to add custom-data layers to mesh vertices so that I may use the values to generate a colorramp on the surface during rendering.

Currently, I first map the data to RGBA values and then add vertex color groups...see code below assuming I have a mesh called surface with 100 vertices:

mesh = surface.data
bm = bmesh.new()  # Create new bmesh object
bm.from_mesh(mesh)  # Init the bmesh with the current mesh
#random colors atm, but would be mapped to RGBA from the values
scalars = {
    'Velocity':  # vertex color layer name 2
        [(random(), random(), random(), 1) for _ in bm.verts],
    'Salinity':  # vertex color layer name 3
        [(random(), random(), random(), 1) for _ in bm.verts],        
    }
layers = {}
for key in scalars:
    layers[key] = bm.loops.layers.color.new(key)
for key, val in layers.items():
    for vert in bm.verts:
        for loop in vert.link_loops:
            loop[val] = scalars[key][vert.index]
bm.to_mesh(mesh)

This allows me to color the surface via the vertex colors using this node setup:

current

What I would like to do is just assign custom-data layers using the actual float values, so instead of mapping to RGBA then coloring the surface, I could pass the data into a colorramp node to generate the colors for me, something like this:

desired

I tried to achieve this by generating some random data to use:

scalars = {'v':np.random.rand(100),
           's':np.random.rand(100)}

and modifying the above line from:

layers[key] = bm.loops.layers.color.new(key)

to:

layers[key] = bm.loops.layers.float.new(key)

which ran without error, but I do not see anything listed in my object data properties nor can I access this data from the attribute node or any other that I can see.

Do these custom-data layers get updated to the bpy mesh? If so, how can I use them in shading nodes?

Honestly a bit lost trying to follow the API docs for this one.

Thanks, Derek

$\endgroup$
6
  • 1
    $\begingroup$ See the update to my answer to your previous question for mor information about that. For your information, any color value can be converted to a float value with a "RGB to BW" node. If the RGB channels of the color are the same, the resulting float value will be the same as this value. So your best bet is to initialize n arrays of size 4 for the 4 channels of each color, then convert it. I don't know of another way to transfer this kind of data to each vertex in the shader editor. $\endgroup$ – Gorgious Apr 19 '20 at 8:48
  • $\begingroup$ thanks..question about this, which I tried. In your example, the RGBA vals are all 0-1 so its fine to take any of the 4 channels and putt them into a colorramp, main shader, etc. In reality my data could be anything, +/- thousands, millions. I tried adding the data into the 1st channel and left the rest 1. I then tried getting 1st channel using sep rgb/xyz node, throwing it into a map range node to normalize it, then putting that into a colorramp node. But it didn't turn out, perhaps I did it wrong but does Blender automatically normalize/edit any values you pass into a vertex color layer? $\endgroup$ – Derek Eden Apr 19 '20 at 8:59
  • $\begingroup$ guess I could probably just normalize my data, do what I just mentioned ^, separate out the data and use it for a colorramp node? might just be confusing myself and doing things wrong in the node editor, but await your advice $\endgroup$ – Derek Eden Apr 19 '20 at 9:06
  • $\begingroup$ The caveat of using vertex colors is that you can only use values between 0 and 1 for each channel, so you will have to normalize the data before mapping to the vertex colors. Values outside of this range will get clamped. I would recommend doing that in the script anyway, and since you seem to know numpy already, you can write very efficient code, at least more efficient than what you would get using map range or color ramp nodes in the node editor $\endgroup$ – Gorgious Apr 19 '20 at 11:19
  • $\begingroup$ Related blender.stackexchange.com/questions/161664/…. Not aware if any range limitations are placed on stored values of u, v. $\endgroup$ – batFINGER Apr 19 '20 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.