【Blender×Python】マテリアルデータを名称で取得する方法について徹底解説

Blender

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

# materialsプロパティによる名称を用いたマテリアルデータの取得
bpy.data.materials[MATERIAL_NAME]

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

・Blender上で、どの様にPythonを使うの?
・Blender上のPythonで、どの様にマテリアルデータを名称で取得するの?

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

materialsプロパティによる名称を用いたマテリアルデータの取得

bpy.data.materialsプロパティの基本構文

bpy.data.materials[MATERIAL_NAME: str]
→ Material: bpy.types.Material

bpy.data.materialsプロパティの使い方

import bpy

SampleMaterial = bpy.data.materials["Material"]
print(f"Material:\n{SampleMaterial} {type(SampleMaterial)}")

print(SampleMaterial.name)
print(SampleMaterial.name_full)
print(SampleMaterial.node_tree)
print(SampleMaterial.node_tree.nodes)
print(SampleMaterial.node_tree.links)

for key, value in SampleMaterial.node_tree.nodes.items():
    print(f"[{key}] {value}, {value.type}, {[value_input.name for value_input in value.inputs]}, {[value_output.name for value_output in value.outputs]}")

for key, value in SampleMaterial.node_tree.links.items():
    print(f"[{key}] {value} {value.from_node.name} ({value.from_socket.name}) -> {value.to_node.name} ({value.to_socket.name})")
Material:
<bpy_struct, Material("Material") at 0x000001A043186208> <class 'bpy.types.Material'>
Material
Material
<bpy_struct, ShaderNodeTree("Shader Nodetree") at 0x000001A037FD0B88>
<bpy_collection[3], Nodes>
<bpy_collection[2], NodeLinks>
[Principled BSDF] <bpy_struct, ShaderNodeBsdfPrincipled("Principled BSDF") at 0x000001A043056948>, BSDF_PRINCIPLED, 
['Base Color', 'Subsurface', 'Subsurface Radius', 'Subsurface Color', 'Subsurface IOR', 
  'Subsurface Anisotropy', 'Metallic', 'Specular', 'Specular Tint', 'Roughness', 
  'Anisotropic', 'Anisotropic Rotation', 'Sheen', 'Sheen Tint', 
  'Clearcoat', 'Clearcoat Roughness', 'IOR', 'Transmission', 'Transmission Roughness', 
  'Emission', 'Emission Strength', 'Alpha', 'Normal', 'Clearcoat Normal', 
  'Tangent', 'Weight'], ['BSDF']
[Material Output] <bpy_struct, ShaderNodeOutputMaterial("Material Output") at 0x000001A043056788>, OUTPUT_MATERIAL, ['Surface', 'Volume', 'Displacement', 'Thickness'], []
[Image Texture] <bpy_struct, ShaderNodeTexImage("Image Texture") at 0x000001A04D695D08>, TEX_IMAGE, ['Vector'], ['Color', 'Alpha']
[0] <bpy_struct, NodeLink at 0x000001A037FCE848> Principled BSDF (BSDF) -> Material Output (Surface)
[1] <bpy_struct, NodeLink at 0x000001A04C7F2408> Image Texture (Color) -> Principled BSDF (Base Color)

まとめ

「bpy」(Blender Python API)は、Pythonを使ったBlenderの開発に非常に役立ちます。この記事では、マテリアルデータを名称で取得する方法を紹介しました。ぜひ活用してみてください。

関連検索ワード

How to get a material data by name?

関連キーワード

blender, python, get, material, name, 取得, マテリアル, 名称, 名前