0
0

More than 1 year has passed since last update.

Blenderでシェーダーのノード位置の調整

Last updated at Posted at 2022-04-26

概要

Pythonでマテリアルのシェーダーのノード位置を調整してみます。
シェーダーのノード自体をPythonで作成したときに使えるかもしれません(下図は実行例)。

ロジック

NetworkXで有向グラフを作り、マテリアル出力ノードまでの距離ごとにレイヤーを作り、上から配置していきます。

実行してみる

対象のマテリアルの名前をMaterialとしたとき、下記を実行すると位置を調整します。
NetworkXのインストールが必要です。

import bpy
import networkx as nx
from collections import defaultdict

# 有向グラフの作成
g = nx.DiGraph()
m = bpy.data.materials["Material"]
for link in m.node_tree.links:
    g.add_edge(link.from_node, link.to_node)

# 出力までの距離を算出
mo = m.node_tree.nodes["Material Output"]
dist = nx.shortest_path_length(g, None, mo)

posy = defaultdict(int)  # 現在のY座標
for nd in m.node_tree.nodes:
    d = dist[nd]  # ノードの距離(レイヤー)
    nd.location = -d * 300, posy[d]
    posy[d] -= 300

ノードの幅と高さの属性が(有効な値として)使えなかったので固定値にしています。

参考:BlenderでPythonを実行する方法

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0