bpy.context.scene
是 Blender 中的一个 Python 对象,代表当前场景。通过它,我们可以访问场景中的各种对象,比如物体、灯光等等,也可以设置一些场景相关的属性。
我们可以通过 bpy.context.scene
对象访问场景中的各种对象,比如:
# 获取当前场景中的所有物体
for obj in bpy.context.scene.objects:
print(obj.name)
# 获取当前场景中的所有灯光
for lamp in bpy.context.scene.lights:
print(lamp.name)
这里的 objects
和 lights
都是 bpy.types.Scene
类中的属性,分别代表场景中的物体和灯光。
我们也可以通过 bpy.context.scene
对象设置场景的各种属性,比如:
# 设置场景的单位为英尺
bpy.context.scene.unit_settings.system = 'IMPERIAL'
# 设置场景的帧率为 30 帧/秒
bpy.context.scene.render.fps = 30
这里的 unit_settings
和 render
都是 bpy.types.Scene
类中的属性,分别代表场景的单位设置和渲染设置。
下面列举一些常用的 bpy.context.scene
属性:
bpy.context.scene.name
:场景的名称(字符串类型)bpy.context.scene.objects
:场景中的所有物体(bpy.collections.Collection
类型)bpy.context.scene.lights
:场景中的所有灯光(bpy.collections.Collection
类型)bpy.context.scene.camera
:场景中当前的摄像机(bpy.types.Object
类型)bpy.context.scene.render.engine
:当前的渲染引擎(字符串类型,比如 BLENDER_EEVEE
或 CYCLES
)bpy.context.scene.render.resolution_x
和 bpy.context.scene.render.resolution_y
:渲染输出的分辨率bpy.context.scene.eevee.use_ssr
:是否启用屏幕空间反射(布尔类型)更多属性可以查看 Blender 的 Python API 文档。
在修改 bpy.context.scene
对象的属性时,最好在操作之前保存一下当前场景的状态,以免修改后无法恢复。
另外,当我们在 Blender 中切换场景时,bpy.context.scene
对象也会随之改变。因此,在访问或修改 bpy.context.scene
对象时,最好先检查一下当前场景是否是我们要操作的那个场景。