从Blender

时间:2017-08-19 19:34:13

标签: python blender

我正在尝试使用下面给出的脚本用Blender创建一系列同心球壳。这个想法是创建一系列的同心球体,然后使用布尔运算符差异来获得贝壳。

import bpy


mat0 = bpy.data.materials.new("hole")
mat0.emit=0.5
mat0.diffuse_color = 0.5,0.5,0.5


mat1 = bpy.data.materials.new("EO")
mat1.emit=0.5
mat1.diffuse_color = 0.0,0.0,0.8

mat2 = bpy.data.materials.new("ALK")
mat2.emit=0.5
mat2.diffuse_color = 0.5,0.5,0.0

mat3 = bpy.data.materials.new("CH")
mat3.emit=0.5
mat3.diffuse_color = 0.5,0.0,0.0

radii = [1, 1.1, 1.3, 1.4, 1.7]
mat = [mat0, mat1, mat2, mat1, mat3]
name = ["sphere0", "sphere1", "sphere2", "sphere3", "sphere4"]


for i in range(0,len(radii)): #Loop to create all the spheres, which will make up the shells of the sphere
  bpy.ops.mesh.primitive_uv_sphere_add(segments=64,ring_count=32,size=radii[i],location=(0.0, 0.0, 0.0), rotation=(0, 0, 0))
  ob = bpy.context.object
  me = ob.data
  me.materials.append(mat[i])
  ob.name = name[i]


for i in range(len(radii)-1,0,-1): #Transforms the spheres into shell, by the     boolean operator difference between the larger shell and the smaller one

  # Create a boolean modifier named 'my_bool_mod' for the sphere.
  mod_bool = ob.modifiers.new('my_bool_mod', 'BOOLEAN')
  # Set the mode of the modifier to DIFFERENCE.
  mod_bool.operation = 'DIFFERENCE'
  # Set the object to be used by the modifier.

  mod_bool.object =  bpy.data.objects[name[i-1]] 

  # Set the name[i] as the active object.
  bpy.context.scene.objects.active = bpy.data.objects[name[i]]

  # Apply the modifier.
  res = bpy.ops.object.modifier_apply(modifier = 'my_bool_mod')

因此,脚本可以细分为以下部分:

在第一部分中,我定义了外壳的材料及其尺寸。

在第二部分中,我创建了从最小到最大的球体,但序列并不重要。

直到这里,脚本完美无缺。

在第三部分中,我想在最大的球体和稍微更小的球体之间做出区别,即,我从第i个球体中减去第(i-1)个球体。 但是,在我看来,这条线

      bpy.context.scene.objects.active = bpy.data.objects[name[i]]

不能像我想的那样工作,因为脚本的结果是一系列同心球体,但布尔差异仅在sphere4上使用sphere3应用一次。在其他领域没有做任何事情。此外,球体4的三个非应用运算符仍在那里(见截图)。

所以,问题出现了:我做错了什么?如何从应用了布尔修饰符的名称列表中选择活动对象,并从同一列表中选择对象?

提前感谢任何建议。

Screenshot

1 个答案:

答案 0 :(得分:0)

在最后一个循环中,您将向ob添加一个布尔修饰符,这是您在上一个循环中创建的最后一个对象,然后使bpy.data.objects[name[i]]成为将使用apply修饰符运算符的活动对象因为它们不是同一个对象,所以操作符不会应用您期望的修饰符。值得注意的是,使用mod_bool.name将通过添加具有现有名称的第二个修饰符来克服任何重命名问题。

但是,修复那个循环不会给你你想要的结果。当您添加第二个修改器时,它将布置与新球体的差异,该球体小于您刚从中间切出的球体,并且不会与现有的壳体相交。

我认为获得所需结果的方法是用 -

替换最后一个循环
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_pattern(pattern='sphere*')
bpy.ops.object.join()

这会将所有球体连接到一个对象中,为您提供所追求的图层。