Skip to content

Latest commit

 

History

History
532 lines (408 loc) · 13.6 KB

655-2733716-运动的总结.sy.md

File metadata and controls

532 lines (408 loc) · 13.6 KB
show version enable_checker
step
1.0
true

python_blender

开始

  • 上次玩了动画
  • 可以把动画流程总结一下吗?

还原场景

import bpy

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

def create_man():
    mat = bpy.data.materials.new('blue')
    color = (0, 0, 1, 1)
    mat.diffuse_color = color
    man  = bpy.data.objects.new("man", None)
    bpy.data.collections["Collection"].objects.link(man)
    bpy.ops.mesh.primitive_uv_sphere_add()
    head = bpy.context.object
    head.location = (0,0,2.5)
    head.data.materials.append(mat)
    head.parent = man
    bpy.ops.mesh.primitive_cone_add()
    body = bpy.context.object
    body.location = (0,0,1)
    body.parent = man
    body.data.materials.append(mat)

def create_plane():
    bpy.ops.mesh.primitive_plane_add()
    plane = bpy.context.object
    plane.scale = (10,10,1)

def create_camera():
    bpy.ops.object.camera_add()
    camera = bpy.context.object
    camera.location = (3.059990406036377, -9.885231018066406, 12.905241966247559)
    camera.rotation_euler = (0.7260572910308838, -6.679311326251991e-09, 0.30019673705101013)
    bpy.context.scene.camera = camera

def create_spot():
    bpy.ops.object.light_add(type='SPOT')
    spot = bpy.context.object
    spot.location = (-1.2811667919158936, -3.316868543624878, 12.282358169555664)
    spot.rotation_euler = (0.34208473563194275, 1.8850268901360323e-08, -0.3071775436401367)
    spot.data.energy = 1000

def render():
    bpy.context.scene.render.resolution_x = 320
    bpy.context.scene.render.resolution_y = 240
    bpy.context.scene.render.resolution_percentage = 50
    bpy.context.scene.render.engine = 'CYCLES'
    bpy.context.scene.render.filepath = '/tmp/render2.png'
    bpy.ops.render.render(write_still=True)

clear_scene()
create_man()
create_plane()
create_camera()
create_spot()
render()

设置动画

  • 为人物增加关键帧
帧号 location
1 (0,0,0)
10 (10,0,0)
man.location = (0, 0, 0) 
man.keyframe_insert(data_path="location", frame=1)  
man.location = (10, 0, 0)
man.keyframe_insert(data_path="location", frame=10) 
  • 在layout视图中观察到
    • 关键
    • 人物动画

图片描述

  • 能动了吗?

观察

  • 在layout工作区
    • 观察man的时间轴
    • 确实有关键帧

图片描述

  • 能渲染出来吗

设置渲染

  • 切回Script工作区
    • 设置渲染动画
def render():
    bpy.context.scene.frame_end = 10
    bpy.context.scene.render.resolution_x = 200
    bpy.context.scene.render.resolution_y = 150
    bpy.context.scene.render.resolution_percentage = 50
    bpy.context.scene.render.engine = 'CYCLES'
    bpy.context.scene.render.filepath = "/home/shiyanlou/Code/o"
    bpy.ops.render.render(animation=True)
  • 点击运行按钮
    • 渲染起来

图片描述

安装序列查看工具

feh /home/shiyanlou/Code
  • 最终效果
    • 人物动了
    • 跑黑影里了

图片描述

  • 下面准备
    • 让灯光追踪角色

添加约束

  • 选中聚光灯spot
    • 约束调板
    • 添加对象约束

图片描述

设置约束

  • 约束类型为
    • Track to

图片描述

  • 将Track To目标
    • 设置为man

图片描述

  • 并设置z轴负向
    • 指向man

约束效果

  • 开启实时渲染模式

图片描述

  • 在layout工作区

图片描述

  • 将上述工作转化为代码

代码

import bpy

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

def create_man():
    mat = bpy.data.materials.new('blue')
    color = (0, 0, 1, 1)
    mat.diffuse_color = color
    man  = bpy.data.objects.new("man", None)
    bpy.data.collections["Collection"].objects.link(man)
    bpy.ops.mesh.primitive_uv_sphere_add()
    head = bpy.context.object
    head.location = (0,0,2.5)
    head.data.materials.append(mat)
    head.parent = man
    bpy.ops.mesh.primitive_cone_add()
    body = bpy.context.object
    body.location = (0,0,1)
    body.parent = man
    body.data.materials.append(mat)
    man.location = (0, 0, 0)
    man.keyframe_insert(data_path="location", frame=1)
    man.location = (10, 0, 0)
    man.keyframe_insert(data_path="location", frame=10)

def create_plane():
    bpy.ops.mesh.primitive_plane_add()
    plane = bpy.context.object
    plane.scale = (10,10,1)

def create_camera():
    bpy.ops.object.camera_add()
    camera = bpy.context.object
    camera.location = (3.059990406036377, -9.885231018066406, 12.905241966247559)
    camera.rotation_euler = (0.7260572910308838, -6.679311326251991e-09, 0.30019673705101013)
    bpy.context.scene.camera = camera

def create_spot():
    bpy.ops.object.light_add(type='SPOT')
    spot = bpy.context.object
    spot.location = (-1.2811667919158936, -3.316868543624878, 12.282358169555664)
    spot.rotation_euler = (0.34208473563194275, 1.8850268901360323e-08, -0.3071775436401367)
    spot.data.energy = 1000
    spot.constraints.new(type='TRACK_TO')
    spot.constraints["Track To"].track_axis = 'TRACK_NEGATIVE_Z'
    spot.constraints["Track To"].use_target_z = True
    spot.constraints["Track To"].up_axis = 'UP_X'
    spot.constraints["Track To"].target = bpy.data.objects["man"]

def render():
    bpy.context.scene.frame_end = 10
    bpy.context.scene.render.resolution_x = 200
    bpy.context.scene.render.resolution_y = 150
    bpy.context.scene.render.resolution_percentage = 50
    bpy.context.scene.render.engine = 'CYCLES'
    bpy.context.scene.render.filepath = "/home/shiyanlou/Code/o"
    bpy.ops.render.render(animation=True)

clear_scene()
create_man()
create_plane()
create_camera()
create_spot()
render()

问题

feh /home/shiyanlou/Code
  • 观察序列效果

图片描述

  • 摄影机没跟上

初始帧

  • 在layout工作区
    • 选中摄影机
    • 在第1帧
    • 设置rotation属性为关键帧

图片描述

调试摄影机

  • 开启 4视图模式

图片描述

调整视图

  • 将播放头移动到第10帧
    • 并将 透视图 设置为
    • 摄影机看到的画面

图片描述

调整旋转

  • 在保证
    • 摄影机位置不变的基础上
    • 调整摄影机的旋转角度
      • 蓝色轴控制z轴旋转
      • 红绿轴控制xy轴旋转

图片描述

  • 观察最终位置
    • 并代码化

代码化

图片描述

  • 相应代码
import bpy

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

def create_man():
    mat = bpy.data.materials.new('blue')
    color = (0, 0, 1, 1)
    mat.diffuse_color = color
    man  = bpy.data.objects.new("man", None)
    bpy.data.collections["Collection"].objects.link(man)
    bpy.ops.mesh.primitive_uv_sphere_add()
    head = bpy.context.object
    head.location = (0,0,2.5)
    head.data.materials.append(mat)
    head.parent = man
    bpy.ops.mesh.primitive_cone_add()
    body = bpy.context.object
    body.location = (0,0,1)
    body.parent = man
    body.data.materials.append(mat)
    man.location = (0, 0, 0)
    man.keyframe_insert(data_path="location", frame=1)
    man.location = (10, 0, 0)
    man.keyframe_insert(data_path="location", frame=10)

def create_plane():
    bpy.ops.mesh.primitive_plane_add()
    plane = bpy.context.object
    plane.scale = (10,10,1)

def create_camera():
    bpy.ops.object.camera_add()
    camera = bpy.context.object
    camera.location = (3.059990406036377, -9.885231018066406, 12.905241966247559)
    camera.rotation_euler = (0.7260572910308838, -6.679311326251991e-09, 0.30019673705101013)
    bpy.context.scene.camera = camera    
    camera.keyframe_insert(data_path="rotation_euler", frame=1)
    camera.rotation_euler = (0.7763736844062805, -0.06353582441806793, -0.6263947486877441)
    camera.keyframe_insert(data_path="rotation_euler", frame=10)

def create_spot():
    bpy.ops.object.light_add(type='SPOT')
    spot = bpy.context.object
    spot.location = (-1.2811667919158936, -3.316868543624878, 12.282358169555664)
    spot.rotation_euler = (0.34208473563194275, 1.8850268901360323e-08, -0.3071775436401367)
    spot.data.energy = 1000
    spot.constraints.new(type='TRACK_TO')
    spot.constraints["Track To"].track_axis = 'TRACK_NEGATIVE_Z'
    spot.constraints["Track To"].use_target_z = True
    spot.constraints["Track To"].up_axis = 'UP_X'
    spot.constraints["Track To"].target = bpy.data.objects["man"]

def render():
    bpy.context.scene.frame_end = 10
    bpy.context.scene.render.resolution_x = 200
    bpy.context.scene.render.resolution_y = 150
    bpy.context.scene.render.resolution_percentage = 50
    bpy.context.scene.render.engine = 'CYCLES'
    bpy.context.scene.render.filepath = "/home/shiyanlou/Code/o"
    bpy.ops.render.render(animation=True)

clear_scene()
create_man()
create_plane()
create_camera()
create_spot()
render()

再观察

feh /home/shiyanlou/Code
  • 观察序列效果
    • 灯光和摄影机
      • 都开始跟着角色了

图片描述

  • 可以 把镜头
    • 推上来吗?

摄影机镜头推焦距

名称 时长
起幅 10帧
镜头焦距推 10帧
落幅 10帧
  • 将视图
    • 从4视图
    • 还原为单一视图

图片描述

  • 观察摄影机焦距
    • 50mm
  • 尝试推镜头

推镜头

  • 将镜头焦距
    • 设置为70 mm
    • 观察效果

图片描述

  • 将 推镜头 代码化

代码

import bpy

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

def create_man():
    mat = bpy.data.materials.new('blue')
    color = (0, 0, 1, 1)
    mat.diffuse_color = color
    man  = bpy.data.objects.new("man", None)
    bpy.data.collections["Collection"].objects.link(man)
    bpy.ops.mesh.primitive_uv_sphere_add()
    head = bpy.context.object
    head.location = (0,0,2.5)
    head.data.materials.append(mat)
    head.parent = man
    bpy.ops.mesh.primitive_cone_add()
    body = bpy.context.object
    body.location = (0,0,1)
    body.parent = man
    body.data.materials.append(mat)
    man.location = (0, 0, 0)
    man.keyframe_insert(data_path="location", frame=1)
    man.location = (10, 0, 0)
    man.keyframe_insert(data_path="location", frame=10)

def create_plane():
    bpy.ops.mesh.primitive_plane_add()
    plane = bpy.context.object
    plane.scale = (10,10,1)

def create_camera():
    bpy.ops.object.camera_add()
    camera = bpy.context.object
    camera.location = (3.059990406036377, -9.885231018066406, 12.905241966247559)
    camera.rotation_euler = (0.7260572910308838, -6.679311326251991e-09, 0.30019673705101013)
    bpy.context.scene.camera = camera    
    camera.keyframe_insert(data_path="rotation_euler", frame=1)
    camera.rotation_euler = (0.7763736844062805, -0.06353582441806793, -0.6263947486877441)
    camera.keyframe_insert(data_path="rotation_euler", frame=10)
    camera.data.lens = 50
    camera.data.keyframe_insert(data_path="lens", frame=20)
    camera.data.lens = 70
    camera.data.keyframe_insert(data_path="lens", frame=30)
    camera.data.lens = 70
    camera.data.keyframe_insert(data_path="lens", frame=40)
    

def create_spot():
    bpy.ops.object.light_add(type='SPOT')
    spot = bpy.context.object
    spot.location = (-1.2811667919158936, -3.316868543624878, 12.282358169555664)
    spot.rotation_euler = (0.34208473563194275, 1.8850268901360323e-08, -0.3071775436401367)
    spot.data.energy = 1000
    spot.constraints.new(type='TRACK_TO')
    spot.constraints["Track To"].track_axis = 'TRACK_NEGATIVE_Z'
    spot.constraints["Track To"].use_target_z = True
    spot.constraints["Track To"].up_axis = 'UP_X'
    spot.constraints["Track To"].target = bpy.data.objects["man"]
    

def render():
    bpy.context.scene.frame_end = 40
    bpy.context.scene.render.resolution_x = 200
    bpy.context.scene.render.resolution_y = 150
    bpy.context.scene.render.resolution_percentage = 50
    bpy.context.scene.render.engine = 'CYCLES'
    bpy.context.scene.render.filepath = "/home/shiyanlou/Code/o"
    bpy.ops.render.render(animation=True)

clear_scene()
create_man()
create_plane()
create_camera()
create_spot()
render()

观察最终效果

图片描述

  • 这次总共需要渲染40帧
feh /home/shiyanlou/Code
  • 可以在时间轴上看关键帧吗?

观看效果

  • 在dope sheet 中观察
    • 关键帧

图片描述

  • 摄影机的旋转
    • 1、10帧
  • 摄影机的焦距
    • 20、30、40帧

总结 🤔

  • 这次将动态图渲染做了总结
  • ?🤔
  • 我们下次再说!👋