Skip to content

Latest commit

 

History

History
335 lines (273 loc) · 10.9 KB

File metadata and controls

335 lines (273 loc) · 10.9 KB
Error in user YAML: (<unknown>): could not find expected ':' while scanning a simple key at line 3 column 1
---
- oeasy Python 0712
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
本教程同步发布在: 

     个人网站: `https://oeasy.org` 
     蓝桥云课: `https://www.lanqiao.cn/courses/3584` 
     GitHub: `https://github.com/overmind1980/oeasy-python-tutorial` 
     Gitee: `https://gitee.com/overmind1980/oeasypython` 
---

话筒和音符

图片描述

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)

图片描述

import bpy
import math

def create_material(name, color):
    mat = bpy.data.materials.new(name)
    mat.use_nodes = True
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    return mat

def create_cat():
    # ===================== 修改:只删除旧的小猫模型,保留摄影机和灯光 =====================
    for obj in bpy.context.scene.objects:
        # 只删除网格和曲线类型的物体(旧的小猫模型),保留相机和灯光
        if obj.type in ['MESH', 'CURVE']:
            obj.select_set(True)
        else:
            obj.select_set(False)
    bpy.ops.object.delete()

    # 创建身体
    bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, location=(0, 0.5, 1))
    body = bpy.context.active_object
    body.name = "body"
    body.scale = (1, 2, 1)
    body.data.materials.append(create_material('mat_body', (1.0, 1.0, 1.0, 1)))

    # 创建头部
    bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, location=(0, 0, 1.7))
    head = bpy.context.active_object
    head.name = "head"
    head.data.materials.append(create_material('mat_head', (1.0, 1.0, 1.0, 1)))

    # 创建眼睛
    eye_mat = create_material('mat_eye', (0, 0, 0, 1))
    
    bpy.ops.mesh.primitive_uv_sphere_add(radius=0.1, location=(-0.2, -0.4, 0.1))
    left_eye = bpy.context.active_object
    left_eye.name = "left_eye"
    left_eye.data.materials.append(eye_mat)
    left_eye.parent = head

    bpy.ops.mesh.primitive_uv_sphere_add(radius=0.1, location=(0.2, -0.4, 0.1))
    right_eye = bpy.context.active_object
    right_eye.name = "right_eye"
    right_eye.data.materials.append(eye_mat)
    right_eye.parent = head

    # 创建耳朵
    ear_mat = create_material('mat_ear', (1.0, 1.0, 1.0, 1))
    
    bpy.ops.mesh.primitive_cone_add(radius1=0.2, radius2=0.0, depth=0.35, location=(-0.2, 0, 0.45))
    left_ear = bpy.context.active_object
    left_ear.name = "left_ear"
    left_ear.rotation_euler = (0, 0, math.radians(30))
    left_ear.data.materials.append(ear_mat)
    left_ear.parent = head

    bpy.ops.mesh.primitive_cone_add(radius1=0.2, radius2=0.0, depth=0.35, location=(0.2, 0, 0.45))
    right_ear = bpy.context.active_object
    right_ear.name = "right_ear"
    right_ear.rotation_euler = (0, 0, math.radians(-30))
    right_ear.data.materials.append(ear_mat)
    right_ear.parent = head

    # 创建胡须
    beard_length = 0.5
    beard_thickness = 0.02
    beard_mat = create_material('mat_beard', (0, 0, 0, 1))

    for i in range(3):
        bpy.ops.mesh.primitive_cylinder_add(
            radius=beard_thickness, 
            depth=beard_length,
            location=(-0.3, -0.4, -0.3 + i * 0.1)
        )
        left_beard = bpy.context.active_object
        left_beard.name = f"LeftBeard{i + 1}"
        left_beard.rotation_euler = (0, math.radians(85 + i * 5), 0)
        left_beard.parent = head
        left_beard.data.materials.append(beard_mat)

        bpy.ops.mesh.primitive_cylinder_add(
            radius=beard_thickness, 
            depth=beard_length,
            location=(0.3, -0.4, -0.3 + i * 0.1)
        )
        right_beard = bpy.context.active_object
        right_beard.name = f"RightBeard{i + 1}"
        right_beard.rotation_euler = (0, math.radians(95 + i * -5), 0)
        right_beard.parent = head
        right_beard.data.materials.append(beard_mat)

    # 创建腿
    leg_mat = create_material('mat_leg', (1.0, 1.0, 1.0, 1))
    leg_radius = 0.1
    leg_depth = 1

    leg_positions = [
        (-0.3, -0.25, -0.5, "left_front_leg"),
        (0.3, -0.25, -0.5, "right_front_leg"),
        (-0.3, 0.25, -0.5, "left_back_leg"),
        (0.3, 0.25, -0.5, "right_back_leg")
    ]

    for pos in leg_positions:
        bpy.ops.mesh.primitive_cylinder_add(
            radius=leg_radius, 
            depth=leg_depth, 
            location=pos[:3]
        )
        leg = bpy.context.active_object
        leg.name = pos[3]
        leg.data.materials.append(leg_mat)
        leg.parent = body

    # 创建尾巴
    curve = bpy.data.curves.new(name="CatTailCurve", type='CURVE')
    curve.dimensions = '3D'
    spline = curve.splines.new(type='BEZIER')
    spline.bezier_points.add(3)

    tail_points = [
        (0, 1.5, 1),
        (0, 2.1, 1.3),
        (0, 2.4, 1.9),
        (0, 2.8, 2.1)
    ]

    for i, point in enumerate(tail_points):
        spline.bezier_points[i].co = point
        spline.bezier_points[i].handle_left_type = 'AUTO'
        spline.bezier_points[i].handle_right_type = 'AUTO'

    curve_obj = bpy.data.objects.new("tail", curve)
    bpy.context.collection.objects.link(curve_obj)
    curve.bevel_depth = 0.1

    # 创建尾巴材质
    tail_mat = create_material('mat_tail', (1.0, 1.0, 1.0, 1))
    curve_obj.data.materials.append(tail_mat)

    # 创建主容器并设置父级关系
    cat = bpy.data.objects.new("cat", None)
    bpy.data.collections["Collection"].objects.link(cat)
    head.parent = cat
    body.parent = cat
    curve_obj.parent = cat
    return cat

if __name__ == "__main__":
    cat = create_cat()


  • 本文来自 oeasy Python 系统教程。
  • 想完整、扎实学 Python,
  • 搜索 oeasy 即可。