【Blender×Python】コレクションデータをシーンデータから取得する方法について徹底解説

Blender

「bpy」とは、PythonからBlenderの機能を呼び出すことができるBlender Python APIのことです。Blenderはオープンソースの3Dアニメーションソフトウェアであり、「bpy」を使うことでBlenderの様々な機能をPythonスクリプトから呼び出して利用することができます。本記事では、「bpy」を用いたコレクションデータをシーンデータから取得する方法を紹介します。

# collectionプロパティによるシーンデータを用いたコレクションデータの取得
"Scene".collection

下記の様な内容で悩んでいる/困っている場合に使える方法を参考までにご共有させて頂きます。

・Blender上で、どの様にPythonを使うの?
・Blender上のPythonで、どの様にコレクションデータをシーンデータから取得するの?

また、「bpy」を使用する上で基礎的な情報は下記の記事で紹介しております。
他の「bpy」のクラスや関数について気になる方はこちらの記事をご覧ください。

collectionプロパティによるシーンデータを用いたコレクションデータの取得

“Scene”.collectionプロパティの基本構文

"Scene".collection
→ Collection: bpy_types.Collection

“Scene”.collectionプロパティの使い方

import bpy

SampleScene    = bpy.data.scenes["Scene"]
MainCollection = SampleScene.collection

print(f"Main Collection:\n{MainCollection} {type(MainCollection)}")

SceneCollections = MainCollection.children
print(f"Scene Collection:\n{SceneCollections} {type(SceneCollections)}")
for key, value in SceneCollections.items():
    print(f"[{key}] {value}")

SceneCollectionList = [SceneCollection for SceneCollection in SceneCollections]
print(f"Scene Collection List:\n{SceneCollectionList}")
Main Collection:
<bpy_struct, Collection("Scene Collection") at 0x000001A037E32688> <class 'bpy_types.Collection'>
Scene Collection:
<bpy_collection[2], CollectionChildren> <class 'bpy_prop_collection'>
[Collection] <bpy_struct, Collection("Collection") at 0x000001A037E32508>
[Camera] <bpy_struct, Collection("Camera") at 0x000001A037E32B08>
Scene Collection List:
[bpy.data.collections['Collection'], bpy.data.collections['Camera']]

まとめ

「bpy」(Blender Python API)は、Pythonを使ったBlenderの開発に非常に役立ちます。この記事では、コレクションデータをシーンデータから取得する方法を紹介しました。ぜひ活用してみてください。

関連検索ワード

How to get collection data from scene data?

関連キーワード

blender, python, get, collection, scene, 取得, コレクション, シーン