LoginSignup
2
1

More than 1 year has passed since last update.

Houdiniのよく使うノード群をテンプレート化して使い回す

Posted at

はじめに

よく使うノード群を保存しておいて好きな時に呼び出して使い回せるように、シェルフに以下の2つのスクリプトを登録します。

シェルフへの登録の仕方はこちらです。

コード

選択したノード群を保存するコード

Save_Nodes.py

import hou

# cpioを保存するディレクトリを設定
dirPath = "DIRECTORY"

def SaveTemplate(name):

    sel = hou.selectedNodes()
    path = sel[0].path()
    nodeCategoryName = sel[0].type().category().name()
    pathSplit = path.split("/")
    pathSplit.pop(-1)
    
    getRootPath = "/".join(pathSplit)
    rootName = getRootPath[1:].replace("/","_")
    
    contextnode = hou.node(getRootPath)
    
    filename = "{}/{}_{}".format(dirPath, nodeCategoryName, name + ".cpio")
    
    contextnode.saveItemsToFile(sel, filename, save_hda_fallbacks = False)
    hou.ui.displayMessage("Success!\n" + "Save File: "+filename)
    print("Save File: "+filename)


Dialog = hou.ui.readInput(message ="Save Select Nodes?\n",title = "Save Template",severity=hou.severityType.Message,buttons=["Save","Cancel"])

if Dialog[0]==0:
    SaveTemplate(Dialog[1])

説明

houにあるsaveItemsToFileなどを使用してノード群をcpioファイルとして保存しています。


ノードを読み込むコード

Import_Nodes.py

import hou
import os

# cpioを保存してあるディレクトリを設定
dirPath = "DIRECTORY"

files = os.listdir(dirPath)
fileList = [f for f in files if os.path.isfile(os.path.join(dirPath, f))]

def ImportTemplate(filename):
    desktop = hou.ui.curDesktop()
    pane =  desktop.paneTabOfType(hou.paneTabType.NetworkEditor)
    current_context = pane.pwd().path()
    
    hou.clearAllSelected()
    
    contextnode = hou.node(current_context)
    nodes = contextnode.loadItemsFromFile(filename, ignore_load_warnings=False)
    sel = hou.selectedNodes()
    
    firstNodePos = sel[0].position()
    for node in sel:
        node.setPosition(
        node.position() + hou.ui.paneTabOfType(hou.paneTabType.NetworkEditor).visibleBounds().center() - firstNodePos
        )

Dialog = hou.ui.selectFromList(fileList, message='Select Import File')
 
if len(Dialog)!=0:
    for i in Dialog:
        ImportTemplate("{}/{}".format(dirPath, fileList[i]))

解説

ディレクトリからcpioのファイルを読み込んでloadItemsFromFileでcpioファイルからHoudiniにノードをペーストしています。

読み込んだノード群は、ビューワーの中央になるように調整しています。

2
1
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
2
1