import bpy
from math import pi
# 清除所有现有对象
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
Lars = bpy.data.objects.new("Lars", None)
bpy.data.collections["Collection"].objects.link(Lars)
# 创建话筒的主体
def create_conical_cylinder(radius_top, radius_bottom, height, vertices):
bpy.ops.mesh.primitive_cone_add(vertices=vertices, radius1=radius_top, radius2=radius_bottom, depth=height, location=(0, 0, height / 2))
bpy.context.object.parent = Lars
bpy.context.object.name = "body"
# 设置参数
radius_top = 0.27
radius_bottom = 0.35
height = 2.5
vertices = 32
# 创建圆柱
create_conical_cylinder(radius_top, radius_bottom, height, vertices)
bpy.ops.mesh.primitive_cube_add(size=0.1, location=(0.3, 0, 1.4))
screen = bpy.context.object
screen.scale = (0.5, 3, 5)
bpy.context.object.parent = Lars
bpy.context.object.name = "screen"
mat = bpy.data.materials.new('screen')
color = color = (0, 0, 0, 0.8)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
# 创建话筒的网罩
bpy.ops.mesh.primitive_uv_sphere_add(segments=32, ring_count=16, radius=0.6, location=(0, 0, 2.8))
bpy.context.object.parent = Lars
bpy.context.object.name = "mesh"
bpy.ops.mesh.primitive_torus_add(align='WORLD', location=(0, 0, 2.8), major_radius=0.6, minor_radius=0.07)
bpy.context.object.parent = Lars
bpy.context.object.name = "ring"
Lars.rotation_euler[0] = - pi / 6
def create_note(location):
# 创建音符符头(一个球体)
bpy.ops.mesh.primitive_uv_sphere_add(segments=32, ring_count=16, radius=0.3, location=(0, 0, 0))
note_head = bpy.context.object
bpy.ops.transform.resize(value=(1.0, 0.9, 1.0))
bpy.ops.object.transform_apply(scale=True)
bpy.context.object.parent = Lars
# 获取音符符头的引用
note_head = bpy.context.object
# 创建音符符杆(一个细长的立方体)
bpy.ops.mesh.primitive_cube_add(size=0.05, location=(0, 0, 0.6))
bpy.context.object.scale[2] = 20
bpy.context.object.parent = Lars
# 获取音符符杆的引用
note_stem = bpy.context.object
# 合并音符符头和符杆
bpy.ops.object.select_all(action='DESELECT')
note_head.select_set(True)
note_stem.select_set(True)
bpy.context.view_layer.objects.active = note_head
bpy.ops.object.join()
# 创建音符符尾(使用曲线)
bpy.ops.curve.primitive_bezier_curve_add(radius=0.1, location=(0, 0, 1.1))
bpy.context.object.parent = Lars
# 获取音符符尾的引用
note_flag = bpy.context.object
# 调整音符符尾的形状
bpy.context.object.data.bevel_depth = 0.02
bpy.context.object.data.bevel_resolution = 4
bpy.context.object.data.splines[0].bezier_points[0].co = (0, 0, 0)
bpy.context.object.data.splines[0].bezier_points[0].handle_right = (0.2, 0.2, 0.5)
bpy.context.object.data.splines[0].bezier_points[1].co = (0.5, 0.5, 0.2)
bpy.context.object.data.splines[0].bezier_points[1].handle_left = (0.3, 0.3, 0.4)
# 将音符符头、符杆和符尾合并为一个对象
bpy.ops.object.convert(target='MESH')
bpy.ops.object.select_all(action='DESELECT')
note_head.select_set(True)
note_flag.select_set(True)
bpy.context.view_layer.objects.active = note_head
bpy.ops.object.join()
# 重命名对象为 "音符"
bpy.context.object.name = "音符"
# 将音符对象放置在麦克风上方
bpy.context.object.location = (location[0], location[1], location[2] + 0.3)
# 创建第1个音符
create_note((0, 1, 4))
bpy.context.object.rotation_euler[0] = pi /6
mat = bpy.data.materials.new('mat_not1')
color = color = (1, 1, 0.3, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
# 创建第2个音符
create_note((0.5, 2.2, 5))
bpy.context.object.rotation_euler[0] = pi /5
mat = bpy.data.materials.new('mat_not2')
color = color = (0.8, 0.4, 1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
# 创建第3个音符
create_note((0.5, 3.4, 6))
bpy.context.object.rotation_euler[0] = pi /8
mat = bpy.data.materials.new('mat_not3')
color = color = (1, 0.5, 0.6, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_plane_add(size=20)
bpy.context.object.location = (0,0,-3)
camera = bpy.data.cameras.new('Camera')
camera_obj = bpy.data.objects.new('Camera', camera)
bpy.data.collections["Collection"].objects.link(camera_obj)
camera.lens = 25 # Focal length in millimeters
camera.sensor_width = 36 # Sensor width in millimeters
camera.sensor_height = 24 # Sensor height in millimeters
camera_obj.location = (13.6, 5, 10.5) # X, Y, Z coordinates
camera_obj.rotation_euler = (-2.233,3.14,-1.047)
bpy.context.scene.camera = camera_obj
bpy.ops.object.light_add(type='SPOT', radius=1)
bpy.context.object.data.energy = 2000
bpy.context.object.location = (6.27,-3.4,6)
bpy.context.object.rotation_euler = (1.172,0,0.907)
bpy.context.scene.render.resolution_x = 640
bpy.context.scene.render.resolution_y = 480
bpy.context.scene.render.resolution_percentage = 50
# Set the render engine (e.g., CYCLES, BLENDER_EEVEE)
bpy.context.scene.render.engine = 'CYCLES'
# Set the output file path
bpy.context.scene.render.filepath = '/tmp/render2.png'
# Render the current view
bpy.ops.render.render(write_still=True)