设置复制的原始 uv 球体的颜色

Set color of copied primitive uv spheres

我正在创建一个球体的多个副本,但我想更改每个球体的颜色。这是我用来创建初始球体然后复制它的代码。

bpy.ops.object.select_all(action='DESELECT')
bpy.ops.mesh.primitive_uv_sphere_add(size=radius)
sphere = bpy.context.object

def makeSphere(x,y,z,r,g,b):
    ob = sphere.copy()
    ob.location.x = x
    ob.location.y = y
    ob.location.z = z

    # Attempt to change sphere's color
    activeObject = bpy.context.active_object 
    mat = bpy.data.materials.new(name="MaterialName")
    activeObject.data.materials.append(mat) 
    bpy.context.object.active_material.diffuse_color = (r/255,g/255,b/255) 

    bpy.context.scene.objects.link(ob)

脚本编译并运行良好,但球体的颜色没有改变。

有几点,bpy.context.objectbpy.context.active_object 是同一个对象。您正在复制对象,而不是具有 material 的对象数据,这意味着您将每个新的 material 附加到相同的对象数据,但第一个 material 是唯一的正在使用。

bpy.ops.object.select_all(action='DESELECT')
bpy.ops.mesh.primitive_uv_sphere_add(size=.3)
sphere = bpy.context.object

def makeSphere(x,y,z,r,g,b):
    ob = sphere.copy()
    ob.data = sphere.data.copy()
    ob.location.x = x
    ob.location.y = y
    ob.location.z = z

    # Attempt to change sphere's color
    mat = bpy.data.materials.new(name="MaterialName")
    mat.diffuse_color = (r/255,g/255,b/255)
    ob.active_material = mat

    bpy.context.scene.objects.link(ob)