0
$\begingroup$

enter image description herepremise

The code below is intended to duplicate an object and change the vertex color of the duplicated object. Select the object called "ic" and run it. Ultimately, I want to change the color to a gradient while continuously generating dozens of objects, so this code is I extracted only the problematic elements.

problem

I found that it works fine in Blender 3.5, but in Blender 4.1 there is a problem where the color of the original object changes. Is this a specification change in 4.0 or later? If so, how can I fix it so it will work properly in 4.1?

import colorsys
import bpy
import random

def duplicate_move_and_rename(obj, x, y, z):
    bpy.ops.object.duplicate_move(TRANSFORM_OT_translate={"value":(x, y, z)})
    return
    

obj = bpy.context.active_object

# -------------------------------------------------
duplicate_move_and_rename(obj, -2, 0, 0)
obj2 = bpy.context.active_object;
vc2 = obj2.data.vertex_colors[0].data
hue = 1.0
print(hue)
saturation = 1.0
value = 1.0
for polygon2 in obj2.data.polygons:
    for i, v in zip(polygon2.loop_indices, polygon2.vertices):
        vc2[i].color = colorsys.hsv_to_rgb(hue, saturation, value)+(1,)
$\endgroup$
2
  • $\begingroup$ Hello. Vertex colors have been moved to a generic attribute on the mesh so it may be because of that. $\endgroup$
    – Gorgious
    Commented May 13 at 8:59
  • $\begingroup$ @Gorgious Oh, thank you. I don't know exactly what's going on, but I'll look into them and try to fix them. $\endgroup$
    – shimiken
    Commented May 13 at 9:51

1 Answer 1

0
$\begingroup$

Since ChatGPT4 was newly released for free, I asked if it was possible to adapt to the specification changes in Blender version 4.0, and received the following response. This resolved the issue. Thank you. As Gorgious pointed out, the cause was the new specification changes in Attributes.

import colorsys
import bpy
import random

def duplicate_move_and_rename(obj, x, y, z):
    bpy.ops.object.duplicate_move(TRANSFORM_OT_translate={"value":(x, y, z)})
    return bpy.context.active_object

def change_object_color(obj, hue, saturation, value):
    # 頂点カラー属性を取得または作成
    if 'Col' in obj.data.color_attributes:
        color_attr = obj.data.color_attributes['Col']
    else:
        color_attr = obj.data.color_attributes.new(name='Col', type='FLOAT_COLOR', domain='CORNER')
    
    # 頂点カラーを設定
    for i, loop in enumerate(obj.data.loops):
        color = colorsys.hsv_to_rgb(hue, saturation, value) + (1,)
        color_attr.data[i].color = color

# アクティブなオブジェクトを取得
obj = bpy.context.active_object

# オブジェクトを複製して色を変更
obj2 = duplicate_move_and_rename(obj, -2, 0, 0)

# 色を変更する
hue = random.random()
saturation = 1.0
value = 1.0
change_object_color(obj2, hue, saturation, value)
```
$\endgroup$

You must log in to answer this question.

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