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 = 0.7 # 桌面长度,单位:米 table_width = 0.7 # 桌面宽度,单位:米 table_thickness = 0.05 # 桌面厚度,单位:米 table_name = 'Desktop' 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 = 'TableLeg' # 桌面厚度,单位:米 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)
添加父子级关系
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | 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) 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.8 # 桌面宽度,单位:米 table_thickness = 0.05 # 桌面厚度,单位:米 table_name = 'Desktop' 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 = 'TableLeg' # 桌面厚度,单位:米 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 ) table_leg.parent = table |
分类:
blender
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2023-04-14 python word 转pdf