我正在创建一个球体的多个副本,但是我想更改每个球体的颜色。这是我用来创建初始球体然后对其进行复制的代码。
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)
该脚本可以编译并正常运行,但是球体的颜色不会改变。
答案 0 :(得分:0)
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)