import bpy
import math  # 导入 math 模块


#jianmo
def jianMo(l,w,h,name='CafeBody',location_x=0,location_y=0,location_z=0):
    # 定义咖啡馆的尺寸
    cafe_length = l  # 长度,单位:米
    cafe_width = w  # 宽度,单位:米
    cafe_height = h   # 高度,单位:米

    # 创建咖啡馆的主体(立方体)
    bpy.ops.mesh.primitive_cube_add(size=cafe_height, enter_editmode=False, location=(location_x, location_y, location_z))
    cafe_body = bpy.context.selected_objects[0]
    cafe_body.name = name
    
    # 设置咖啡馆主体的尺寸
    cafe_body.dimensions = (cafe_width, cafe_length, cafe_height)
    
    

    # 应用变换到咖啡馆主体
    bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
    
    return cafe_body

    
# 清除当前选中的所有对象
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 set_zhouxiang_is_open(mirror_name,zhouxing_xiaobiao,is_open):
    if is_open:
        bpy.context.object.modifiers["Mirror_Modifier"].use_axis[int(zhouxing_xiaobiao)] = True
    else:
        bpy.context.object.modifiers["Mirror_Modifier"].use_axis[int(zhouxing_xiaobiao)] = False
            
            
#添加镜像修改器到物体
def addMirror(select_obj,
              mirror_name="Mirror_Modifier",
              is_open_zhouxian_x=False,
              is_open_zhouxian_y=True,
              is_open_zhouxian_z=False):
    # 添加镜像修改器到桌腿
    bpy.ops.object.modifier_add(type='MIRROR')
    mirror_modifier = select_obj.modifiers[-1]  # 获取新添加的镜像修改器
    mirror_modifier.name = mirror_name  # 重命名镜像修改器
    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 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)
    
    





if __name__ == '__main__':
    table_length = 1.5  # 桌面长度,单位:米
    table_width = 0.4  # 桌面宽度,单位:米
    table_thickness = 0.05  # 桌面厚度,单位:米
    table_name = 'BenchSeat'
    table_location_x = 0
    table_location_y = 0
    table_location_z = 0
    
    table = jianMo(l=table_length,
    w=table_width ,
    h=table_thickness,
    name=table_name,
    location_x=table_location_x,
    location_y=table_location_y,
    location_z=table_location_z)
    
    #添加倒角
    addDaoJiao(select_obj=table)

    print("daojiao")
    table_leg_length = 0.05  # 桌面长度,单位:米
    table_leg_width = 0.05   # 桌面宽度,单位:米
    table_leg_thickness = 0.75  # 桌面厚度,单位:米
    table_leg_name = 'BenchLeg'  # 桌面厚度,单位:米
    table_leg_location_x=table_width/2-table_leg_width
    table_leg_location_y=-table_length/2+table_leg_length
    table_leg_location_z=-table_leg_thickness/2+table_thickness/2
    
    table_leg=jianMo(l=table_leg_length,
    w=table_leg_width ,
    h=table_leg_thickness,
    name=table_leg_name,
    location_x=table_leg_location_x,
    location_y=table_leg_location_y,
    location_z=table_leg_location_z)
    
    
    #清除当前选中的所有对象
    clear_selected_objects()
    
    #xuan
    selectObjByName(name_str=table_leg_name)
    
    #添加倒角
    addDaoJiao(select_obj=table_leg)
    
    # 添加镜像修改器到桌腿
    addMirror(select_obj=table_leg,
    mirror_name="Mirror_Modifier",
    is_open_zhouxian_x=True,
    is_open_zhouxian_y=True,
    is_open_zhouxian_z=False)

 

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