Error in user YAML: (<unknown>): could not find expected ':' while scanning a simple key at line 3 column 1
---
- oeasy Python 0725
- 这是 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
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete()
def create_niao():
niao = bpy.data.objects.new("niao", None)
bpy.data.collections["Collection"].objects.link(niao)
bpy.ops.mesh.primitive_uv_sphere_add()
head_obj = bpy.context.object
head_obj.name = "head"
head_obj.location = (0.7, 0, 4.5)
mat = bpy.data.materials.new('mat_tou')
mat.diffuse_color = (1, 1, 1, 1)
head_obj.data.materials.append(mat)
bpy.context.object.parent = niao
bpy.context.scene.frame_start = 1
bpy.context.scene.frame_end = 10
head_obj.location = (0.7, 0, 4.5)
head_obj.rotation_euler = (0,0,0)
head_obj.keyframe_insert(data_path="location", index=-1, frame=1)
head_obj.keyframe_insert(data_path="rotation_euler", index=-1, frame=1)
head_obj.location = (1.5, 0, 3)
head_obj.rotation_euler = (0,1,0)
head_obj.keyframe_insert(data_path="location", index=-1, frame=5)
head_obj.keyframe_insert(data_path="rotation_euler", index=-1, frame=5)
head_obj.location = (0.7, 0, 4.5)
head_obj.rotation_euler = (0,0,0)
head_obj.keyframe_insert(data_path="location", index=-1, frame=10)
head_obj.keyframe_insert(data_path="rotation_euler", index=-1, frame=10)
bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name ="body"
bpy.context.object.parent = niao
bpy.data.objects["body"].location = (-1,0,2.5)
bpy.data.objects["body"].rotation_euler = (0,0,0)
bpy.data.objects["body"].scale = (2,2,2)
mat = bpy.data.materials.new('mat_body')
color = (1, 1, 1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = niao
bpy.ops.mesh.primitive_cone_add()
zui_obj = bpy.context.object
zui_obj.name = "zui"
zui_obj.location = (1.2,0,0)
zui_obj.rotation_euler = (0,1.5,0)
zui_obj.scale = (0.6,0.7,0.7)
mat = bpy.data.materials.new('mat_zui')
color = (0.5, 0.3, 0.2, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
zui_obj.parent = head_obj
bpy.ops.mesh.primitive_uv_sphere_add()
toushi1_obj = bpy.context.object
toushi1_obj.name = "toushi1"
toushi1_obj.location = (-0,0,1.3)
toushi1_obj.scale = (0.5,0.5,0.5)
mat = bpy.data.materials.new('mat_toushi1')
color = (0.3, 0.1, 0.1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
toushi1_obj.parent = head_obj
bpy.ops.mesh.primitive_uv_sphere_add()
toushi2_obj = bpy.context.object
toushi2_obj.name = "toushi2"
toushi2_obj.location = (-0.8,0,1)
toushi2_obj.scale = (0.5,0.5,0.5)
mat = bpy.data.materials.new('mat_toushi2')
color = (0.3, 0.1, 0.1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
toushi2_obj.parent = head_obj
bpy.ops.mesh.primitive_uv_sphere_add()
toushi3_obj = bpy.context.object
toushi3_obj.name = "toushi3"
toushi3_obj.location = (0.7,0,-1)
toushi3_obj.scale = (0.5,0.5,0.5)
mat = bpy.data.materials.new('mat_toushi3')
color = (0.3, 0.1, 0.1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
toushi3_obj.parent = head_obj
bpy.ops.mesh.primitive_uv_sphere_add()
eye_l_obj = bpy.context.object
eye_l_obj.name = "eye_l"
eye_l_obj.location = (0.8,0.5,0.4)
eye_l_obj.scale = (0.5,0.5,0.5)
bpy.data.objects["eye_l"].scale = (0.3,0.3,0.3)
mat = bpy.data.materials.new('mat_eye_l')
color = (0, 0, 0, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
eye_l_obj.parent = head_obj
bpy.ops.mesh.primitive_uv_sphere_add()
eye_r_obj = bpy.context.object
eye_r_obj.name = "eye_r"
eye_r_obj.location = (0.8,-0.5,0.4)
eye_r_obj.scale = (0.5,0.5,0.5)
bpy.data.objects["eye_r"].scale = (0.3,0.3,0.3)
mat = bpy.data.materials.new('mat_eye_r')
color = (0, 0, 0, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
eye_r_obj.parent = head_obj
bpy.ops.mesh.primitive_cylinder_add()
bpy.context.object.name = "jiaor"
bpy.context.object.parent = niao
bpy.data.objects["jiaor"].location = (-1,-0.5,0)
bpy.data.objects["jiaor"].rotation_euler = (0,0,0)
bpy.data.objects["jiaor"].scale = (0.1,0.1,0.8)
mat = bpy.data.materials.new('mat_jiaor')
color = (0.5, 0.3, 0.2, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = niao
bpy.ops.mesh.primitive_cylinder_add()
bpy.context.object.name = "jiaol"
bpy.context.object.parent = niao
bpy.data.objects["jiaol"].location = (-1,0.5,0)
bpy.data.objects["jiaol"].rotation_euler = (0,0,0)
bpy.data.objects["jiaol"].scale = (0.1,0.1,0.8)
mat = bpy.data.materials.new('mat_jiaol')
color = (0.5, 0.3, 0.2, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = niao
bpy.ops.mesh.primitive_cylinder_add()
bpy.context.object.name = "cao"
bpy.data.objects["cao"].location = (0,0,0)
bpy.data.objects["cao"].rotation_euler = (0,0,0)
bpy.data.objects["cao"].scale = (20,20,0.1)
mat = bpy.data.materials.new('mat_cao')
color =( 0.1, 0.6, 0.1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "mi"
bpy.data.objects["mi"].location = (2,.7,0.2)
bpy.data.objects["mi"].rotation_euler = (0,0,5)
bpy.data.objects["mi"].scale = (0.3,0.1,0.1)
mat = bpy.data.materials.new('mat_mi')
color = (1, 1, 0.4, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
def init_camera():
camera = bpy.data.cameras.new('Camera')
camera_obj = bpy.data.objects.new('Camera', camera)
bpy.data.collections["Collection"].objects.link(camera_obj)
camera.lens = 50 # Focal length in millimeters
camera.sensor_width = 36 # Sensor width in millimeters
camera.sensor_height = 24 # Sensor height in millimeters
camera_obj.location = (21,5.9, 8.1) # X, Y, Z coordinates
camera_obj.rotation_euler = (1.172,0,1.8)
bpy.context.scene.camera = camera_obj
def create_camera():
bpy.ops.object.camera_add()
camera = bpy.context.object
camera.location = (12.101823806762695, -4.5784618854522705, 8.910723686218262)
camera.rotation_euler = (1.1, 0.013265283778309822, 1.1482713222503662)
bpy.context.scene.camera = camera
def create_light():
bpy.ops.object.light_add(type='SPOT')
light = bpy.context.object
light.location = (6.413093566894531, -6.293662071228027, 12.76513385772705)
light.rotation_euler = (0.6422812938690186, -1.269639060552663e-08, 0.802851676940918)
light.data.energy = 8000
bpy.context.object.scale = (3,3,3)
def render():
bpy.context.scene.frame_end = 10
bpy.context.scene.render.resolution_x = 1280
bpy.context.scene.render.resolution_y = 720
bpy.context.scene.render.resolution_percentage = 50
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.render.filepath = 'D:/bL/ji'
bpy.ops.render.render(animation=True)
create_niao()
create_camera()
create_light()
render()
import bpy
def create_material(name, color):
mat = bpy.data.materials.new(name)
mat.diffuse_color = color
return mat
def create_egg():
# 清空场景
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete()
# 创建空对象作为父级
egg = bpy.data.objects.new("egg", None)
bpy.data.collections["Collection"].objects.link(egg)
# 创建各个部件
parts = {}
# 身体
bpy.ops.mesh.primitive_uv_sphere_add()
parts['body'] = bpy.context.object
parts['body'].name = "body"
parts['body'].location = (0, 0, 0)
parts['body'].scale = (5.0, 5.0, 5.0)
parts['body'].data.materials.append(create_material('mat_body', (1, 0.779, 0.012, 1)))
# 脸部
bpy.ops.mesh.primitive_uv_sphere_add()
parts['face'] = bpy.context.object
parts['face'].name = "face"
parts['face'].location = (1.5, 0, 0.5)
parts['face'].scale = (3.85, 3.9, 3.9)
parts['face'].data.materials.append(create_material('mat_face', (1, 0.829, 0.726, 1)))
# 手部
hand_configs = [
("r_hand", (0.4, 3.8, 0), (1.6, 4.5, 1.6), (0, 0.35, 0)),
("l_hand", (0.4, -3.8, 0.5), (1.6, 4.5, 1.6), (-0.6, 0.35, 0))
]
for name, loc, scale, rot in hand_configs:
bpy.ops.mesh.primitive_uv_sphere_add()
hand = bpy.context.object
hand.name = name
hand.location = loc
hand.scale = scale
hand.rotation_euler = rot
hand.data.materials.append(create_material('mat_hand', (1, 0.779, 0.012, 1)))
parts[name] = hand
# 脚部
foot_configs = [
("r_foot", (4.5, 2.5, -2.6), (1.5, 1.6, 2.0), (0, 0.35, 0.87)),
("l_foot", (-0.5, -2.8, -4.5), (1.6, 1.8, 2.0), (-0.5, 0, -0.6))
]
for name, loc, scale, rot in foot_configs:
bpy.ops.mesh.primitive_uv_sphere_add()
foot = bpy.context.object
foot.name = name
foot.location = loc
foot.scale = scale
foot.rotation_euler = rot
foot.data.materials.append(create_material('mat_foot', (1, 1, 1, 1)))
parts[name] = foot
# 头顶装饰
bpy.ops.mesh.primitive_cylinder_add()
parts['kaka'] = bpy.context.object
parts['kaka'].name = "kaka"
parts['kaka'].location = (0, 0, 5.5)
parts['kaka'].scale = (0.4, 0.4, 1)
parts['kaka'].data.materials.append(create_material('mat_kaka', (1, 0.779, 0.012, 1)))
bpy.ops.mesh.primitive_uv_sphere_add()
parts['jiujiu'] = bpy.context.object
parts['jiujiu'].name = "jiujiu"
parts['jiujiu'].location = (0, 0, 6.5)
parts['jiujiu'].scale = (0.9, 0.9, 0.9)
parts['jiujiu'].data.materials.append(create_material('mat_jiujiu', (1, 0.779, 0.012, 1)))
# 眼睛
eye_configs = [
("r_eye", (4.7, 1.3, 2.0), (0.6, 0.6, 0.6), (1, 1, 1, 1)),
("l_eye", (4.7, -1.3, 2.0), (0.6, 0.6, 0.6), (1, 1, 1, 1)),
("r_eyelittle", (5.0, 1.3, 2.1), (0.4, 0.4, 0.4), (0, 0, 0, 1)),
("l_eyelittle", (5.0, -1.3, 2.1), (0.4, 0.4, 0.4), (0, 0, 0, 1))
]
for name, loc, scale, color in eye_configs:
bpy.ops.mesh.primitive_uv_sphere_add()
eye = bpy.context.object
eye.name = name
eye.location = loc
eye.scale = scale
eye.data.materials.append(create_material(f'mat_{name}', color))
parts[name] = eye
# 嘴巴
bpy.ops.mesh.primitive_uv_sphere_add()
parts['mouth'] = bpy.context.object
parts['mouth'].name = "mouth"
parts['mouth'].location = (5.3, 0, 0.5)
parts['mouth'].scale = (0.2, 0.4, 0.6)
parts['mouth'].data.materials.append(create_material('mat_mouth', (1, 0.2, 0.3, 1)))
# 选择所有对象
bpy.ops.object.select_all(action='DESELECT')
for obj in bpy.data.objects:
if obj != egg and obj.type == 'MESH':
obj.select_set(True)
obj.parent = egg
return egg,parts
def setup_scene():
egg, parts = create_egg()
# 创建地面
bpy.ops.mesh.primitive_plane_add(size=20, location=(0, 0, -6))
ground = bpy.context.object
ground.name = "Ground"
ground.cycles.is_shadow_catcher = True
# 设置相机
camera = bpy.data.cameras.new('MyCamera')
camera_obj = bpy.data.objects.new('CameraObj', camera)
bpy.context.scene.collection.objects.link(camera_obj)
camera.lens = 50
camera.sensor_width = 36
camera.sensor_height = 24
camera_obj.location = (35, -1.8, 8.0)
camera_obj.rotation_euler = (1.40, 0.014, 1.50)
bpy.context.scene.camera = camera_obj
# 设置灯光
bpy.ops.object.light_add(type='SUN',radius=20,location=(0,20,0), rotation=(-1.57,0,0))
bpy.ops.object.light_add(type='AREA',radius=20,location=(-13, -8, 10), rotation=(0.0, -0.84, 0.58))
bpy.context.object.data.energy = 2000
bpy.ops.object.light_add(type='AREA',radius=20,location=(13, -8, 10), rotation=(0.0, -0.84, 2.8))
bpy.context.object.data.energy = 2000
# 动画
bpy.context.scene.frame_end = 30
bpy.context.scene.frame_current = 1
# 获取角色和部件引用
r_foot = parts['r_foot']
l_foot = parts['l_foot']
r_hand = parts['r_hand']
l_hand = parts['l_hand']
for i in range(5):
if i % 2 == 0:
egg.location = (0,0,0)
else:
egg.location = (0,0,4)
egg.keyframe_insert(data_path="location",frame=1+i*15,index=2)
if i % 2 == 0:
r_foot.location = (4.5, 2.5, -2.6)
l_foot.location = (-0.5, -2.8, -4.5)
else:
r_foot.location = (-0.5, 2.5, -4.5)
l_foot.location = (4.5, -2.8, -2.6)
# 为脚部添加关键帧(Y轴方向)
r_foot.keyframe_insert(data_path="location",frame=1+i*15)
l_foot.keyframe_insert(data_path="location",frame=1+i*15)
if i % 2 == 0:
r_hand.rotation_euler = (0, 0.35, 0)
l_hand.rotation_euler = (-0.6, 0.35, 0)
else:
r_hand.rotation_euler = (0.6, 0.35, 0)
l_hand.rotation_euler = (0.6, 0.35, 0)
# 为脚部添加关键帧(Y轴方向)
r_hand.keyframe_insert(data_path="rotation_euler",frame=1+i*15)
l_hand.keyframe_insert(data_path="rotation_euler",frame=1+i*15)
# 设置序列图片渲染参数
bpy.context.scene.render.resolution_x = 1280
bpy.context.scene.render.resolution_y = 960
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.render.filepath = "D:/eggy/e"
# 设置视频渲染路径和渲染动画
bpy.context.scene.render.filepath = "/tmp/egg.mp4"
bpy.context.scene.render.image_settings.file_format = 'FFMPEG'
bpy.ops.render.render(animation=True)
if __name__ == "__main__":
egg = create_egg()
setup_scene()
bpy.ops.render.render(write_still=True)
- 本文来自 oeasy Python 系统教程。
- 想完整、扎实学 Python,
- 搜索 oeasy 即可。

