Skip to content

Latest commit

 

History

History
300 lines (231 loc) · 7.79 KB

File metadata and controls

300 lines (231 loc) · 7.79 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 0692
  • 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。

用聚光灯_spot_light_功率_控制灯光效果

开始

  • 配套视频
  • 上次我们
    • 设置了 渲染 参数
      • 大小
  • 以前的人物 渲染出来
    • 没有灯光
    • 效果不好

图片描述

  • 需要来一盏灯!🤔

整合

  • 设置 渲染场景的摄影机
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
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.context.scene.render.resolution_x = 640
bpy.context.scene.render.resolution_y = 480
bpy.context.scene.render.resolution_percentage = 50
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.render.filepath = './render2.png'
bpy.ops.render.render(write_still=True)
  • 执行成功

图片描述

  • 现在来
    • 添加 光照

手动加灯

图片描述

  • shift + a
    • 添加一盏聚光灯
    • spot light

图片描述

找到代码

图片描述

  • 将代码复制到游乐场
bpy.ops.object.light_add(type='SPOT', radius=1, location=(0, 0, 0))
  • 这灯怎么照比较合适呢?

灯照效果

  • 先选中
    • 聚光灯
    • spot light

图片描述

  • 将活动对象
    • 也就是聚光灯spot light
    • 设置为 活跃摄影机
    • Active Camera

图片描述

  • 假装 这个灯 是一个活动摄影机

控制视角

  • 旋转 视角
    • 从 侧上方 拍摄娃娃
  • 于是 这盏聚光灯
    • 从 侧上方 照着娃娃

图片描述

  • Align View 对齐视图
    • 设置活动摄影机 对齐到 当前视图

图片描述

效果

  • 也就是
    • 设置 聚光灯(spot light)
    • 对齐 到当前视图

图片描述

  • 摄影机位置、旋转
    • 在对齐的一刻
    • 发生了改变

渲染一张

  • F12
  • 观看效果

图片描述

  • 聚光灯 功率不够

提升功率

图片描述

  • 效果

图片描述

  • 摄影机角度
    • 和聚光灯角度
    • 太过一致
    • 看不到影子
  • 原因是现在聚光灯
    • 既当聚光灯
    • 又当摄影机

设置摄影机

  • 在大纲视图选中摄影机
    • 设置当前活跃对象为摄影机
    • 就是设置摄影机为摄影机

图片描述

  • 现在这个状态还可以

图片描述

  • 聚光灯坐标

整合代码

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
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.context.object.data.energy = 1000
bpy.context.object.location = (6.27,-3.4,2.83)
bpy.context.object.rotation_euler = (1.172,0,0.907)
bpy.context.scene.render.resolution_x = 640
bpy.context.scene.render.resolution_y = 480
bpy.context.scene.render.resolution_percentage = 50


# Set the render engine (e.g., CYCLES, BLENDER_EEVEE)
bpy.context.scene.render.engine = 'CYCLES'
# Set the output file path
bpy.context.scene.render.filepath = './render2.png'
# Render the current view
bpy.ops.render.render(write_still=True)
  • 最终效果

图片描述

需要注意

  • 如果反复不能成功

    • 就新建一个blender工程
    • 然后再跑脚本
  • 可能的原因是

    • Current file 里面已经有了Spot Light 聚光灯
    • 但是后来被删除了
bpy.data.lights["Spot"].energy = 1000
  • 新聚光灯 Spot.001 没有设置亮度
    • 设置的还是老聚光灯的亮度

总结 🤔

  • 这次通过python设置了舞台基本要素
    • 灯光
    • 物体
    • 摄像机
  • 并且将结果渲染为一张png
  • 有什么优秀作品可以看一下吗??🤔
  • 我们下次再说!👋
  • 配套视频

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