Houdini Pythonでノードの検索と選択(備忘録2)
ノードを検索して、そのノードを選択状態に
Houdiniでの繰り返し作業を自動化する上で、ノード検索とその選択は頻繁に行いそうです。そこで今回は、Houdini Pythonでノードの検索と選択、ついでにNetwork Editor上でのフォーカスはどう書くのかを調べてみました。このスクリプトは、シェルフツールとして使用することを想定しています。
環境
Windows10 Pro
Houdini Indie 20.5.332 py3
指定した名前のノードを検索
import hou
# Specify the name of the node to find
node_name = "null*"
# Get the selected node
selected_nodes = hou.selectedNodes()
# Clear Selection
for node in selected_nodes:
node.setSelected(False)
if selected_nodes:
# Get the parent node
parent_node = selected_nodes[0].parent()
if parent_node:
# Find nodes matching node_name
matching_nodes = parent_node.glob(node_name)
if matching_nodes:
print("Nodes in the same hierarchy:")
for node in matching_nodes:
print(node.path())
# Select found nodes in Network Editor
node.setSelected(True)
else:
print("No nodes found in the same hierarchy.")
else:
print("Selected node has no parent node.")
else:
print("Please select nodes.")
選択されているノードと同じ階層の中から名前でノードを検索するスクリプトを作成しました。
node_nameという変数の文字列を使ってノードを検索します。必要に応じてこの文字列を変更して検索できます。ノードが見つかると、Network Editor上でそのノードが選択状態になります。
将来的にはダイアログを表示させて、テキスト入力によって検索する機能を追加したいですね。Network Editorの右上にあるFind Nodeボタンという既存の検索機能があるので、このスクリプトの必要性は低いですが、より複雑なスクリプトの一部として組み込むことを想定して作成しました。
node_nameのところを任意の文字列に変更
検索したい階層のノードを選択
スクリプトを実行すると、名前でマッチしたノードが選択状態に
ネットワークエディター上で選択ノードにフォーカス
import hou
selected_nodes = hou.selectedNodes()
if selected_nodes:
# Get the pane tab of the Network Editor
pane = hou.ui.paneTabOfType(hou.paneTabType.NetworkEditor)
# Initialize variables
min_x = 0.0
min_y = 0.0
max_x = 0.0
max_y = 0.0
# Find min/max positions
for node in selected_nodes:
pos = node.position()
min_x = min(min_x, pos[0])
min_y = min(min_y, pos[1])
max_x = max(max_x, pos[0])
max_y = max(max_y, pos[1])
# Set BoundingRect
bound = hou.BoundingRect(hou.Vector2(min_x, min_y), hou.Vector2(max_x, max_y))
# Set Network Editor view (with optional padding)
padding = hou.Vector2(2, 2)
bound_with_padding = hou.BoundingRect(bound.min() - padding, bound.max() + padding)
pane.setVisibleBounds(bound_with_padding, 0.2)
else:
print ("Please select nodes.")
このスクリプトは、選択中のノードをNetwork Editor上でフォーカス表示します。Houdiniにはすでに用意されているショートカットのFがあり、選択中のノードをNetwork Editorの中央に表示する機能があります。
ショートカットFで事足りますが、何らかのスクリプトを実行したついでに働くよう、スクリプトに組み込むことを想定して作成しました。F押すのも面倒くさいときもありますよね。
Network Editor上で大きく表示したいノードを選択
実行すると、ショートカットのFを押したときと同様にフォーカスされます
まとめ
お読みいただきありがとうございました。Houdini Pythonについては学習を始めたばかりなため、拙いところもあるかと思います。少しでもお役に立てれば幸いです。
参考
Discussion