import bpy
import math
import mathutils
def create_monitor():
# 删除所有现有对象
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
# 创建显示器屏幕
bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, align='WORLD', location=(0, 0, 1))
screen = bpy.context.object
screen.scale[0] = 1.8 # 宽度
screen.scale[1] = 0.1 # 厚度
screen.scale[2] = 1.2 # 高度
screen.name = 'Screen'
# 添加屏幕材质
mat_screen = bpy.data.materials.new('mat_screen')
mat_screen.diffuse_color = (0, 0, 0, 1) # 纯黑色
screen.data.materials.append(mat_screen)
# 创建显示器支架
bpy.ops.mesh.primitive_cylinder_add(radius=0.1, depth=2, enter_editmode=False, align='WORLD', location=(0, -0.2, 0))
stand = bpy.context.object
stand.location[2] = -0.35 # 支架位置调整
stand.name = 'Stand'
# 添加支架材质
mat_stand = bpy.data.materials.new('mat_stand')
mat_stand.diffuse_color = (1, 1, 1, 1) # 白色
stand.data.materials.append(mat_stand)
# 创建显示器底座
bpy.ops.mesh.primitive_cylinder_add(radius=0.5, depth=0.1, enter_editmode=False, align='WORLD', location=(0, 0, 0))
base = bpy.context.object
base.location[2] = stand.location[2] - stand.dimensions[2] / 2 - 0.05 # 底座与支架下端相连
base.name = 'Base'
# 添加底座材质
mat_base = bpy.data.materials.new('mat_base')
mat_base.diffuse_color = (1, 1, 1, 1) # 白色
base.data.materials.append(mat_base)
# 创建显示器后部结构
bpy.ops.mesh.primitive_cube_add(size=1, enter_editmode=False, align='WORLD', location=(0, 0, 0))
back_structure = bpy.context.object
back_structure.scale[0] = 0.4 # 宽度
back_structure.scale[1] = 0.15 # 厚度
back_structure.scale[2] = 0.5 # 高度
back_structure.name = 'BackStructure'
back_structure.location[2] = 0.9
back_structure.location[1] = -0.2 # 紧贴显示器
back_structure.location[0] = 0 # 对齐支架
stand.location[0] = back_structure.location[0] # 支架与后部结构对齐
# 添加后部结构材质
mat_back_structure = bpy.data.materials.new('mat_back_structure')
mat_back_structure.diffuse_color = (0.2, 0.2, 0.2, 1) # 灰色
back_structure.data.materials.append(mat_back_structure)
# 创建按钮
bpy.ops.mesh.primitive_cylinder_add(radius=0.05, depth=0.02, enter_editmode=False, align='WORLD', location=(0, 0.35, base.location[2] + 0.05))
button = bpy.context.object
button.name = 'Button'
# 添加按钮材质
mat_button = bpy.data.materials.new('mat_button')
mat_button.diffuse_color = (1, 0, 0, 1) # 红色
button.data.materials.append(mat_button)
# 创建点光源
bpy.ops.object.light_add(type='POINT', align='WORLD', location=(3, 3, 3))
light = bpy.context.object
light.data.energy = 1000 # 调整光源亮度
# 创建摄像机
bpy.ops.object.camera_add(align='WORLD', location=(0, 15, 0))
camera = bpy.context.object
camera.rotation_euler[0] = math.radians(90) # 旋转摄像机使其面向模型
camera.rotation_euler[1] = 0
camera.rotation_euler[2] = math.radians(180)
# 动画关键帧
num_frames = 100
radius = 15
bpy.context.scene.frame_end = num_frames
for frame in range(num_frames + 1):
angle = frame * (2 * math.pi / num_frames)
camera.location.x = radius * math.cos(angle)
camera.location.y = radius * math.sin(angle)
camera.location.z = 0
# 使摄像机始终对准原点
direction = camera.location - mathutils.Vector((0, 0, 0))
rot_quat = direction.to_track_quat('Z', 'Y')
camera.rotation_euler = rot_quat.to_euler()
camera.keyframe_insert(data_path="location", frame=frame)
camera.keyframe_insert(data_path="rotation_euler", frame=frame)
# 创建墙角背景
# 创建地板
bpy.ops.mesh.primitive_plane_add(size=40, enter_editmode=False, align='WORLD', location=(0, 0, -1.5))
floor = bpy.context.object
floor.name = 'Floor'
# 添加地板材质
mat_floor = bpy.data.materials.new('mat_floor')
mat_floor.diffuse_color = (1, 1, 1, 1) # 白色
floor.data.materials.append(mat_floor)
# 创建墙壁
bpy.ops.mesh.primitive_plane_add(size=40, enter_editmode=False, align='WORLD', location=(0, 20, 15))
wall = bpy.context.object
wall.rotation_euler[0] = math.radians(90) # 旋转使其垂直于地板
wall.name = 'Wall'
# 添加墙壁材质
mat_wall = bpy.data.materials.new('mat_wall')
mat_wall.diffuse_color = (1, 1, 1, 1) # 白色
wall.data.materials.append(mat_wall)
create_monitor()