我正在创建一个球体的多个副本,但我想更改每个球体的颜色。这是我用来创建初始球体并复制它的代码
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.object
和bpy.context.active_object
是同一个对象。您复制的是对象,而不是具有材质的对象数据,这意味着您将每个新材质附加到相同的对象数据中,但第一种材质是唯一使用的材质。
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)