import bpy

# 创建立方体并返回对象
def create_cube(name, dimensions, location, parent=None):
    # 执行添加立方体的操作
    bpy.ops.mesh.primitive_cube_add(size=dimensions[2], enter_editmode=False, location=location)
    # 选择新创建的立方体
    cube_object = bpy.context.selected_objects[0]
    # 设置立方体的名称
    cube_object.name = name
    # 设置立方体的尺寸
    cube_object.dimensions = dimensions
    
    # 应用变换
    bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
    # 如果提供了父对象,则将立方体设置为父对象的子对象
    if parent:
        cube_object.parent = parent
    return cube_object

# 创建圆柱体并返回对象
def create_cylinder(name, radius, height, location, parent=None, rotation_axis=(0, 0, 1), rotation_angle=90):
    # 执行添加圆柱体的操作
    bpy.ops.mesh.primitive_cylinder_add(radius=radius, depth=height, enter_editmode=False, location=location)
    # 选择新创建的圆柱体
    cylinder = bpy.context.selected_objects[0]
    # 设置圆柱体的名称
    cylinder.name = name
    # 如果提供了父对象,则将圆柱体设置为父对象的子对象
    if parent:
        cylinder.parent = parent
    # 旋转圆柱体以改变其方向
    rotation_delta = cylinder.rotation_euler
    rotation_delta[rotation_axis[0]] = rotation_angle * (3.14159 / 180)  # 将角度转换为弧度
    cylinder.rotation_euler = rotation_delta
    
    # 应用变换
    bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
    
    return cylinder

#添加倒角
def addDaoJiao(select_obj):
    
    # 添加倒角修饰符
    bpy.ops.object.modifier_add(type='BEVEL')
    
    bevel_modifier = select_obj.modifiers[-1]  # 获取新添加的 dao jiao修改器
    
    bevel_modifier.name = "Bevel"

    # 设置倒角的宽度和分段数
    bevel_modifier.width = 0.03  # 倒角的宽度
    bevel_modifier.segments = 4  # 倒角的分段数
    

    # 应用倒角修饰符
    bpy.ops.object.modifier_apply(modifier=bevel_modifier.name)


#添加镜像修改器到物体
def addMirror(select_obj,
              mirror_name="Mirror_Modifier",
              axis=(True, False, False)):
    
    # 添加镜像修改器到桌腿
    bpy.ops.object.modifier_add(type='MIRROR')
    mirror_modifier = select_obj.modifiers[-1]  # 获取新添加的镜像修改器
    mirror_modifier.name = mirror_name  # 重命名镜像修改器
    bpy.context.object.modifiers[mirror_name].use_axis[0] = axis[0]
    bpy.context.object.modifiers[mirror_name].use_axis[1] = axis[1]
    bpy.context.object.modifiers[mirror_name].use_axis[2] = axis[2]
    #set_zhouxiang_is_open(mirror_name=mirror_name,zhouxing_xiaobiao=0,is_open=is_open_zhouxian_x)
    #set_zhouxiang_is_open(mirror_name=mirror_name,zhouxing_xiaobiao=1,is_open=is_open_zhouxian_y)
    #set_zhouxiang_is_open(mirror_name=mirror_name,zhouxing_xiaobiao=2,is_open=is_open_zhouxian_z)
    

    # 应用镜像修改器
    #bpy.ops.object.modifier_apply(modifier=mirror_modifier.name, apply_as='DATA')
    
    return mirror_modifier


# 清除当前选中的所有对象
def clear_selected_objects():
    # 清除当前选中的所有对象
    bpy.context.selected_objects.clear()


#选中物体
def selectObjByName(name_str):
    #选中名为"CafeBody"的对象
    for i in bpy.data.objects:
        if i.name == name_str:
            # 找到对象后,执行相应的操作
            print("找到名为 %s 的对象" % name_str)
            i.select_set(state=True)

# 选中并激活门对象
def select_and_activate_door(door_name):
    # 遍历所有对象,找到指定名称的门对象
    for obj in bpy.data.objects:
        if obj.name == door_name:
            # 选择对象
            obj.select_set(True)
            # 激活对象
            bpy.context.view_layer.objects.active = obj
            return obj # 已找到并激活对象,退出函数

# 主函数
def main():
    # 衣柜的整体尺寸
    width = 1.5  # 宽度,单位:米
    depth = 0.65  # 深度,单位:米
    height = 2.2  # 高度,单位:米
    


    # 创建衣柜的基本形状
    wardrobe = create_cube("Wardrobe", (width, depth, height), (0, 0, 0))
    print(width/2)
    
    # 创建衣柜门
    door_width = width/2  # 门宽度,单位:米
    door_depth = 0.1  # 门深度,单位:米
    door_height = height  # 门高度,单位:米
    
    # 创建衣柜的门1
    door1 = create_cube("Door1", (door_width, door_depth, door_height), (-(width/2)/2, -depth/2, 0),wardrobe)
    
    # 创建中间横挡板
    drawer1 = create_cube("Drawer1", (width, depth, 0.1), (0,0,0),wardrobe)
    
    # 门把手的尺寸
    knob_radius = 0.03  # 半径,单位:米
    knob_height = 0.3  # 高度,单位:米
    
    knob_heng_radius = 0.02  # 半径,单位:米
    knob_heng_height = 0.3  # 高度,单位:米
    
    # 在门上添加门把手
    knob_location = (-knob_radius, -depth/2-door_depth/2-knob_heng_height/2, 0)  # 门把手的位置
    
    knob1 = create_cylinder("DoorKnob1",knob_radius, knob_height, knob_location, door1,rotation_axis=(0, 0, 1), rotation_angle=0)
    
    knob1_shang_location = (-knob_radius, -depth/2-door_depth/2, knob_height/4)  # 门把手的位置
    knob1_xia_location = (-knob_radius, -depth/2-door_depth/2, -knob_height/4)  # 门把手的位置
    
    knob1_shang = create_cylinder("DoorKnob1_shang",knob_heng_radius, knob_heng_height, knob1_shang_location, knob1,rotation_axis=(0, 0, 1), rotation_angle=90)
    knob1_xia = create_cylinder("DoorKnob1_xia",knob_heng_radius, knob_heng_height, knob1_xia_location, knob1,rotation_axis=(0, 0, 1), rotation_angle=90)
    
    # 创建衣柜的门2
    door2 = create_cube("Door2", (door_width, door_depth, door_height), ((width/2)/2, -depth/2, 0),wardrobe)
    
    # 在门上添加门把手
    knob2_location = (knob_radius, -depth/2-door_depth/2-knob_heng_height/2, 0)  # 门把手的位置
    
    knob2 = create_cylinder("DoorKnob1",knob_radius, knob_height, knob2_location, door2,rotation_axis=(0, 0, 1), rotation_angle=0)
    
    knob2_shang_location = (knob_radius, -depth/2-door_depth/2, knob_height/4)  # 门把手的位置
    knob2_xia_location = (knob_radius, -depth/2-door_depth/2, -knob_height/4)  # 门把手的位置
    
    knob2_shang = create_cylinder("DoorKnob2_shang",knob_heng_radius, knob_heng_height, knob2_shang_location, knob1,rotation_axis=(0, 0, 1), rotation_angle=90)
    knob2_xia = create_cylinder("DoorKnob2_xia",knob_heng_radius, knob_heng_height, knob2_xia_location, knob1,rotation_axis=(0, 0, 1), rotation_angle=90)
    

    
    #select_and_activate_door(door_name="Door1")
    
    #addMirror(select_obj=door1,
    #          mirror_name="Mirror_Modifier",
    #          axis=(True,False,False))
    
    


    
    


if __name__ == '__main__':
    main()

 

posted on 2024-04-14 20:00  大话人生  阅读(58)  评论(0编辑  收藏  举报