【Blender Python】5.Blender场景中的集合

概述

这里的"集合"是指Blender场景中的集合。你可以在"大纲视图"面板中看到 图标的,就是集合,可以看做是文件夹,用于分类和整理场景中的对象。

获取场景的集合

python 复制代码
>>> C.scene
bpy.data.scenes['Scene']
python 复制代码
>>> C.scene.collection.name
'Scene Collection'

获取集合的对象

python 复制代码
>>> list(C.scene.collection.objects)
[]

可以看到,"场景集合"下没有直接的子对象,所以返回一个空集。

python 复制代码
>>> list(C.scene.collection.all_objects)
[bpy.data.objects['Cube'], bpy.data.objects['Light'], bpy.data.objects['Camera']]

all_objects会忽略层级关系,将所有层级中的对象全部列出。

使用具体名称获取集合下的对象

python 复制代码
>>> D.collections["Collection"].objects
bpy.data.collections['Collection'].objects

>>> list(D.collections["Collection"].objects)
[bpy.data.objects['Cube'], bpy.data.objects['Light'], bpy.data.objects['Camera']]

遍历输出集合下的对象名称

python 复制代码
>>> objs = D.collections["Collection"].objects
>>> for obj in objs:
...     print(f"obj name is:{obj.name}" )
...     
obj name is:Cube
obj name is:Light
obj name is:Camera

这里,使用的Python的格式化字符串f-string形式。

创建和添加新的集合

python 复制代码
>>> c = D.collections.new("c2")              # 创建一个新的集合,并命名为c2
>>> C.scene.collection.children.link(c)      # 添加到场景的大纲视图

总结

本节介绍Blender中的集合,以及在Blender Python中的获取以及创建。内容不详之处,后续改进。

相关推荐
ZhengEnCi1 天前
M3-markconv库找不到wkhtmltopdf问题
python
2301_764441331 天前
LISA时空跃迁分析,地理时空分析
数据结构·python·算法
chushiyunen1 天前
python rest请求、requests
开发语言·python
cTz6FE7gA1 天前
Python异步编程:从协程到Asyncio的底层揭秘
python
baidu_huihui1 天前
在 CentOS 9 上安装 pip(Python 的包管理工具)
开发语言·python·pip
南 阳1 天前
Python从入门到精通day63
开发语言·python
lbb 小魔仙1 天前
Python_RAG知识库问答系统实战指南
开发语言·python
FreakStudio1 天前
MicroPython LVGL基础知识和概念:底层渲染与性能优化
python·单片机·嵌入式·电子diy
素玥1 天前
实训5 python连接mysql数据库
数据库·python·mysql