Skip to content

Latest commit

 

History

History
394 lines (293 loc) · 9.3 KB

File metadata and controls

394 lines (293 loc) · 9.3 KB
Error in user YAML: (<unknown>): did not find expected key while parsing a block mapping at line 1 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 0689
  • 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。

晴天娃娃的制作_设置位置和旋转

开始

  • 配套视频
  • 上次我们
    • 深入理解了清场
    • 原来只是删除了对象
    • 并没有删除实际的
      • 网格
      • 灯光
      • 摄影机

图片描述

  • 想做个晴天娃娃
    • 怎么做呢?🤔

制作三个球体

  • 白色的为头部
  • 黑色的为眼球

图片描述

  • 眼球 参数
    • 一会儿要用

图片描述

  • 如何使用代码生成?

代码生成头部

import bpy

bpy.ops.object.select_all(action="SELECT") 
bpy.ops.object.delete() 
head = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "head"
  • 生成头部

图片描述

生成左眼

import bpy

bpy.ops.object.select_all(action="SELECT") # 选择所有物体
bpy.ops.object.delete() # 删除选定的物体
head = bpy.ops.mesh.primitive_uv_sphere_add()
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)
  • 效果

图片描述

  • 准备上色

图片描述

上色

import bpy

bpy.ops.object.select_all(action="SELECT") 
bpy.ops.object.delete()
head = bpy.ops.mesh.primitive_uv_sphere_add()
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')
mat.use_nodes = True
color = (0, 0, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)
  • 效果

图片描述

另一只眼睛

import bpy

bpy.ops.object.select_all(action="SELECT") 
bpy.ops.object.delete() 
head = bpy.ops.mesh.primitive_uv_sphere_add()
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')
mat.use_nodes = True
color = (0, 0, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

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)
  • 红眼睛的效果

图片描述

  • 可以控制层次结构吗?

层次结构

  • 左右眼对象
    • 隶属于 脑袋
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.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = 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
  • 效果

图片描述

  • 左眼、右眼 两个对象
    • 成为 head 的字对象

增加一个身体

  • 添加一个圆锥体

图片描述

  • 并修改参数

图片描述

  • 将添加 身体圆锥 的操作
    • 转化为 代码

代码化

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')
mat.use_nodes = True
color = (0, 0, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = 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)
  • 可以加上 双手和双脚吗?
  • 可以带上 帽子 或者 眼镜吗?
  • 可以 添加 鼻子和耳朵吗?
  • 这个任务就交给你了

再封装

  • 现在场景中有两个对象
    • head
    • body

图片描述

  • 可以把身体和头部
    • 整合成一个 角色 吗?

再封装

  • 把head、body
    • 封装进 character
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')
mat.use_nodes = True
color = (0, 0, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = 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
  • 制作成功

图片描述

  • 可以封装成函数吗?

函数

import bpy

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

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_eye(name, location, parent):
    bpy.ops.mesh.primitive_uv_sphere_add()
    eye = bpy.context.object
    eye.name = name
    eye.location = location
    eye.scale = (0.3, 0.3, 0.3)
    
	mat = bpy.data.materials.new('mat_eye')
	mat.use_nodes = True
	color = (0, 0, 0, 1)
	mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
	bpy.context.object.data.materials.append(mat)
    
    eye.parent = parent
    return eye

def create_character():
    # Create empty parent object
    character = bpy.data.objects.new("character", None)
    bpy.data.collections["Collection"].objects.link(character)
    
    # Create head
    bpy.ops.mesh.primitive_uv_sphere_add()
    head = bpy.context.object
    head.name = "head"
    head.parent = character
    
    # Create eyes
    create_eye("r_eye", (0.7, 0.5, 0.3), head)
    create_eye("l_eye", (0.7, -0.5, 0.3), head)
    
    # Create body
    bpy.ops.mesh.primitive_cone_add()
    body = bpy.context.object
    body.name = "body"
    body.location = (0, 0, -1)
    body.scale = (1, 1, 2)
    body.parent = character
    
    return character

if __name__ == "__main__":
    clear_scene()
    create_character()

更多可能性

  • 双腿

图片描述

双脚

图片描述

帽子

图片描述

翅膀

图片描述

参数化生成毛毛虫

图片描述

总结 🤔

  • 这次我们制作了
    • 晴天娃娃
  • 可以控制摄像机
    • 从不同角度拍摄他吗?
  • 我们下次再说!👋
  • 配套视频

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