Skip to content

Latest commit

 

History

History
271 lines (209 loc) · 7.18 KB

648-2552168-灯光效果_聚光灯_spot_light_功率.sy.md

File metadata and controls

271 lines (209 loc) · 7.18 KB
show version enable_checker
step
1.0
true

python_blender

开始

  • 上次我们
    • 设置了 渲染 参数
      • 大小
  • 以前的人物 渲染出来
    • 没有灯光
    • 效果不好
  • 需要来一盏灯!🤔

图片描述

整合

  • 设置 渲染场景的摄影机
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.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
    • 对齐 到当前视图

渲染一张

  • 观看效果

图片描述

  • 聚光灯 功率不够

提升功率

图片描述

  • 效果

图片描述

  • 摄影机角度
    • 和聚光灯角度
    • 太过一致
    • 光影效果不好
  • 原因是现在聚光灯
    • 既当聚光灯
    • 又当摄影机

设置摄影机

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

图片描述

  • 现在这个状态还可以

图片描述

  • 聚光灯坐标

整合代码

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.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
  • 有什么优秀作品可以看一下吗??🤔
  • 我们下次再说!👋