从混合中的名称中选择要用于修改器的活动对象

2024-05-14 13:39:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用下面给出的脚本,用混合器创建一系列同心球壳。其思想是创建一系列同心球体,然后使用布尔算子差分得到壳。在

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)第1个球。 然而,在我看来

^{pr2}$

不会像我想象的那样工作,因为脚本的结果是一系列同心球体,但是布尔差分仅在sphere4上使用sphere3应用过一次。其他领域什么也没做。此外,sphere4的三个未应用的操作符仍然存在(见截图)。在

所以,问题来了:我做错了什么?如何从“名称”列表中选择活动对象,在该列表中应用布尔修改器,并使用从同一列表中选择的对象?在

提前谢谢你的建议。在

Screenshot


Tags: thenamemodnewdataobjectcolorbool
1条回答
网友
1楼 · 发布于 2024-05-14 13:39:43

在最后一个循环中,您将向ob添加一个布尔修饰符,该修饰符是您在上一个循环中创建的最后一个对象,然后使{}成为应用修改器操作符的活动对象,因为它们不是同一个对象,操作符不会应用您期望的修饰符。请注意,使用mod_bool.name可以克服使用现有名称添加第二个修饰符所带来的任何重命名问题。在

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

我想得到你想要的结果的方法是用-

bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_pattern(pattern='sphere*')
bpy.ops.object.join()

这将把所有球体连接到一个对象中,给你你想要的层。在

相关问题 更多 >

    热门问题