5
$\begingroup$

Many objects in my scene has Vertex color (left image) that is used in Shader Node (right image).

enter image description here enter image description here

^ I want to change color1 to a more meaningful name e.g. color2 (for the sake of example).

However, renaming it in Shader Node (right image) alone is not enough - I need to rename vertex color for each object too (left image).

How to rename/update-the-name of vertex color name color1 of every object in file to color2?

After I searched, I believe there is no such feature. Can it be done using python?

# 1. get all object (how?)
# 2. check whether each object has vertex color name `color1`
# 3.  ... if so, get integer index of the slot, and rename the slot to `color2`
       perhap it may look like this code :-
bpy.data.objects[some_object_name_string].renameVertexColor(`color1`,`color2`)  ???
$\endgroup$
1
  • $\begingroup$ @brockmann :: sorry for not being clear, I want only "How to change the name of the vertex color group". I can do the later (shader node) by hand. ^^ $\endgroup$ Jun 10, 2021 at 9:34

1 Answer 1

8
$\begingroup$

This should work

import bpy

name_before = "color1"
name_after = "color2"

for mesh in bpy.data.meshes:
    col_layer = mesh.vertex_colors.get(name_before)
    if col_layer is not None:
        col_layer.name = name_after

        # Make sure it renamed; it won't work if there
        # was already another layer named color2
        assert col_layer.name == name_after

If you want to update materials (and node groups) too, add this

node_trees = [mat.node_tree for mat in bpy.data.materials]
node_trees += list(bpy.data.node_groups)

for node_tree in node_trees:
    if node_tree:
        for node in node_tree.nodes:
            if node.type == 'VERTEX_COLOR':
                if node.layer_name == name_before:
                    node.layer_name = name_after
            elif node.type == 'ATTRIBUTE':
                if node.attribute_name == name_before:
                    node.attribute_name = name_after
$\endgroup$
6
  • $\begingroup$ I tested the first half, it works. Thank a lot! $\endgroup$ Jun 10, 2021 at 10:12
  • $\begingroup$ The second half works mostly. However, it has no effect for Vertex Color Node inside Node Group. $\endgroup$ Jun 10, 2021 at 10:16
  • 1
    $\begingroup$ Hah, I thought about doing groups too. I'll update it. $\endgroup$
    – scurest
    Jun 10, 2021 at 10:19
  • $\begingroup$ Now, it works for nodes in node group too. Thank! $\endgroup$ Jun 10, 2021 at 10:26
  • 3
    $\begingroup$ lol. I've contributed a little to some addons so I remember the general shape of the API. Python tooltips and autocomplete in the Python Console work to fill in the details. The API refs are here if you want to look through them though. $\endgroup$
    – scurest
    Jun 10, 2021 at 10:35

You must log in to answer this question.

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