【Blender×Python】コンテキストなビューレイヤーデータを取得する方法について徹底解説

Blender

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

# view_layerプロパティによるコンテキストなビューレイヤーデータの取得
bpy.context.view_layer

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

・Blender上で、どの様にPythonを使うの?
・Blender上のPythonで、どの様にコンテキストなビューレイヤーデータを取得するの?

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

view_layerプロパティによるコンテキストなビューレイヤーデータの取得

bpy.context.view_layerプロパティの基本構文

bpy.context.view_layer
→ ViewLayer: bpy.types.ViewLayer

bpy.context.view_layerプロパティの使い方

import bpy

ContextViewLayer = bpy.context.view_layer
print(f"Context View Layer:\n{ContextViewLayer} {type(ContextViewLayer)}")

print(ContextViewLayer.name)
print(ContextViewLayer.objects)
print(ContextViewLayer.layer_collection)
print(ContextViewLayer.layer_collection.collection)
print(ContextViewLayer.layer_collection.collection.children)
print(ContextViewLayer.eevee)
print(ContextViewLayer.cycles)

for key, value in ContextViewLayer.layer_collection.collection.children.items():
    print(f"[{key}] {value}")
Context View Layer:
<bpy_struct, ViewLayer("View Layer") at 0x00000238E2B92808> <class 'bpy.types.ViewLayer'>
View Layer
<bpy_collection[3], LayerObjects>
<bpy_struct, LayerCollection("Scene Collection") at 0x00000238E246D0D8>
<bpy_struct, Collection("Scene Collection") at 0x00000238E2B92688>
<bpy_collection[2], CollectionChildren>
<bpy_struct, ViewLayerEEVEE at 0x00000238E2B928F8>
<bpy_struct, CyclesRenderLayerSettings("") at 0x00000238E2479A88>
[Collection] <bpy_struct, Collection("Collection") at 0x00000238E2B92508>
[Camera] <bpy_struct, Collection("Camera") at 0x00000238E2B92B08>

まとめ

「bpy」(Blender Python API)は、Pythonを使ったBlenderの開発に非常に役立ちます。この記事では、コンテキストなビューレイヤーデータを取得する方法を紹介しました。ぜひ活用してみてください。

関連検索ワード

How to get a context view layer data?

関連キーワード

blender, python, get, context, view layer, 取得, コンテキスト, ビューレイヤー