0%

Houdini Python 快速开始

摘要

Houdini的Python耦合性比较高, 有些方法没有那么自觉.

放几个简单案例用来快速开始使用~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#coding=utf-8
import hou
#############################################
# 获取与设置节点属性
#############################################

def get_and_set_node_parameters(node_name=""):
print("\n------------\n")
print("Start Create!\n")

current_root_node = hou.node("/obj")# type: hou.Node
geo_node = current_root_node.createNode("geo") #type: hou.ObjNode
if node_name:
if not hou.node("{}/{}".format(current_root_node.path(), node_name)):
geo_node.setName(node_name)
geo_node.setSelected(True, clear_all_selected=True) #替换选择
# NOTE: 在此节点下创建其它节点, 不需要选中, 以上选中仅用于测试.
# NOTE: run_init_scripts的作用主要是在Geometry中内置一个File SOP
# 如果设置为了False就不会做这个事情了
# 除此以外还有一些渲染相关的参数也不会被加到Geometry中
box_node = geo_node.createNode("box", run_init_scripts=False) # type: hou.SopNode
box_node.moveToGoodPosition()
# 细分节点
subd_node = geo_node.createNode("subdivide", run_init_scripts=False)# type: hou.SopNode
# NOTE: 设置生成节点的参数
iteration_parm = subd_node.parm("iterations") # type: hou.Parm
if iteration_parm:
iteration_parm.set(3)
subd_node.setInput(0, box_node)
subd_node.moveToGoodPosition() # 自动设置在Houdini 中的 layout
subd_node.setDisplayFlag(True)
subd_node.setRenderFlag(True)
print("\n------------\n")
print("Sub node count under {}:{}".format(geo_node.path(), geo_node.numItems()))

# 参数获取
box_tx_ch = hou.ch("{}/tx".format(box_node.path()))
print("box_tx type: {} val: {}".format(type(box_tx_ch),box_tx_ch))
box_scale = box_node.parm("scale") #type: hou.Parm
print("box_scale type: {} val: {}".format(type(box_scale), box_scale.eval()))
# 复数参数
box_divs = box_node.parmTuple("divs") # type: hou.ParmTuple
print("box_divs type: {} val: {}".format(type(box_divs), box_divs.eval()))
box_parms_list =box_node.parms() # type: tuple[hou.Parm]
print("\n------------\n")
print(box_parms_list)
print("\n------------\n")
box_parm_tuples = box_node.parmTuples() #type: tuple[hou.ParmTuple]
print(box_parm_tuples)
print("\n------------\n")

# NOTE:
# ParmTuple -> size : tuple[Parm]
# Parm -> sizex, sizey, sizez : Parm

# 参数设置
box_node.parm("tx").set(box_tx_ch + 0.5)
box_node.parmTuple("size").set([0.5,0.5,0.5])
box_scale.set(2)
box_divs.set([0,10,0])


# eval 用于返回表达式值
# houdini的设置属性,一般都是set
# 但是如果想获取到属性值,就必须得用eval,他没有对应的get函数,这个和平常不太一样
val_box_div_a = box_divs.eval()
val_box_div_b = box_node.evalParmTuple("divs")
val_geo_tx_a = geo_node.evalParm("tx")
val_geo_tx_b = geo_node.parm("tx").eval()
print("divs:{}, divs:{}, tx:{}, tx:{}".format(val_box_div_a, val_box_div_b, val_geo_tx_a, val_geo_tx_b))

geo_node.moveToGoodPosition()

get_and_set_node_parameters("TestNode")


#############################################
# 从节点反向生成代码
#############################################
# ref: https://elmagnifico.tech/2023/04/21/Houdini-GeometrySpreadsheet/

def get_code_from_object ():

current_root_node = hou.node("/obj")# type: hou.Node
geo_node = current_root_node.createNode("geo") #type: hou.ObjNode
box_node = geo_node.createNode("box", run_init_scripts=False) # type: hou.SopNode
geo_node.setSelected(True, clear_all_selected=True)

# print(hou.node("/obj/box1").asCode())
print(box_node.asCode())

歡迎關注我的其它發布渠道