Skip to content

Latest commit

 

History

History
506 lines (415 loc) · 18 KB

File metadata and controls

506 lines (415 loc) · 18 KB
Error in user YAML: (<unknown>): could not find expected ':' while scanning a simple key at line 4 column 1
---
show: step
version: 1.0 
enable_checker: true
本教程同步发布在: 

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

  • oeasy Python 0727
  • 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。

python_blender

开始

  • 上次 我们
    • 设置了 关键帧
    • 制作了 动画
    • 并且渲染出了 动画序列

图片描述

  • 我们可以设置变化的灯光吗?🤔
import bpy

default_camera = bpy.data.objects['Camera']
bpy.data.objects.remove(default_camera)
default_light = bpy.data.objects['Light']
bpy.data.objects.remove(default_light)

bpy.ops.object.select_all(action='DESELECT')  
bpy.ops.object.select_by_type(type='MESH')   
bpy.ops.object.delete() 


bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, location=(0, 0, 2))
head = bpy.context.object
head.data.materials.append(bpy.data.materials.new(name="Head"))
head.data.materials[-1].diffuse_color = (1.0, 0.8, 0.2, 1.0)

bpy.ops.mesh.primitive_cube_add(location=(0, 0, 1.2))
body = bpy.context.object
body.scale = (0.5, 0.5, 0.5)  
body.data.materials.append(bpy.data.materials.new(name="Body"))
body.data.materials[-1].diffuse_color = (0.2, 0.4, 0.8, 1.0)

bpy.ops.mesh.primitive_cube_add(location=(0.7, 0, 1.3))
left_arm = bpy.context.object
left_arm.scale = (0.2, 0.2, 0.2)
left_arm.data.materials.append(bpy.data.materials.new(name="LeftArm"))
left_arm.data.materials[-1].diffuse_color = (0.8, 0.2, 0.2, 1.0)

bpy.ops.mesh.primitive_cube_add(location=(-0.7, 0, 1.3))
right_arm = bpy.context.object
right_arm.scale = (0.2, 0.2, 0.2)
right_arm.data.materials.append(bpy.data.materials.new(name="RightArm"))
right_arm.data.materials[-1].diffuse_color = (0.8, 0.2, 0.2, 1.0)

bpy.ops.mesh.primitive_cube_add(location=(0.2, 0, 0.5))
left_leg = bpy.context.object
left_leg.scale = (0.14, 0.2, 0.6)
left_leg.data.materials.append(bpy.data.materials.new(name="LeftLeg"))
left_leg.data.materials[-1].diffuse_color = (0.2, 0.8, 0.2, 1.0)

bpy.ops.mesh.primitive_cube_add(location=(-0.2, 0, 0.5))
right_leg = bpy.context.object
right_leg.scale = (0.14, 0.2, 0.6)
right_leg.data.materials.append(bpy.data.materials.new(name="RightLeg"))
right_leg.data.materials[-1].diffuse_color = (0.2, 0.8, 0.2, 1.0)

bpy.ops.mesh.primitive_cylinder_add(radius=0.1, depth=0.4, location=(0.9, 0, 1.3))
left_arm_cylinder = bpy.context.object
left_arm_cylinder.rotation_euler[1] = 1.5708  # 使其平行于X轴
left_arm_cylinder.name = "左臂圆柱"
mat_red = bpy.data.materials.get("LeftArm")
if mat_red is None:
    mat_red = bpy.data.materials.new(name="LeftArm")
    mat_red.diffuse_color = (0.8, 0.2, 0.2, 1.0)
left_arm_cylinder.data.materials.append(mat_red)

bpy.ops.mesh.primitive_cylinder_add(radius=0.1, depth=0.4, location=(-0.9, 0, 1.3))
right_arm_cylinder = bpy.context.object
right_arm_cylinder.rotation_euler[1] = 1.5708  # 使其平行于X轴
right_arm_cylinder.name = "右臂圆柱"
mat_red = bpy.data.materials.get("RightArm")
if mat_red is None:
    mat_red = bpy.data.materials.new(name="RightArm")
    mat_red.diffuse_color = (0.8, 0.2, 0.2, 1.0)
right_arm_cylinder.data.materials.append(mat_red)

bpy.ops.object.select_all(action='DESELECT')
head.select_set(True)
body.select_set(True)
left_arm.select_set(True)
right_arm.select_set(True)
left_leg.select_set(True)
right_leg.select_set(True)
left_arm_cylinder.select_set(True)
right_arm_cylinder.select_set(True)
bpy.context.view_layer.objects.active = head
bpy.ops.object.join()
bpy.context.object.name = "MergedCharacter"

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.15, location=(0, 1.5, 1.5))
red_sphere = bpy.context.object

mat_red = bpy.data.materials.new(name="Red")
mat_red.diffuse_color = (1, 0, 0, 1)  
red_sphere.data.materials.append(mat_red)

bpy.ops.mesh.primitive_cylinder_add(radius=0.05, depth=1.5, location=(0, 1.5, 0.75))
mic_pole = bpy.context.object
mat_white = bpy.data.materials.new(name="MicPoleWhite")
mat_white.diffuse_color = (1, 1, 1, 1)
mic_pole.data.materials.append(mat_white)

bpy.ops.mesh.primitive_cylinder_add(radius=1.5, depth=0.1, location=(0, 0, 0.05))
lens = bpy.context.object
mat_black = bpy.data.materials.new(name="Black")
mat_black.diffuse_color = (0, 0, 0, 1)  
lens.data.materials.append(mat_black)

bpy.ops.mesh.primitive_torus_add(align='WORLD', location=(0, 0, 0), major_radius=1.6, minor_radius=0.1)
frame = bpy.context.object
mat_gold = bpy.data.materials.new(name="Gold")
mat_gold.diffuse_color = (1.0, 0.5, 1.0, 1)  
frame.data.materials.append(mat_gold)

lens.name = "舞台"
frame.name = "舞台边"

bpy.ops.mesh.primitive_cylinder_add(radius=1.5, depth=0.1, location=(5, -8, 5))
lens1 = bpy.context.object
lens1.name = "舞台左"
mat_black1 = bpy.data.materials.new(name="Black")
mat_black1.diffuse_color = (0, 0, 0, 1)  
lens1.data.materials.append(mat_black1)

bpy.ops.mesh.primitive_torus_add(align='WORLD', location=(5, -8, 5), major_radius=1.6, minor_radius=0.1)
frame1 = bpy.context.object
frame1.name = "舞台边左"
mat_gold1 = bpy.data.materials.new(name="Gold")
mat_gold1.diffuse_color = (0.8, 0.6, 0.2, 1)  
frame1.data.materials.append(mat_gold1)

bpy.ops.mesh.primitive_cylinder_add(radius=1.5, depth=0.1, location=(0, -8, 5))  
lens2 = bpy.context.object
lens2.name = "舞台右"
mat_black2 = bpy.data.materials.new(name="Black")
mat_black2.diffuse_color = (0, 0, 0, 1)  
lens2.data.materials.append(mat_black2)

bpy.ops.mesh.primitive_torus_add(align='WORLD', location=(0, -8, 5), major_radius=1.6, minor_radius=0.1)  
frame2 = bpy.context.object
frame2.name = "舞台边右"
mat_gold2 = bpy.data.materials.new(name="Gold")
mat_gold2.diffuse_color = (0.8, 0.6, 0.2, 1)  
frame2.data.materials.append(mat_gold2)

bpy.context.view_layer.objects.active = bpy.data.objects["舞台左"]
bpy.data.objects["舞台左"].select_set(True)
bpy.data.objects["舞台边左"].select_set(True)

bpy.context.view_layer.objects.active = bpy.data.objects["舞台右"]
bpy.data.objects["舞台右"].select_set(True)
bpy.data.objects["舞台边右"].select_set(True)

bpy.ops.transform.rotate(value=1.5708, orient_axis='X') 

bpy.ops.mesh.primitive_cylinder_add(radius=0.2, depth=2, location=(2.5, -8, 5))  
connector = bpy.context.object
connector.name = "眼睛连接"
bpy.ops.transform.resize(value=(1, 1, 1))  
bpy.ops.transform.rotate(value=1.5708, orient_axis='X')  
bpy.ops.transform.rotate(value=1.5708, orient_axis='Z')  
mat_black = bpy.data.materials.new(name="Black")
mat_black.diffuse_color = (0, 0, 0, 1)  
connector.data.materials.append(mat_black)

bpy.ops.mesh.primitive_cylinder_add(radius=0.1, depth=2, location=(6.5, -10, 5))  
leg_left = bpy.context.object
leg_left.name = "左眼睛腿"
bpy.ops.transform.resize(value=(1, 1, 2))  
bpy.ops.transform.rotate(value=1.5708, orient_axis='Y')  
bpy.ops.transform.rotate(value=1.5708, orient_axis='Z')  
leg_left.data.materials.append(mat_black)

bpy.ops.mesh.primitive_cylinder_add(radius=0.1, depth=2, location=(-1.5, -10, 5))  
leg_right = bpy.context.object
leg_right.name = "右眼睛腿"
bpy.ops.transform.resize(value=(1, 1, 2))  
bpy.ops.transform.rotate(value=1.5708, orient_axis='Y')  
bpy.ops.transform.rotate(value=1.5708, orient_axis='Z')  
leg_right.data.materials.append(mat_black)

bpy.ops.mesh.primitive_cube_add(size=2, location=(-4, 0, 1.5))  
bpy.ops.transform.resize(value=(1, 1, 1.5))  
speaker_box = bpy.context.object
speaker_box.name = "音箱主体"

mat_yellow = bpy.data.materials.new(name="FrontMaterialYellow")
mat_yellow.diffuse_color = (1, 1, 0, 1)  

speaker_box.data.materials.append(mat_yellow)
speaker_box.data.polygons[4].material_index = 0 


bpy.ops.mesh.primitive_cylinder_add(vertices=100, radius=1, depth=0.1, location=(-4, 1, 2))  
large_circle = bpy.context.object
bpy.ops.transform.resize(value=(0.6, 0.6, 0.6))  
bpy.ops.transform.rotate(value=1.5708, orient_axis='X')  
bpy.ops.transform.rotate(value=1.5708, orient_axis='Y') 
large_circle.name 
mat_red = bpy.data.materials.new(name="SpeakerTopRed")
mat_red.diffuse_color = (1, 0, 0, 1)  
large_circle.data.materials.append(mat_red)

bpy.ops.mesh.primitive_cylinder_add(vertices=100, radius=1, depth=0.1, location=(-4, 1, 0.7))  
large_circle2 = bpy.context.object
bpy.ops.transform.resize(value=(0.6, 0.6, 0.6))  
bpy.ops.transform.rotate(value=1.5708, orient_axis='X') 
bpy.ops.transform.rotate(value=1.5708, orient_axis='Y')  
large_circle2.name 
mat_red = bpy.data.materials.new(name="SpeakerTopRed")
mat_red.diffuse_color = (1, 0, 0, 1)  
large_circle2.data.materials.append(mat_red)

bpy.ops.mesh.primitive_torus_add(major_radius=0.6, minor_radius=0.05, location=(-4, 1, 2)) 
large_ring1 = bpy.context.object
large_ring1.name = "大圆环1"
bpy.ops.transform.rotate(value=1.5708, orient_axis='X')  
mat_blue = bpy.data.materials.new(name="FrontBlue")
mat_blue.diffuse_color = (0, 0, 0.5, 1)  
large_ring1.data.materials.append(mat_blue)

bpy.ops.mesh.primitive_torus_add(major_radius=0.6, minor_radius=0.05, location=(-4, 1, 0.7)) 
large_ring2 = bpy.context.object
large_ring2.name = "大圆环2"
bpy.ops.transform.rotate(value=1.5708, orient_axis='X')  
mat_blue = bpy.data.materials.new(name="SpeakerFrontBlue")
mat_blue.diffuse_color = (0, 0, 0.5, 1)  
large_ring2.data.materials.append(mat_blue)

bpy.ops.object.text_add(location=(-2, -5, 3.5))
text_object = bpy.context.object
text_object.name = "RapstarText"
text_object.data.body = "Rapstar"  
text_object.data.extrude = 0.1  
text_object.data.size = 1.5  
bpy.ops.transform.rotate(value=3.14159/2, orient_axis='X')
bpy.ops.transform.rotate(value=3.14159, orient_axis='Y')
mat_white = bpy.data.materials.new(name="PureWhite")
mat_white.diffuse_color = (1, 1, 1, 1)  
text_object.data.materials.append(mat_white)

import math


bpy.ops.object.camera_add(location=(-1.6966, 14.759, 2.3761))


rotation_angle = (math.radians(85.437), math.radians(-0.000011), math.radians(-172.79))
bpy.context.object.rotation_euler = rotation_angle

camera = bpy.context.object

camera.location = (-1.6966, 14.759, 2.3761)  
camera.keyframe_insert(data_path="location", frame=80)  

camera.location = (-1.6966, 25, 2.3761) 
camera.keyframe_insert(data_path="location", frame=120) 

import math

bpy.ops.object.light_add(type='SPOT', location=(12.594, 8.0708, 6.4524))

spot_light = bpy.context.object
spot_light.data.energy = 1500  
spot_light.data.spot_size = math.radians(60)  
spot_light.data.spot_blend = 0.2  


spot_light.location = (6.363, 4.8202, 5.5)

spot_light.rotation_euler = (math.radians(246.89), math.radians(188.58), math.radians(-47.18))

color_keyframes = [(1, (1, 0, 0)),  
                   (20, (1, 1, 0)), 
                   (40, (0, 0, 1)), 
                   (60, (0, 1, 0)), 
                   (80, (1, 1, 1))] 

for frame, color in color_keyframes:
    spot_light.data.color = color
    spot_light.data.keyframe_insert(data_path="color", frame=frame)

spot_light.data.keyframe_insert(data_path="color", frame=130)

bpy.ops.mesh.primitive_plane_add(size=100, location=(0, 0, 0))

head.location = (0, 0, 2) 
head.keyframe_insert(data_path="location", frame=1)  
head.location = (3, 0, 2)
head.keyframe_insert(data_path="location", frame=20) 
head.location = (0, 0, 2)
head.keyframe_insert(data_path="location", frame=40)
head.location = (-2, 0, 2)
head.keyframe_insert(data_path="location", frame=60)
head.location = (0, 0, 2)
head.keyframe_insert(data_path="location", frame=80)

bpy.context.view_layer.objects.active = head

text_object.location=(-2,2,3.5)
text_object.keyframe_insert(data_path="location", frame=80)
text_object.location=(-2,-5,3.5)
text_object.keyframe_insert(data_path="location", frame=100)
text_object.location=(-2,2,3.5)
text_object.keyframe_insert(data_path="location", frame=110)
text_object.location=(-2,-5,3.5)
text_object.keyframe_insert(data_path="location", frame=120)
text_object.location=(-2,2,3.5)
text_object.keyframe_insert(data_path="location", frame=130)

speaker_box.location=(-4,0,1.5)
speaker_box.keyframe_insert(data_path="location", frame=80)
speaker_box.location=(-4,0,3)
speaker_box.keyframe_insert(data_path="location", frame=100)
speaker_box.location=(-4,0,1.5)
speaker_box.keyframe_insert(data_path="location", frame=110)
speaker_box.location=(-4,0,3)
speaker_box.keyframe_insert(data_path="location", frame=120)
speaker_box.location=(-4,0,1.5)
speaker_box.keyframe_insert(data_path="location", frame=130)

large_circle.location=(-4,1,2)
large_circle.keyframe_insert(data_path="location", frame=80)
large_circle.location=(-4,1,3.5)
large_circle.keyframe_insert(data_path="location", frame=100)
large_circle.location=(-4,1,2)
large_circle.keyframe_insert(data_path="location", frame=110)
large_circle.location=(-4,1,3.5)
large_circle.keyframe_insert(data_path="location", frame=120)
large_circle.location=(-4,1,2)
large_circle.keyframe_insert(data_path="location", frame=130)

large_circle2.location=(-4,1,0.7)
large_circle2.keyframe_insert(data_path="location", frame=80)
large_circle2.location=(-4,1,2.2)
large_circle2.keyframe_insert(data_path="location", frame=100)
large_circle2.location=(-4,1,0.7)
large_circle2.keyframe_insert(data_path="location", frame=110)
large_circle2.location=(-4,1,2.2)
large_circle2.keyframe_insert(data_path="location", frame=120)
large_circle2.location=(-4,1,0.7)
large_circle2.keyframe_insert(data_path="location", frame=130)

large_ring1.location=(-4,1,2)
large_ring1.keyframe_insert(data_path="location", frame=80)
large_ring1.location=(-4,1,3.5)
large_ring1.keyframe_insert(data_path="location", frame=100)
large_ring1.location=(-4,1,2)
large_ring1.keyframe_insert(data_path="location", frame=110)
large_ring1.location=(-4,1,3.5)
large_ring1.keyframe_insert(data_path="location", frame=120)
large_ring1.location=(-4,1,2)
large_ring1.keyframe_insert(data_path="location", frame=130)

large_ring2.location=(-4,1,0.7)
large_ring2.keyframe_insert(data_path="location", frame=80)
large_ring2.location=(-4,1,2.2)
large_ring2.keyframe_insert(data_path="location", frame=100)
large_ring2.location=(-4,1,0.7)
large_ring2.keyframe_insert(data_path="location", frame=110)
large_ring2.location=(-4,1,2.2)
large_ring2.keyframe_insert(data_path="location", frame=120)
large_ring2.location=(-4,1,0.7)
large_ring2.keyframe_insert(data_path="location", frame=130)

下楼梯的效果

图片描述

import bpy
from math import pi

bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete()

collection = bpy.data.collections["Collection"]
staircase = bpy.data.objects.new("staircase", None)
collection.objects.link(staircase)

bpy.ops.mesh.primitive_cube_add(size=2, location=(0, 0, 0))
cube1 = bpy.context.object
cube1.name = "stair_1"
bpy.context.object.parent = staircase
green1_material = bpy.data.materials.new('Green1Material')
color = (0, 0.29, 0, 1.0) 
green1_material.diffuse_color = color
bpy.context.object.data.materials.append(green1_material)

bpy.ops.mesh.primitive_cube_add(size=2, location=(2, 0, 1))
cube2 = bpy.context.object
cube2.name = "stair_2"
bpy.context.object.parent = staircase
bpy.context.object.data.materials.append(green1_material)

bpy.ops.mesh.primitive_cube_add(size=2, location=(4, 0, 1.5))
cube3 = bpy.context.object
cube3.name = "stair_3"
cube3.dimensions = (2, 2, 3)  
bpy.context.object.parent = staircase
bpy.context.object.data.materials.append(green1_material)

bpy.ops.mesh.primitive_cube_add(size=2, location=(6, 0, 2))
cube4 = bpy.context.object
cube4.dimensions = (2, 2, 4)  
cube4.name = "stair_4"
bpy.context.object.parent = staircase
bpy.context.object.data.materials.append(green1_material)

collection = bpy.data.collections["Collection"]
girl = bpy.data.objects.new("girl", None)
collection.objects.link(girl)

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.8, location=(6, 0, 7))
head = bpy.context.object
head.name = "head"
bpy.context.object.parent = girl
green_material = bpy.data.materials.new('GreenMaterial')
color = (0, 2, 1, 1)
green_material.diffuse_color = color
head.data.materials.append(green_material)

bpy.ops.mesh.primitive_cone_add(radius1=1.2, depth=2, location=(6, 0, 6))
body = bpy.context.object
bpy.context.object.parent = girl
body.data.materials.append(green_material)

bpy.ops.mesh.primitive_cylinder_add(radius=0.2, depth=1, location=(6, -0.3, 4.5))
leftleg = bpy.context.object
bpy.context.object.parent = girl
leftleg.data.materials.append(green_material)

bpy.ops.mesh.primitive_cylinder_add(radius=0.2, depth=1, location=(6, 0.3, 4.5))
rightleg = bpy.context.object
bpy.context.object.parent = girl
rightleg.data.materials.append(green_material)

bpy.ops.mesh.primitive_plane_add(size=30, location=(-2, 0, 0))
ground = bpy.context.object

bpy.ops.object.light_add(type='SPOT', radius=500, location=(-11.98, 4.04, 14.78))
spotlight1 = bpy.context.object
spotlight1.data.energy = 8000
spotlight1.rotation_euler = (-22 * pi / 180, -50 * pi / 180, 14 * pi / 180)

camera = bpy.data.cameras.new('Camera')
camera = bpy.data.objects.new('Camera', camera)
bpy.context.scene.collection.objects.link(camera)
camera.location = (-1.92, 10.26, 5.54)
camera.rotation_euler = (89 * pi/180,0, 561 * pi/180)
bpy.context.scene.camera = camera

girl.location = (0, 0, 0)
girl.keyframe_insert(data_path="location", frame=0)
girl.location = (-1, 0,0)
girl.keyframe_insert(data_path="location", frame=5) 
girl.location = (-2, 0,-1)
girl.keyframe_insert(data_path="location", frame=10) 
girl.location = (-3, 0,-1)
girl.keyframe_insert(data_path="location", frame=15) 
girl.location = (-4, 0,-2)
girl.keyframe_insert(data_path="location", frame=20) 
girl.location = (-5, 0,-2)
girl.keyframe_insert(data_path="location", frame=25) 
girl.location = (-6, 0,-3)
girl.keyframe_insert(data_path="location", frame=30) 
girl.location = (-7, 0,-3)
girl.keyframe_insert(data_path="location", frame=35) 
girl.location = (-8, 0,-4)
girl.keyframe_insert(data_path="location", frame=40) 

camera.location = (-1.92, 10.26, 5.54)
camera.keyframe_insert(data_path="location", frame=0)
camera.location = (-4.18, 9.64, 2.2)
camera.keyframe_insert(data_path="location", frame=40)

总结 🤔

  • 我们下次再说!👋

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