Skip to content

Latest commit

 

History

History
486 lines (364 loc) · 14 KB

652-2685177-关键帧的设置_物体的运动_动画的渲染与查看feh.sy.md

File metadata and controls

486 lines (364 loc) · 14 KB
show version enable_checker
step
1.0
true

python_blender

开始

  • 上次通过python设置了舞台基本要素
    • 灯光
    • 物体
    • 摄像机
  • 并且将结果渲染为一张png
  • 可以让物体动起来吗?🤔

手动运动

  • 设置结束帧为10

图片描述

  • 通过调整滑杆
    • 控制时间轴范围

设置开始帧

  • 选中第1帧
    • 选中立方体
    • 设置属性
    • 并点击关键帧

图片描述

设置结束帧

  • 选中第10帧
    • 选中立方体
    • 设置属性
    • 并点击关键帧

图片描述

颜色作用

图片描述

点击播放

  • 观看效果

图片描述

  • 可以把这10帧导出吗?

导出设置

  • 控制输出大小
    • 宽度
    • 高度
    • 时间范围

图片描述

  • 输出结果

图片描述

  • 可以用代码
    • 完成上述操作吗?

动画设置

  • 新建工程
import bpy

obj = bpy.data.objects["Cube"]
obj.location = (0, 0, 0) 
obj.keyframe_insert(data_path="location", frame=1)  
obj.location = (10, 0, 0)
obj.keyframe_insert(data_path="location", frame=10) 
  • 执行后可以运动

图片描述

渲染设置

import bpy
bpy.context.scene.frame_end = 10
bpy.context.scene.render.resolution_x = 400
bpy.context.scene.render.resolution_y = 300
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.render.filepath = "/tmp/r"
bpy.ops.render.render(animation=True)
  • 可以渲染动画

图片描述

整合代码

  • 执行后可以运动
import bpy

obj = bpy.data.objects["Cube"]
obj.location = (0, 0, 0) 
obj.keyframe_insert(data_path="location")  
obj.location = (10, 0, 0)
obj.keyframe_insert(data_path="location", frame=10) 
bpy.context.scene.frame_end = 10
bpy.context.scene.render.resolution_x = 400
bpy.context.scene.render.resolution_y = 300
bpy.context.scene.render.engine = 'CYCLES'
bpy.ops.render.render(animation=True)
  • 设置并渲染成功

图片描述

  • 可以只设置location.x 上的关键帧吗?

手动过程

  • 新建工程

图片描述

  • 只设置了location.x在第1帧的数值

插入关键帧

  • 对于Cube设置
    • location.x
    • 在第10帧的关键帧

图片描述

  • 点击播放
    • 物体运动
  • 可以用代码来设置吗?

代码设置

  • 新建工程
obj = bpy.data.objects["Cube"]
obj.keyframe_insert(
  • 按下 tab

图片描述

  • 可以控制
    • 第index个分量的动画

代码

  • 新建工程
import bpy

obj = bpy.data.objects["Cube"]
obj.location[0] = 0
obj.keyframe_insert('location', index=0)  
obj.location[0] = 10
obj.keyframe_insert(data_path="location",index=0,frame=10) 
bpy.context.scene.frame_end = 10
bpy.context.scene.render.resolution_x = 200
bpy.context.scene.render.resolution_y = 150
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.render.filepath = "/home/shiyanlou/Code/o"
bpy.ops.render.render(animation=True)
  • 观察

图片描述

  • 有没有能够看图片序列的应用呢?

安装应用

sudo apt update
yes | sudo apt install feh
feh /home/shiyanlou/Code

  • 上下控制缩放
  • 左右控制帧号

图片描述

  • 最终图片序列
    • 可以下载得到

图片描述

  • 但是目前有个问题

看不见了

  • 从6到10
    • 这几帧都看不到了

图片描述

  • 摄影机位置
    • 需要调整

重修调整摄影机

  • 先调整当前活跃视角
    • active view

图片描述

  • 再将摄影机视角
    • 对齐到
    • 活跃视角
    • active view

图片描述

  • 可以直接渲染成视频文件吗?

渲染成视频

  • 控制分辨率
    • 帧数
import bpy

# 设置渲染路径和渲染动画
bpy.context.scene.render.filepath = "/tmp/house_animation"
bpy.context.scene.render.image_settings.file_format = 'FFMPEG'
bpy.ops.render.render(animation=True)
  • 做一个晴天娃娃的动画吧

晴天娃娃

import bpy

bpy.ops.object.select_all(action="SELECT") 
bpy.ops.object.delete() 
bpy.ops.mesh.primitive_uv_sphere_add()
head = bpy.context.object
bpy.context.object.name  = "head"
r_eye = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "r_eye"
bpy.context.object.location = (0.7, 0.5, 0.3)
bpy.context.object.scale = (0.3, 0.3, 0.3)
mat = bpy.data.materials.new('mat_eye')
color = (0, 0, 0, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = head
l_eye = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "l_eye"
bpy.context.object.location = (0.7, -0.5, 0.3)
bpy.context.object.scale = (0.3, 0.3, 0.3)
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = head
bpy.ops.mesh.primitive_cone_add()
body = bpy.context.object
body.name  = "body"
body.location = (0,0,-1)
body.scale = (1,1,2)
character = bpy.data.objects.new("character", None)
bpy.data.collections["Collection"].objects.link(character)
head.parent = character
body.parent = character
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 = 50  # 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.data.lights["Spot"].energy = 1000
bpy.context.object.location = (6.27,-3.4,2.83)
bpy.context.object.rotation_euler = (1.172,0,0.907)

character.location = (0, 0, 0) 
character.keyframe_insert(data_path="location")  
character.location = (10, 0, 0)
character.keyframe_insert(data_path="location", frame=10) 

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.ops.render.render(animation=True)
  • 渲染成功

图片描述

  • 角色真的跑了

动画综合案例

图片描述

import bpy
from math import pi,cos,sin
import random

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

#制作土地
bpy.ops.mesh.primitive_plane_add(size=100, enter_editmode=False, align='WORLD', location=(25, 25, -2.5), scale=(1, 1, 20))
black = bpy.data.materials.new('black')
color = (0.1, 0.1, 0.1 ,1 )
black.diffuse_color = color
bpy.context.object.data.materials.append(black)

        
#选择花田的花朵的数量
size = 10
for n1 in range(size):
    for n2 in range(size):
        flower = bpy.data.objects.new("flower", None)
        bpy.data.collections["Collection"].objects.link(flower)
        '''
        #history version
        #如何实现多层封装,这样可以控制花的上部分随机方向转动
        #flower_ban = bpy.data.objects.new("flower_ban", None)
        #bpy.data.collections["Collection"].objects.link(flower_ban)
        #bpy.context.object.parent = flower
        '''


        #制作花心、花杆、花叶1、花叶2
        h = random.random()
        h = h + 3
        bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(0, 0, h), scale=(1.5, 1.5, 0.3))
        bpy.context.object.name = "flower_xin"
        brown = bpy.data.materials.new('brown')
        color = (0.7 ,0.3 ,0.06 ,1 )
        brown.diffuse_color = color
        bpy.context.object.data.materials.append(brown)
        bpy.context.object.active_material.metallic = 1
        bpy.context.object.active_material.specular_intensity = 1
        bpy.context.object.active_material.roughness = 1        
        bpy.context.object.parent = flower
        
        bpy.ops.mesh.primitive_cylinder_add(radius=1, depth=2, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(0.2, 0.2, h))
        bpy.context.object.name = "flower_gan"
        green_dark = bpy.data.materials.new('green_dark')
        color = (0,0.5,0.02,1 )
        green_dark.diffuse_color = color
        bpy.context.object.data.materials.append(green_dark)
        bpy.context.object.active_material.metallic = 1
        bpy.context.object.active_material.specular_intensity = 1
        bpy.context.object.active_material.roughness = 1
        bpy.context.object.parent = flower
        
        '''
        #没法选择融球
        #bpy.ops.object.metaball_add(type='ELLIPSOID', enter_editmode=False, align='WORLD', location=(0, 0.8, 1), scale=(0.4, 0.1, 1))
        '''
        
        bpy.ops.mesh.primitive_uv_sphere_add(enter_editmode=False, align='WORLD', location=(0, 0.8, 0.8), scale=(0.5, 0.1, 1.2))
        bpy.context.object.rotation_euler[0] = -40/360 * 2* pi
        bpy.context.object.name = "flower_ye1"
        green = bpy.data.materials.new('green')
        color = (0.2,0.6,0.05,1 )
        green.diffuse_color = color
        bpy.context.object.data.materials.append(green)
        bpy.context.object.active_material.metallic = 1
        bpy.context.object.active_material.specular_intensity = 1
        bpy.context.object.active_material.roughness = 1
        bpy.context.object.parent = flower
        
        '''
        #bpy.ops.object.metaball_add(type='ELLIPSOID', enter_editmode=False, align='WORLD', location=(0, -0.8, 1), scale=(0.4, 0.1, 1))
        '''
        
        bpy.ops.mesh.primitive_uv_sphere_add(enter_editmode=False, align='WORLD', location=(0, -0.8, 0.8), scale=(0.5, 0.1, 1.2))
        bpy.context.object.rotation_euler[0] = 40/360 * 2* pi
        bpy.context.object.name = "flower_ye2"
        bpy.context.object.data.materials.append(green)
        bpy.context.object.active_material.metallic = 1
        bpy.context.object.active_material.specular_intensity = 1
        bpy.context.object.active_material.roughness = 1
        bpy.context.object.parent = flower

        #制作花瓣
        r = 2
        num = 11
        w = random.random()
        for i in range(num):
            theta = i * pi * 2/num
            pos = (r*cos(theta) , r*sin(theta) , h)
            bpy.ops.mesh.primitive_cone_add(enter_editmode=False, align='WORLD', location=pos, scale=(1, 0.3, 0.6))
            bpy.context.object.name = "flower_ban"
            bpy.context.object.parent = flower
            #根据与花朵中心的位置关系,每个花瓣z轴方向旋转不同度
            bpy.context.object.rotation_euler[2] = (i * pi * 2/num) + 1.57
            #每个花瓣x轴旋转随机角度使之平躺 #所有的操作都是以自己为中心的
            bpy.context.object.rotation_euler[0] = 1.57-0.2*(w+1)
            yellow = bpy.data.materials.new('yellow')
            color = (1 ,0.8 ,0 ,1 )
            yellow.diffuse_color = color
            bpy.context.object.data.materials.append(yellow)
            bpy.context.object.active_material.metallic = 1
            bpy.context.object.active_material.specular_intensity = 1
            bpy.context.object.active_material.roughness = 1




        l1 = random.random()
        '''
        #l2 = random.random()# history version
        #位置随机离太近会穿模,应该使用粒子模型
        '''
        flower.location.x = n1 * (r * 1.8 + 1) #-(l1-0.5)
        flower.location.y = n2 * (r * 1.8 + 1) #-(l2-0.5)
        #随机转角度
        flower.rotation_euler[2] = (w-0.5)*pi
        #随即大小,花的长度与花的大小成正比
        flower.scale = (l1*0.4+0.8 ,l1*0.4+0.8,l1*0.4+0.8)
        
        # 插入关键帧,制作动画
        key_point = 20
        for t in range(key_point):
            bpy.context.scene.frame_set(20*t)
            s = random.random()
            rotation_angle_x = (s - 0.5) * 0.02 * pi
            rotation_angle_y = (s - 0.5) * 0.02 * pi
            flower.rotation_euler.rotate_axis('X', rotation_angle_x)
            flower.rotation_euler.rotate_axis('Y', rotation_angle_y)
            flower.keyframe_insert(data_path="rotation_euler", index=0)  # X轴
            flower.keyframe_insert(data_path="rotation_euler", index=1)  # Y轴
            

bpy.ops.object.light_add(type='SPOT', align='WORLD', location=(20, -8, 25), scale=(1, 1, 1))
bpy.context.object.rotation_euler[0] = 0.785
bpy.context.object.rotation_euler = (0.785, 0.785 ,0)
bpy.ops.object.camera_add(enter_editmode=False, align='VIEW', location=(4, 9, 15), rotation=(0.872, 0, -1.046), scale=(1, 1, 1))
bpy.context.object.data.type = 'ORTHO'
bpy.context.object.data.ortho_scale = 35

总结 🤔

  • 这次 我们
    • 设置了 关键帧
    • 制作了 动画
    • 并且渲染出了 动画序列
  • 我们可以设置变化的灯光吗?🤔

图片描述

  • 我们下次再说!👋