4
$\begingroup$

I was reading this post and this one :

Other than copy new data to specific "known" vertex attribute like "Color" for example.

There is way to read custom vertex attributes in the Attribute node \ shader network
shader script ?

$\endgroup$
3
  • $\begingroup$ ATM, you can read any attribute defined / redefined within Geometry Nodes.. would that do? $\endgroup$ Jul 28, 2021 at 5:48
  • $\begingroup$ @RobinBetts thank you for the reply, I`m going to use same vertex weights for shape key and shader mix as part of automated process. can I incorporate and control the geometry nodes graph using python ? It might help... Wrote this question while ago: blender.stackexchange.com/questions/231304/… $\endgroup$
    – barakooda
    Jul 28, 2021 at 6:05
  • $\begingroup$ Well, the shader can read vertex weights via GeoNodes, (that would be in the modifier/shader combo, a fixed pipeline). If you need the plumbing to be automated via Python, I'm not sure whether the API reaches GeoNodes yet. Maybe hold out for a Pythonista. $\endgroup$ Jul 28, 2021 at 6:12

1 Answer 1

2
$\begingroup$

This code example and related image demonstrate creation of new vertex attributes and using them in the shader network.

  • I was not able to create string based attribute
  • Its important to use cycles to easily see the results when using vertex attribute values higher than 1.0

enter image description here

import bpy
from bpy import data as data, context as context
from mathutils import Vector
import bmesh
import numpy as np

#clean objects in scene
for object in data.objects :
    if object.type == 'MESH':
        data.objects.remove(object, do_unlink=True)

bpy.ops.mesh.primitive_cube_add()
obj_a = data.objects["Cube"]
obj_a.name = "A"

bpy.ops.mesh.primitive_cube_add()
obj_b = data.objects["Cube"]
obj_b.name = "B"
obj_b.hide_viewport = True

a_mesh = obj_a.data
bm = bmesh.new()
bm.from_mesh(a_mesh)


# Create a new vertex attributes
# color: 0-1 value 4 element float (it will clamp if > 1)
# float_color: non normalzied 4 elemnet vector
# float: single float
# vector_float: non normalized 3 element vector
# string :was not able to create string vertex attribute

knafeh_color = bm.loops.layers.color.new("knafeh_color")
malabi_color_float = bm.loops.layers.float_color.new("malabi_color_float")
falafel_float = bm.loops.layers.float.new("falafel_float")
salad_vector_float = bm.loops.layers.float_vector.new("salad_vector_float")
baklava_str = bm.loops.layers.string.new("baklava_str")


bm_faces = bm.faces
for bm_face in bm_faces:
    face_loops = bm_face.loops
    for bm_loop in face_loops:

        # Assign color to vertex.
        bm_loop[knafeh_color] = [0.9,0.5,0,1]
        bm_loop[malabi_color_float] = [7,0.1,9999,1]
        bm_loop[salad_vector_float] = [1,1,0]
        bm_loop[falafel_float] = 0.1
        

        
bm.to_mesh(a_mesh)
bm.free()
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.